PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SCons_build.py
Go to the documentation of this file.
1 # Documentation for this file. If the \file tag isn't present,
2 # this file won't be documented.
3 ## \file
4 # Given a build environment, this file performs specific builds
5 # using the PIC24 library. It should be called from an SCons script
6 # that exports:
7 # - env: the build environment to use
8 # - buildTargets: a list of strings specifying what to build
9 # (see if statements toward the end of this file).
10 #
11 # This file delegates to templates/SConscript.py to build all
12 # templates.
13 
14 
15 import os
16 Import('buildTargets env')
17 
18 
19 ## @{
20 # \name Setup for building files based on the PIC24 support library
21 ###############################################################################
22 
23 
24 ## Common sources used for the PIC24 support library
25 PIC24SupportLibSources = [
26  'lib/common/dataXfer.c',
27  'lib/common/dataXferImpl.c',
28  'lib/common/pic24_adc.c',
29  'lib/common/pic24_clockfreq.c',
30  'lib/common/pic24_configbits.c',
31  'lib/common/pic24_ecan.c',
32  'lib/common/pic24_flash.c',
33  'lib/common/pic24_i2c.c',
34  'lib/common/pic24_serial.c',
35  'lib/common/pic24_spi.c',
36  'lib/common/pic24_timer.c',
37  'lib/common/pic24_uart.c',
38  'lib/common/pic24_util.c' ]
39 
40 PIC24SupportLibSourcesSmall = [
41  'lib/common/pic24_clockfreq.c',
42  'lib/common/pic24_configbits.c',
43  'lib/common/pic24_serial.c',
44  'lib/common/pic24_spi.c',
45  'lib/common/pic24_timer.c',
46  'lib/common/pic24_uart.c',
47  'lib/common/pic24_util.c' ]
48 
49 
50 PIC24SupportLibSourcesSmall3 = [
51  'lib/common/pic24_clockfreq.c',
52  'lib/common/pic24_configbits.c',
53  'lib/common/pic24_serial.c',
54  'lib/common/pic24_spi.c',
55  'lib/common/pic24_i2c.c',
56  'lib/common/pic24_adc.c',
57  'lib/common/pic24_timer.c',
58  'lib/common/pic24_uart.c',
59  'lib/common/pic24_util.c' ]
60 
61 
62 PIC24SupportLibSourcesSmall2 = [
63  'lib/common/pic24_clockfreq.c',
64  'lib/common/dataXfer.c',
65  'lib/common/dataXferImpl.c',
66  'lib/common/pic24_configbits.c',
67  'lib/common/pic24_serial.c',
68  'lib/common/pic24_spi.c',
69  'lib/common/pic24_i2c.c',
70  'lib/common/pic24_adc.c',
71  'lib/common/pic24_timer.c',
72  'lib/common/pic24_uart.c',
73  'lib/common/pic24_util.c' ]
74 
75 
76 
77 
78 # Inform SCons about the dependencies in the template-based files
79 SConscript('./templates/SConscript.py', 'env')
80 
81 # Inform SCons about the dependencies in the ESOS tree
82 #SConscript('./esos/SConscript.py', 'env')
83 
84 ## @}
85 
86 ## @{
87 # \name Example code build
88 ###############################################################################
89 
90 ## This functions converts a cof to a hex file.
91 # \param cofName The name of the .c or .cof file to be converted (.c files
92 # are assumed to be compiled to .cof elsewhere)
93 # \param buildEnvinonment An Environment in which to build these sources.
94 # \param aliasString A string to serve as an alias for this build.
95 def cof2hex(cofName, buildEnvironment, aliasString):
96  f = os.path.splitext(cofName)[0]
97  myHex = buildEnvironment.Hex(f, f)
98  # Add this hex file to a convenient alias
99  buildEnvironment.Alias(aliasString, myHex)
100 
101 ## This function builds a program which includes the PIC24 library.
102 # \param sourceFileList A list of source files to be built into one program.
103 # \param commonSources A list of source files upon which all sources
104 # in the sourceFileList depend. Wildcards are not
105 # supported.
106 # \param buildEnvinonment An Environment in which to build these sources.
107 # \param aliasString A string to serve as an alias for this build.
108 def buildProgramWithCommonSources(sourceFileList, commonSources, buildEnvironment,
109  aliasString):
110  be = buildEnvironment
111  be.Program(sourceFileList + commonSources)
112  # Pick the name of the target to be the first c file in the list
113  cof2hex(sourceFileList[0], be, aliasString)
114 
115 ## This function takes a list of source files (including wildcards),
116 # adds the PIC24 common
117 # sources to each item, then uses Program to build each item.
118 # \param sourceFileList A list of source files (which may include
119 # wildcards) to be built.
120 # \param commonSources A list of source files upon which all sources
121 # in the sourceFileList depend. Wildcards are not
122 # supported.
123 # \param buildEnvinonment An Environment in which to build these sources.
124 # \param exceptionDict A exceptionFileName={key=value pairs used to create the
125 # custom environment correpsonding to the
126 # exceptionFile} dict
127 # \param aliasString A string to serve as an alias for this set of
128 # builds.
129 def buildWithCommonSources(sourceFileList, commonSources, buildEnvironment,
130  exceptionDict, aliasString):
131  for sourceFileGlob in sourceFileList:
132  for sourceFile in Glob(sourceFileGlob, True, True, True):
133  # See if this is an exception
134  if sourceFile in exceptionDict:
135  # Yes, so modify environment with dict of changes.
136  be = buildEnvironment.Clone()
137  flags = exceptionDict[sourceFile]
138 # print flags
139 # be.MergeFlags(flags) # produces weird
140  # errors, so hand-code a simple alternative
141  # TODO: This overwrites flags, which is a BAD thing
142  for a in flags:
143  be[a] = flags[a]
144  else:
145  # No, just use the existing environment
146  be = buildEnvironment
147  buildProgramWithCommonSources([sourceFile], commonSources, be,
148  aliasString)
149 
150 
151 ## This function takes a list of source files (including wildcards),
152 # adds the PIC24 common
153 # libraries to each item, then uses Program to build each item.
154 # \param sourceFileList A list of source files (which may include
155 # wildcards) to be built.
156 # \param commonLibs A list of libraries upon which all sources
157 # in the sourceFileList depend. Wildcards are not
158 # supported.
159 # \param buildEnvinonment An Environment in which to build these sources.
160 def buildWithCommonLibs(sourceFileList, commonLibs, buildEnvironment):
161  for sourceFileGlob in sourceFileList:
162  for sourceFile in Glob(sourceFileGlob, True, True, True):
163  f=os.path.splitext(sourceFile)[0]
164  print sourceFile, f, commonLibs
165  buildEnvironment.Program([sourceFile], LIBS=commonLibs)
166  myHex = buildEnvironment.Hex(f,f)
167  Depends( myHex, commonLibs)
168 
169 ## Instruct SCons to build the common PIC24 library upon which
170 # all the textbook example programs are based.
171 # PIC24 support library files specified in \ref PIC24SupportLibSources.
172 #myLib = env.StaticLibrary(target='lib/msu_pic24', source=PIC24SupportLibSources )
173 
174 # Instruct SCons to build all the textbook example programs based
175 # on the PIC24 support library files specified in \ref
176 # PIC24SupportLibSources.
177 #if 'lib11' in COMMAND_LINE_TARGETS:
178 # #env.Program(['chap11/dac_r2r.c'],LIBS=myLib)
179 # env.Program(['chap11/dac_r2r.c'] + PIC24SupportLibSources)
180 # env.Hex('chap11/dac_r2r','chap11/dac_r2r')
181 # #buildWithCommonLibs(['chap11/*.c'], myLib, env)
182 
183 ## Compile the support library into objects for the default
184 # environment.
185 PIC24SupportLibObjects = env.Object(PIC24SupportLibSources)
186 PIC24SupportLibObjectsSmall = env.Object(PIC24SupportLibSourcesSmall)
187 PIC24SupportLibObjectsSmall2 = env.Object(PIC24SupportLibSourcesSmall2)
188 
189 
190 if 'reset' in buildTargets:
191  buildWithCommonSources(['chap8/reset.c'], PIC24SupportLibObjectsSmall, env, {}, 'reset')
192 if 'echo' in buildTargets:
193  buildWithCommonSources(['chap8/echo.c'], PIC24SupportLibObjectsSmall, env, {}, 'reset')
194 if 'ledsw1' in buildTargets:
195  buildWithCommonSources(['chap8/ledsw1.c'], PIC24SupportLibObjectsSmall, env, {}, 'ledsw1')
196 if 'chap8' in buildTargets:
197  buildWithCommonSources(['chap8/*.c'], PIC24SupportLibObjects, env, {}, 'chap8')
198 if 'chap9' in buildTargets:
199  buildWithCommonSources(['chap9/*.c'], PIC24SupportLibObjects, env, {}, 'chap9')
200 if 'chap10' in buildTargets:
201  buildWithCommonSources([
202  'chap10/mcp24lc515_i2c_eeprom.c',
203  'chap10/reverse_string.c',
204  'chap10/reverse_string1.c',
205  'chap10/soft_uart.c',
206  'chap10/spi_master_revstring.c',
207  'chap10/spi_slave_revstring.c'],
208  PIC24SupportLibObjects, env,
209  {'chap10\\reverse_string.c' : { 'CPPDEFINES': 'UART1_RX_INTERRUPT'},
210  'chap10\\reverse_string1.c' : { 'CPPDEFINES': ['UART1_RX_INTERRUPT', 'UART1_TX_INTERRUPT']}},
211  'chap10')
212 if 'chap10large' in buildTargets: # These files usually need a larger device (64K) to compile
213  buildWithCommonSources(['chap10/stdio_test.c',
214  'chap10/reverse_string_stdio.c', 'chap10/ds1621_i2c.c', 'chap10/ds1631_i2c.c','chap10/ds1722_spi_tempsense.c',
215  'chap10/mcp41xxx_spi_pot.c',],
216  PIC24SupportLibObjects, env, {}, 'chap10stdio')
217 if 'chap11_24E' in buildTargets:
218  buildWithCommonSources([ 'chap11/adc_spidac_test.c',
219  'chap11/adc_test.c',
220  'chap11/adc_test_12bit.c'], PIC24SupportLibSourcesSmall3, env, {}, 'chap11')
221 if 'chap11nodma' in buildTargets:
222  buildWithCommonSources([ 'chap11/adc2pots1.c','chap11/adc4simul.c','chap11/adc7scan1.c', 'chap11/adc7scan2.c', 'chap11/adc_spidac_test.c',
223  'chap11/adc_test.c',
224  'chap11/adc_test_12bit.c', 'chap11/dac_r2r.c'], PIC24SupportLibSourcesSmall3, env, {}, 'chap11')
225 if 'chap11dma' in buildTargets:
226  buildWithCommonSources(['chap11/adc4simul_dma.c', 'chap11/adc7scan1_dma_conv_order.c',
227  'chap11/adc7scan1_dma_scatter_gather_1.c'],
228  PIC24SupportLibObjects, env, {}, 'chap11dma')
229 if 'chap12_24E' in buildTargets:
230  buildWithCommonSources(['chap12/incap_freqmeasure.c','chap12/incap_switch_pulse_measure.c',
231  'chap12/ir_biphase_decode.c', 'chap12/manual_switch_pulse_measure.c',
232  'chap12/outcompare_contpulse.c', 'chap12/outcompare_squarewave.c',
233  'chap12/outputcompare_multiservo.c','chap12/outputcompare_oneservo.c',
234  'chap12/pwm_dac.c', 'chap12/pwm_dac_test.c', 'chap12/timer32bit_switch_pulse_measure.c'], PIC24SupportLibSourcesSmall3, env, {}, 'chap12')
235 if 'chap12' in buildTargets:
236  buildWithCommonSources(['chap12/incap_freqmeasure.c','chap12/incap_switch_pulse_measure.c',
237  'chap12/ir_biphase_decode.c', 'chap12/ledpwm_pic24f.c', 'chap12/manual_switch_pulse_measure.c',
238  'chap12/outcompare_contpulse.c', 'chap12/outcompare_squarewave.c',
239  'chap12/outputcompare_multiservo.c','chap12/outputcompare_oneservo.c',
240  'chap12/pwm_dac.c', 'chap12/pwm_dac_test.c', 'chap12/timer32bit_switch_pulse_measure.c'], PIC24SupportLibSourcesSmall3, env, {}, 'chap12')
241 if 'chap12big' in buildTargets:
242  buildWithCommonSources(['chap12/ledpwm.c'], PIC24SupportLibSourcesSmall2, env, {}, 'chap12')
243 if 'chap13' in buildTargets:
244  buildWithCommonSources(['chap13/*.c'], PIC24SupportLibObjects, env,
245  {'chap13\\i2c_multmaster_rstring.c' : { 'CPPDEFINES': 'CPU_ID=1' },
246  'chap13\\i2c_multmaster_rstring1.c' : { 'CPPDEFINES': 'CPU_ID=2' }},
247  'chap13')
248 if 'chap14' in buildTargets:
249  buildWithCommonSources(['chap14/*.c'], PIC24SupportLibObjects, env, {}, 'chap14')
250 if 'chap15' in buildTargets:
251  # Case-by-case, since this chapter's program have multiple source files
252  buildProgramWithCommonSources(['chap15/audio.c', 'chap15/audio_adpcm.c', ],
253  PIC24SupportLibObjects, env, 'chap15')
254  buildProgramWithCommonSources(['chap15/reflow_oven.c', 'chap15/reflow_debug.c',
255  'chap15/reflow_flash.c', 'chap15/reflow_operate.c'], PIC24SupportLibObjects,
256  env, 'chap15')
257  buildProgramWithCommonSources(['chap15/robot.c', 'chap15/robot_ir_decode.c', ],
258  PIC24SupportLibObjects, env, 'chap15')
259 if 'explorerh' in buildTargets: # Don't build rtcc.c, since the PIC24H doesn't have it
260  buildWithCommonSources(['explorer16_100p/adc_test.c', 'explorer16_100p/lcd4bit.c',
261  'explorer16_100p/mcp25lc256_spi_eeprom.c', 'explorer16_100p/reset.c',
262  'explorer16_100p/timer1_sosc.c'], PIC24SupportLibObjects, env, {}, 'explorerh')
263 if 'explorer' in buildTargets:
264  buildWithCommonSources(['explorer16_100p/*.c'], PIC24SupportLibObjects, env, {}, 'explorer')
265 if 'bootloader' in buildTargets:
266  targetName = 'p${MCU}_230400baud_bootldr'
267  env.Program(target=targetName, source = ['bootloader/24h_24f_target/main.c',
268  'bootloader/24h_24f_target/mem.c',
269  'bootloader/24h_24f_target/pic24_clockfreq.c',
270  'bootloader/24h_24f_target/pic24_configbits.c'])
271  cof2hex(targetName, env, 'bootloader')
272 
273 ## @}
274 
275 
276 #################################################################################
277 ## TESTING
278 ##
279 ## Interestingly, the adc7scan* projects cause the Linux c30-gcc compiler
280 ## to seg-fault.
281 ##
282 #env.Program('dac_r2r.c', LIBS='msu_pic24', LIBPATH='../lib')
283 #MyHex = env.Hex('dac_r2r', 'dac_r2r')
284 #Depends( MyHex, ['dac_r2r.c'])
285 #Depends( MyHex, myLib)
286 ################################
287 #buildWithCommonLibs(['./*.c'], myLib, env)
288 #buildWithCommonLibs(['chap11/adc2pots1.c','chap11/adc4simul.c','chap11/dac_r2r.c'], myLib, env)
289 #buildWithCommonLibs(['chap11/dac*.c','chap11/adc*.c'], myLib, env)
290 #buildWithCommonLibs(['chap10/ds1621_i2c.c'], myLib, env)