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

def select_arm(env, family):
	env.SetDefault(
		TOOLCHAIN = 'arm-none-eabi-',
	)
	
	if family == 'cortex-m4f':
		family = 'cortex-m4'
		has_mcu = True
	else:
		has_mcu = False
	
	env.Replace(
		CC     = '${TOOLCHAIN}gcc',
		CXX    = '${TOOLCHAIN}g++',
		AS     = '${TOOLCHAIN}gcc',
		LINK   = '${TOOLCHAIN}gcc',
		AR     = '${TOOLCHAIN}ar',
		RANLIB = '${TOOLCHAIN}ranlib',
		
		CCFLAGS   = Split('-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections'),
		CXXFLAGS  = Split('-std=gnu++11 -fno-exceptions -fno-rtti -Wno-pmf-conversions'),
		ASFLAGS   = Split('-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb'),
		LINKFLAGS = Split('-Wall -mcpu=${CPU_FAMILY} -mthumb -nostartfiles -Wl,-T${LINK_SCRIPT} -Wl,--gc-sections'),
		
		CPPPATH = [laks_dir],
		LIBPATH = [ld_dir],
		
		LIB_SOURCES = [],
		
		CPU_FAMILY = family,
	)
	
	if has_mcu:
		env.Append(CCFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16'))
		env.Append(LINKFLAGS = Split('-mfloat-abi=hard -mfpu=fpv4-sp-d16'))
		env.Append(CPPDEFINES = ['HAS_FPU'])

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 == 'f303':
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['STM32F3'])
		
		env['LINK_SCRIPT'] = {
			'b': 'stm32_f303_b.ld',
			'c': 'stm32_f303_c.ld',
		}[flash]
	
	elif family == 'f373':
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['STM32F3'])
		
		env['LINK_SCRIPT'] = {
			'8': 'stm32_f373_8.ld',
			'b': 'stm32_f373_b.ld',
			'c': 'stm32_f373_c.ld',
		}[flash]
	
	elif family in ('f401'):
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['STM32F4'])
		
		env['LINK_SCRIPT'] = {
			'e': 'stm32_f401_e.ld',
		}[flash]
	
	elif family in ('f405', 'f407'):
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['STM32F4'])
		
		env['LINK_SCRIPT'] = {
			'e': 'stm32_f4_e.ld',
			'g': 'stm32_f4_g.ld',
		}[flash]
	
	elif family in ('l051', 'l052', 'l053'):
		select_arm(env, 'cortex-m0plus')
		env.Append(CPPDEFINES = ['STM32L0'])
		
		env['LINK_SCRIPT'] = {
			'8': 'stm32_l0_8.ld',
		}[flash]
	
	elif family == 'wb55':
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['STM32WB'])
		
		env['LINK_SCRIPT'] = {
			'g': 'stm32_wb_g.ld',
		}[flash]
	
	else:
		print 'Unknown stm32 family: %s' % family
		Exit(1)

def select_nrf(env, variant):
	if variant == 'nrf52840':
		select_arm(env, 'cortex-m4f')
		env.Append(CPPDEFINES = ['NRF52840'])
		env['LINK_SCRIPT'] = 'nrf52840.ld'

def SelectMCU(env, mcu, variant_dir = None):
	mcu = mcu.lower()
	
	if mcu.startswith('stm32'):
		select_stm32(env, mcu)
	
	elif mcu.startswith('nrf'):
		select_nrf(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, **kwargs):
	objects = [env.Object(f) for f in Flatten([sources, env['LIB_SOURCES']])]
	firmware = env.Program(target, objects, **kwargs)
	#env.Depends(firmware, env['LINK_SCRIPT']) # TODO
	return firmware

AddMethod(Environment, Firmware)

# vim: syn=python