forked from olin-electric-motorsports/MKIV-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
254 lines (210 loc) · 6.25 KB
/
make.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
'''
The official build chain for the 2017-2018 Olin Electric Motorsports FSAE Formula Team
For more information on the team: https://www.olinelectricmotorsports.com/
@author: Peter Seger '20
Released under MIT License 2018
'''
import glob
import os
import re
import sys
import subprocess
import shutil
import time
CC = 'avr-gcc'
PROGRAMMER = 'avrispmkII'
# PROGRAMMER = 'dragon_isp'
PORT = 'usb'
AVRDUDE = 'avrdude'
OBJCOPY = 'avr-objcopy'
MCU = 'atmega16m1'
PART = 'm16'
F_CPU = '4000000UL'
COMPILER = 'gnu99'
FUSE = '0x65'
CFLAGS = '-Os -g -mmcu=' + MCU + ' -std=' + COMPILER + ' -Wall -Werror '
LDFLAG = '-mmcu=' + MCU + ' -lm -std=' + COMPILER + ' -DF_CPU=' + F_CPU
AVRFLAGS = '-B5 -v -c' + PROGRAMMER + ' -p ' + MCU + ' -P ' + PORT
possible_boards = []
def get_input():
board = input("Board (i.e. Dashboard) or build All (all): ")
flash = input("Flash (y/n) or Set Fuses(fuses): ")
return board, flash
def build_boards_list(boards, head):
'''
Goes through the /boards directory and adds each board to a list
'''
os.chdir(boards)
boards = []
bds = glob.glob('*')
for el in bds:
boards.append(el)
os.chdir(head)
return boards
def make_libs(head):
os.chdir('./lib/')
libs = glob.glob('*.c')
os.chdir(head)
return libs
def ensure_setup(board, dir, head):
t = os.listdir(dir)
if 'outputs' not in t:
os.chdir(dir)
os.system('mkdir outs')
os.chdir(head)
def make_elf(board, dir, libs, head):
os.chdir(dir)
c_files = glob.glob('*.c')
h_files = glob.glob('*.h')
os.system('ls')
out = CC + ' '
includes = ''
for item in c_files:
includes = includes + str(item) + (' ')
out = out + includes + CFLAGS + LDFLAG + ' -o ' + board + '.elf'
print(out)
outs = 'outs/'
os.system(out) #Write command to system
cmd = 'mv *.elf outs/'
os.system(cmd)
os.chdir(head)
def make_hex(board, dir, libs, head):
'''
Takes the elf output files and turns them into hex output
'''
outs = 'outs/'
os.chdir(dir)
os.chdir(outs)
elf = glob.glob('*.elf')
out = OBJCOPY + ' -O ihex -R .eeprom ' + elf[0] + ' ' + board +'.hex'
os.system(out) #Write command to system
os.chdir(head)
def flash_board(board, dir, libs, head):
'''
Takes hex files and uses ARVDUDE w/ ARVFLAGS to flash code onto board
'''
os.chdir(dir)
os.chdir('outs/')
hex_file = glob.glob('*.hex')[0]
out = 'sudo ' + AVRDUDE + ' ' + AVRFLAGS + ' -U flash:w:' + hex_file
os.system(out) #Write command to systems
def set_fuse():
'''
Uses ARVDUDE w/ ARVFLAGS to set the fuse
'''
out = 'sudo ' + AVRDUDE + ' ' + AVRFLAGS + ' -U lfuse:w:' + FUSE + ':m'
os.system(out) #Write command to system
def clean(board, dir, head):
'''
Goes into given directory and deletes all output files for a clean build
'''
outs = 'outs/'
os.chdir(dir)
os.chdir(outs)
files = glob.glob('*')
for f in files:
os.remove(f)
os.chdir(head)
print('Clean Build for %s'%(board))
def check_build_date(board, dir, head):
# TODO
'''
Checks to see whether or not the .c or .h files have been modified since the time
when the output files were made. If not, it returns false otherwise it returns true
to indicate the need to rebuild
'''
sub_head = dir
os.chdir(dir + 'outs/')
outs = glob.glob('*')
if len(outs) < 2: # No output files made, assumed new board or error
os.chdir(head)
return True
else:
out_time = os.path.getctime(outs[0])
os.chdir(dir)
c_files = glob.glob('*.c')
h_files = glob.glob('*.h')
if len(c_files) < 1:
print('No C files found at %s',dir)
os.chdir(head)
return True
else:
c_time = os.path.getctime(files[0])
# if <= c_time <
# print(out_time)
os.chdir(head)
def make_all(head, boards):
board_head = './boards/'
for board in boards:
dir = './boards/%s/'%board
ensure_setup(board, dir, cwd)
libs = make_libs(cwd)
clean(board, dir, cwd)
make_elf(board, dir, libs, cwd)
make_hex(board, dir, libs, cwd)
print('-------------------------------------')
print('Build successful! No boards flashed.')
os.chdir(head)
def get_includes(head, board):
'''
This function gathers all the header files from the lib folder for building
'''
out = 'cp lib/* %s'%board
os.system(out)
def remove_includes(head, board):
'''
This function removes the lib files from the directory
'''
os.chdir(head + '/lib/')
includes = glob.glob('*')
os.chdir(head)
os.chdir(board)
out = 'rm '
for x in includes:
out = out + x + ' '
os.system(out)
os.chdir(head)
def clean_wkdr(head, board):
'''
This function cleans the working directory for building
'''
os.chdir(head + '/lib/')
includes = glob.glob('*')
os.chdir(head)
os.chdir(board)
out = 'rm '
for x in includes:
out = out + x + ' '
os.system(out)
os.chdir(head)
if __name__ == "__main__":
# TODO
'''
-Make argc input when file called and setup logic flow for flashing, clean, and board building
'''
cwd = os.getcwd()
boards = './boards/'
possible_boards = build_boards_list(boards, cwd) # Get a list of all boards
board = input("Board (i.e. Dashboard) or build All (all): ")
if(board == 'all'):
make_all(cwd, possible_boards)
else:
flash = input("Flash (y/n) or Set Fuses(fuses): ")
if(flash == 'fuses'):
set_fuse()
exit()
if board in possible_boards:
dir = './boards/%s/'%board
# check_build_date(board, dir, cwd)
clean_wkdr(cwd, dir)
ensure_setup(board, dir, cwd)
libs = make_libs(cwd)
clean(board, dir, cwd)
get_includes(cwd, dir)
make_elf(board, dir, libs, cwd)
make_hex(board, dir, libs, cwd)
remove_includes(cwd, dir)
if(flash == 'y'):
flash_board(board, dir, libs, cwd)
else:
print("Not a possible board --%s--"%(board))