summaryrefslogtreecommitdiff
path: root/build_rules
blob: 3fa0f91ceafae65cee2a935c6dcfc776e03fdd67 (plain)
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
laks_dir = Dir('.')
ld_dir = Dir('ld_scripts')
main_sconscript = File('SConscript')

def select_arm(env, family):
	env.SetDefault(
		TOOLCHAIN = 'arm-none-eabi-',
	)
	
	env.Replace(
		CC = '${TOOLCHAIN}gcc',
		CXX = '${TOOLCHAIN}g++',
		AS = '${TOOLCHAIN}gcc',
		LINK = '${TOOLCHAIN}gcc',
		AR = '${TOOLCHAIN}ar',
		RANLIB = '${TOOLCHAIN}ranlib',
		
		CCFLAGS = '-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections',
		CXXFLAGS = '-std=c++11 -fno-exceptions -fno-rtti -Wno-pmf-conversions',
		ASFLAGS = '-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb',
		LINKFLAGS = '-Wall -mcpu=${CPU_FAMILY} -mthumb -mhard-float -nostartfiles -Wl,-T${LINK_SCRIPT}', # -Wl,--gc-sections
		
		CPPPATH = [laks_dir],
		LIBPATH = [ld_dir],
		
		LIB_SOURCES = [],
		
		CPU_FAMILY = family,
	)
	
	if family == 'cortex-m4':
		env.Append(CCFLAGS = ' -mhard-float')

def select_stm32(env, variant):
	family = variant[5:9]
	pin_count = variant[9]
	flash = variant[10]
	
	if family == 'f103':
		select_arm(env, 'cortex-m3')
		env.Append(CPPDEFINES = ['STM32F1'])
		
		env['LINK_SCRIPT'] = {
			'8': 'stm32_f1_8.ld',
			'b': 'stm32_f1_b.ld',
		}[flash]
	
	elif family in ('f405', 'f407'):
		select_arm(env, 'cortex-m4')
		env.Append(CPPDEFINES = ['STM32F4'])
		
		env['LINK_SCRIPT'] = {
			'e': 'stm32_f4_e.ld',
			'g': 'stm32_f4_g.ld',
		}[flash]
	
	else:
		print 'Unknown stm32 family: %s' % mcu
		Exit(1)

def SelectMCU(env, mcu, variant_dir = None):
	mcu = mcu.lower()
	
	if mcu.startswith('stm32'):
		select_stm32(env, mcu)
	
	else:
		print 'Unknown MCU: %s' % mcu
		Exit(1)
	
	SConscript(main_sconscript, variant_dir = variant_dir, exports = 'env')

AddMethod(Environment, SelectMCU)

def Firmware(env, target, sources):
	firmware = env.Program(target, [sources, env['LIB_SOURCES']])
	#env.Depends(firmware, env['LINK_SCRIPT']) # TODO
	return firmware

AddMethod(Environment, Firmware)

# vim: syn=python