PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/init_old.c

https://gitlab.com/libreems-suite/libreems-firmware
C | 586 lines | 294 code | 70 blank | 222 comment | 1 complexity | 504180e818db263e1623f880215ea7e3 MD5 | raw file
  1. /* FreeEMS - the open source engine management system
  2. *
  3. * Copyright 2008-2013 Fred Cooke
  4. *
  5. * This file is part of the FreeEMS project.
  6. *
  7. * FreeEMS software is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FreeEMS software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
  19. *
  20. * We ask that if you make any changes to this file you email them upstream to
  21. * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
  22. *
  23. * Thank you for choosing FreeEMS to run your engine!
  24. */
  25. /** @file
  26. *
  27. * @brief Initialise the devices state
  28. *
  29. * Setup, configure and initialise all aspects of the devices state including
  30. * but not limited to:
  31. *
  32. * - Setup the bus clock speed
  33. * - Configuration based variable initialisation
  34. * - I/O register behaviour and initial state
  35. * - Configure and enable interrupts
  36. * - Copy tunable data up to RAM from flash
  37. * - Configure peripheral module behaviour
  38. */
  39. #define INIT_DEPRICATED_C
  40. #include "inc/freeEMS.h"
  41. #include "inc/interrupts_depricated.h"
  42. #include "inc/utils.h"
  43. #include "inc/commsISRs.h"
  44. #include "inc/init_depricated.h"
  45. #include "inc/init.h"
  46. #include "inc/Configuration.h"
  47. #include <string.h>
  48. #include "inc/CANcomms.h"
  49. #include "engineAngleDecoders/common/interface.h"
  50. extern KeyUserDebug KeyUserDebugs;
  51. /** @brief Set the PLL clock frequency
  52. *
  53. * Set the Phase Locked Loop to our desired frequency (80MHz) and enable PLL.
  54. */
  55. void initPLL(){
  56. CLKSEL &= PLLSELOFF; /* Switches to base external OSCCLK to ensure PLL is not being used (off out of reset, but not sure if the monitor turns it on before passing control or not) */
  57. PLLCTL &= PLLOFF; /* Turn the PLL device off to adjust its speed (on by default out of reset) */
  58. REFDV = PLLDIVISOR; /* 16MHz / (3 + 1) = 4MHz Bus frequency */
  59. SYNR = PLLMULTIPLIER; /* 4MHz * (9 + 1) = 40MHz Bus frequency */
  60. PLLCTL |= PLLON; /* Turn the PLL device back on again at 80MHz */
  61. enablePLL();
  62. }
  63. /** @brief Switch to using PLL
  64. *
  65. * Switch to using PLL for clock (40MHz bus speed). Interrupt is enabled elsewhere.
  66. *
  67. * Note: Requires busy wait loop, only for init and emergency use.
  68. *
  69. * @todo Should be limited, and have break out with error code and fall back mechanism.
  70. */
  71. void enablePLL(){
  72. while (!(CRGFLG & LOCK)){
  73. /* Do nothing while we wait till the PLL loop locks onto the target frequency. */
  74. /* Target frequency is given by (2 * (crystal frequency / (REFDV + 1)) * (SYNR + 1)) */
  75. /* Bus frequency is half PLL frequency and given by ((crystal frequency / (REFDV + 1)) * (SYNR + 1)) */
  76. }
  77. CLKSEL = PLLSEL; /* Switches to PLL clock for internal bus frequency */
  78. /* from MC9S12XDP512V2.pdf Section 2.4.1.1.2 page 101 Third paragraph */
  79. /* "This takes a MAXIMUM of 4 OSCCLK clock cylces PLUS 4 PLL clock cycles" */
  80. /* "During this time ALL clocks freeze, and CPU activity ceases" */
  81. /* Therefore there is no point waiting for this to occur, we already are... */
  82. }
  83. /// Set up the analogue inputs
  84. void initADC(){
  85. // Currently not true, and may never be: TODO When the port something uses
  86. // is changed via the tuning interface, the configuration will be done on
  87. // the fly, and the value burned to flash such that next boot happens
  88. // correctly and current running devices are used in that way.
  89. /* Digital input buffers on the ATD channels are off by default, leave them this way! */
  90. //ATD0DIEN = ZEROS; /* You are out of your mind if you waste this on digital Inputs */
  91. //ATD1DIEN0 = ZEROS; /* You are out of your mind if you waste this on digital Inputs (NOT-bonded, can't use) */
  92. //ATD1DIEN1 = ZEROS; /* You are out of your mind if you waste this on digital Inputs */
  93. /* And configure them all for analog input */
  94. //ATD0CTL0 = 0x07/* With mult turned on this is required to be set to cause wrap around, but is correct out of reset */
  95. //ATD0CTL1 = 0x07/* Trigger and interrupt configuration, unused for now. */
  96. ATD0CTL2 = 0xC0; /* Turns on the ADC block and sets auto flag clear */
  97. ATD0CTL3 = 0x40; /* Set sequence length = 8 */
  98. ATD0CTL4 = 0x73; /* Set the ADC clock and sample period for best accuracy */
  99. ATD0CTL5 = 0xB0; /* Sets justification to right, multiplex and scan all channels. Writing to this causes conversions to begin */
  100. /* And configure them all for analog input */
  101. ATD1CTL0 = 0x07; /* TODO bring this out of config based on chip variant variable. Sets wrap on 8th ADC because we can't use the other 8 on 112 pin version */
  102. //ATD0CTL1 = 0x07/* Trigger and interrupt configuration, unused for now. */
  103. ATD1CTL2 = 0xC0; /* Turns on the ADC block and sets auto flag clear */
  104. ATD1CTL3 = 0x40; /* Set sequence length = 8 */
  105. ATD1CTL4 = 0x73; /* Set the ADC clock and sample period for best accuracy */
  106. ATD1CTL5 = 0xB0; /* Sets justification to right, multiplex and scan all channels. Writing to this causes conversions to begin */
  107. }
  108. /** @brief Buffer lookup tables addresses
  109. *
  110. * Save pointers to the lookup tables which live in paged flash.
  111. */
  112. void initLookupAddresses(){
  113. extern const volatile uint16_t IATTransferTable[]; /* 2k */
  114. extern const volatile uint16_t CHTTransferTable[]; /* 2k */
  115. extern const volatile uint16_t MAFTransferTable[]; /* 2k */
  116. extern const volatile uint8_t TestTransferTable[];
  117. extern void* IATTransferTableLocation;
  118. extern void* CHTTransferTableLocation;
  119. extern void* MAFTransferTableLocation;
  120. extern void* TestTransferTableLocation;
  121. IATTransferTableLocation = (void*)&IATTransferTable;
  122. CHTTransferTableLocation = (void*)&CHTTransferTable;
  123. MAFTransferTableLocation = (void*)&MAFTransferTable;
  124. TestTransferTableLocation = (void*)&TestTransferTable;
  125. }
  126. /** @brief Buffer fuel tables addresses
  127. *
  128. * Save pointers to the fuel tables which live in paged flash.
  129. */
  130. void initFuelAddresses(){
  131. extern const volatile mainTable VETableMainFlash;
  132. extern const volatile mainTable VETableSecondaryFlash;
  133. extern const volatile mainTable AirflowTableFlash;
  134. extern const volatile mainTable LambdaTableFlash;
  135. extern const volatile mainTable VETableMainFlash2;
  136. extern const volatile mainTable VETableSecondaryFlash2;
  137. extern const volatile mainTable AirflowTableFlash2;
  138. extern const volatile mainTable LambdaTableFlash2;
  139. extern void* VETableMainFlashLocation;
  140. extern void* VETableMainFlash2Location;
  141. extern void* VETableSecondaryFlashLocation;
  142. extern void* VETableSecondaryFlash2Location;
  143. extern void* AirflowTableFlashLocation;
  144. extern void* AirflowTableFlash2Location;
  145. extern void* LambdaTableFlashLocation;
  146. extern void* LambdaTableFlash2Location;
  147. /* Setup addresses within the page to avoid warnings */
  148. VETableMainFlashLocation = (void*)&VETableMainFlash;
  149. VETableSecondaryFlashLocation = (void*)&VETableSecondaryFlash;
  150. AirflowTableFlashLocation = (void*)&AirflowTableFlash;
  151. LambdaTableFlashLocation = (void*)&LambdaTableFlash;
  152. VETableMainFlash2Location = (void*)&VETableMainFlash2;
  153. VETableSecondaryFlash2Location = (void*)&VETableSecondaryFlash2;
  154. AirflowTableFlash2Location = (void*)&AirflowTableFlash2;
  155. LambdaTableFlash2Location = (void*)&LambdaTableFlash2;
  156. }
  157. /** @brief Copy fuel tables to RAM
  158. *
  159. * Initialises the fuel tables in RAM by copying them up from flash.
  160. */
  161. void initPagedRAMFuel(void){
  162. extern void* VETableMainFlashLocation;
  163. extern void* VETableMainFlash2Location;
  164. extern void* VETableSecondaryFlashLocation;
  165. extern void* VETableSecondaryFlash2Location;
  166. extern void* AirflowTableFlashLocation;
  167. extern void* AirflowTableFlash2Location;
  168. extern void* LambdaTableFlashLocation;
  169. extern void* LambdaTableFlash2Location;
  170. /* Copy the tables from flash to RAM */
  171. RPAGE = RPAGE_FUEL_ONE;
  172. memcpy((void*)&TablesA, VETableMainFlashLocation, sizeof(mainTable));
  173. memcpy((void*)&TablesB, VETableSecondaryFlashLocation, sizeof(mainTable));
  174. memcpy((void*)&TablesC, AirflowTableFlashLocation, sizeof(mainTable));
  175. memcpy((void*)&TablesD, LambdaTableFlashLocation, sizeof(mainTable));
  176. RPAGE = RPAGE_FUEL_TWO;
  177. memcpy((void*)&TablesA, VETableMainFlash2Location, sizeof(mainTable));
  178. memcpy((void*)&TablesB, VETableSecondaryFlash2Location, sizeof(mainTable));
  179. memcpy((void*)&TablesC, AirflowTableFlash2Location, sizeof(mainTable));
  180. memcpy((void*)&TablesD, LambdaTableFlash2Location, sizeof(mainTable));
  181. }
  182. /** @brief Buffer timing tables addresses
  183. *
  184. * Save pointers to the timing tables which live in paged flash.
  185. */
  186. void initTimingAddresses(){
  187. extern const volatile mainTable IgnitionAdvanceTableMainFlash;
  188. extern const volatile mainTable IgnitionAdvanceTableSecondaryFlash;
  189. extern const volatile mainTable InjectionAdvanceTableMainFlash;
  190. extern const volatile mainTable InjectionAdvanceTableSecondaryFlash;
  191. extern const volatile mainTable IgnitionAdvanceTableMainFlash2;
  192. extern const volatile mainTable IgnitionAdvanceTableSecondaryFlash2;
  193. extern const volatile mainTable InjectionAdvanceTableMainFlash2;
  194. extern const volatile mainTable InjectionAdvanceTableSecondaryFlash2;
  195. extern void* IgnitionAdvanceTableMainFlashLocation;
  196. extern void* IgnitionAdvanceTableMainFlash2Location;
  197. extern void* IgnitionAdvanceTableSecondaryFlashLocation;
  198. extern void* IgnitionAdvanceTableSecondaryFlash2Location;
  199. extern void* InjectionAdvanceTableMainFlashLocation;
  200. extern void* InjectionAdvanceTableMainFlash2Location;
  201. extern void* InjectionAdvanceTableSecondaryFlashLocation;
  202. extern void* InjectionAdvanceTableSecondaryFlash2Location;
  203. /* Setup addresses within the page to avoid warnings */
  204. IgnitionAdvanceTableMainFlashLocation = (void*)&IgnitionAdvanceTableMainFlash;
  205. IgnitionAdvanceTableSecondaryFlashLocation = (void*)&IgnitionAdvanceTableSecondaryFlash;
  206. InjectionAdvanceTableMainFlashLocation = (void*)&InjectionAdvanceTableMainFlash;
  207. InjectionAdvanceTableSecondaryFlashLocation = (void*)&InjectionAdvanceTableSecondaryFlash;
  208. IgnitionAdvanceTableMainFlash2Location = (void*)&IgnitionAdvanceTableMainFlash2;
  209. IgnitionAdvanceTableSecondaryFlash2Location = (void*)&IgnitionAdvanceTableSecondaryFlash2;
  210. InjectionAdvanceTableMainFlash2Location = (void*)&InjectionAdvanceTableMainFlash2;
  211. InjectionAdvanceTableSecondaryFlash2Location = (void*)&InjectionAdvanceTableSecondaryFlash2;
  212. }
  213. /** @brief Copy timing tables to RAM
  214. *
  215. * Initialises the timing tables in RAM by copying them up from flash.
  216. */
  217. void initPagedRAMTime(){
  218. extern void* IgnitionAdvanceTableMainFlashLocation;
  219. extern void* IgnitionAdvanceTableMainFlash2Location;
  220. extern void* IgnitionAdvanceTableSecondaryFlashLocation;
  221. extern void* IgnitionAdvanceTableSecondaryFlash2Location;
  222. extern void* InjectionAdvanceTableMainFlashLocation;
  223. extern void* InjectionAdvanceTableMainFlash2Location;
  224. extern void* InjectionAdvanceTableSecondaryFlashLocation;
  225. extern void* InjectionAdvanceTableSecondaryFlash2Location;
  226. /* Copy the tables from flash to RAM */
  227. RPAGE = RPAGE_TIME_ONE;
  228. memcpy((void*)&TablesA, IgnitionAdvanceTableMainFlashLocation, sizeof(mainTable));
  229. memcpy((void*)&TablesB, IgnitionAdvanceTableSecondaryFlashLocation, sizeof(mainTable));
  230. memcpy((void*)&TablesC, InjectionAdvanceTableMainFlashLocation, sizeof(mainTable));
  231. memcpy((void*)&TablesD, InjectionAdvanceTableSecondaryFlashLocation, sizeof(mainTable));
  232. RPAGE = RPAGE_TIME_TWO;
  233. memcpy((void*)&TablesA, IgnitionAdvanceTableMainFlash2Location, sizeof(mainTable));
  234. memcpy((void*)&TablesB, IgnitionAdvanceTableSecondaryFlash2Location, sizeof(mainTable));
  235. memcpy((void*)&TablesC, InjectionAdvanceTableMainFlash2Location, sizeof(mainTable));
  236. memcpy((void*)&TablesD, InjectionAdvanceTableSecondaryFlash2Location, sizeof(mainTable));
  237. }
  238. /** @brief Buffer tunable tables addresses
  239. *
  240. * Save pointers to the tunable tables which live in paged flash and their
  241. * sub-sections too.
  242. */
  243. void initTunableAddresses(){
  244. /* Setup addresses within the page to avoid warnings */
  245. extern const volatile SmallTables1 SmallTablesAFlash;
  246. extern const volatile SmallTables2 SmallTablesBFlash;
  247. extern const volatile SmallTables3 SmallTablesCFlash;
  248. extern const volatile SmallTables4 SmallTablesDFlash;
  249. extern const volatile SmallTables1 SmallTablesAFlash2;
  250. extern const volatile SmallTables2 SmallTablesBFlash2;
  251. extern const volatile SmallTables3 SmallTablesCFlash2;
  252. extern const volatile SmallTables4 SmallTablesDFlash2;
  253. /* Tunable blocks */
  254. extern void* SmallTablesAFlashLocation;
  255. extern void* SmallTablesAFlash2Location;
  256. extern void* SmallTablesBFlashLocation;
  257. extern void* SmallTablesBFlash2Location;
  258. extern void* SmallTablesCFlashLocation;
  259. extern void* SmallTablesCFlash2Location;
  260. extern void* SmallTablesDFlashLocation;
  261. extern void* SmallTablesDFlash2Location;
  262. /* Small chunks of TablesA here */
  263. extern void* voltageVersusDwellDesiredTableLocation;
  264. extern void* voltageVersusDwellDesiredTable2Location;
  265. extern void* injectorDeadTimeTableLocation;
  266. extern void* injectorDeadTimeTable2Location;
  267. extern void* postStartEnrichmentTableLocation;
  268. extern void* postStartEnrichmentTable2Location;
  269. extern void* primingVolumeTableLocation;
  270. extern void* primingVolumeTable2Location;
  271. extern void* engineTempEnrichmentTableLocation;
  272. extern void* engineTempEnrichmentTable2Location;
  273. extern void* RPMVersusDwellTableLocation;
  274. extern void* RPMVersusDwellTable2Location;
  275. extern void* RPMVersusBlendTableLocation;
  276. extern void* RPMVersusBlendTable2Location;
  277. /* Small chunks of TablesB here */
  278. extern void* loggingSettingsLocation;
  279. extern void* loggingSettings2Location;
  280. extern void* perCylinderFuelTrimsLocation;
  281. extern void* perCylinderFuelTrims2Location;
  282. /* Small chunks of TablesC here */
  283. extern void* MAFvsVoltageTableLocation;
  284. extern void* IATvsIgnitionTableLocation;
  285. extern void* CLTvsIgnitionTableLocation;
  286. extern void* ETHvsIgnitionTableLocation;
  287. extern void* fuelDeltaVersusPercentAdderTableLocation;
  288. extern void* EGOlutTableLocation;
  289. extern void* EGTlutTableLocation;
  290. extern void* AAPlutTableLocation;
  291. extern void* MAPlutTableLocation;
  292. extern void* fuelPressurelutTableLocation;
  293. extern void* rpmVersusIACStepsTableLocation;
  294. extern void* rpmVersusEngineTempTableLocation;
  295. /* Fillers here */
  296. extern void* fillerALocation;
  297. extern void* fillerA2Location;
  298. extern void* fillerBLocation;
  299. extern void* fillerB2Location;
  300. extern void* fillerCLocation;
  301. extern void* fillerC2Location;
  302. extern void* fillerDLocation;
  303. extern void* fillerD2Location;
  304. SmallTablesAFlashLocation = (void*)&SmallTablesAFlash;
  305. SmallTablesBFlashLocation = (void*)&SmallTablesBFlash;
  306. SmallTablesCFlashLocation = (void*)&SmallTablesCFlash;
  307. SmallTablesDFlashLocation = (void*)&SmallTablesDFlash;
  308. SmallTablesAFlash2Location = (void*)&SmallTablesAFlash2;
  309. SmallTablesBFlash2Location = (void*)&SmallTablesBFlash2;
  310. SmallTablesCFlash2Location = (void*)&SmallTablesCFlash2;
  311. SmallTablesDFlash2Location = (void*)&SmallTablesDFlash2;
  312. /* TablesA */
  313. voltageVersusDwellDesiredTableLocation = (void*)&SmallTablesAFlash.voltageVSDwellTable;
  314. voltageVersusDwellDesiredTable2Location = (void*)&SmallTablesAFlash2.voltageVSDwellTable;
  315. injectorDeadTimeTableLocation = (void*)&SmallTablesAFlash.injectorDeadTimeTable;
  316. injectorDeadTimeTable2Location = (void*)&SmallTablesAFlash2.injectorDeadTimeTable;
  317. postStartEnrichmentTableLocation = (void*)&SmallTablesAFlash.postStartEnrichmentTable;
  318. postStartEnrichmentTable2Location = (void*)&SmallTablesAFlash2.postStartEnrichmentTable;
  319. primingVolumeTableLocation = (void*)&SmallTablesAFlash.primingVolumeTable;
  320. primingVolumeTable2Location = (void*)&SmallTablesAFlash2.primingVolumeTable;
  321. engineTempEnrichmentTableLocation = (void*)&SmallTablesAFlash.engineTempEnrichmentTable;
  322. engineTempEnrichmentTable2Location = (void*)&SmallTablesAFlash2.engineTempEnrichmentTable;
  323. RPMVersusDwellTableLocation = (void*)&SmallTablesAFlash.RPMVersusDwellTable;
  324. RPMVersusDwellTable2Location = (void*)&SmallTablesAFlash2.RPMVersusDwellTable;
  325. RPMVersusBlendTableLocation = (void*)&SmallTablesAFlash.RPMVersusBlendTable;
  326. RPMVersusBlendTable2Location = (void*)&SmallTablesAFlash2.RPMVersusBlendTable;
  327. /* TablesB */
  328. loggingSettingsLocation = (void*)&SmallTablesBFlash.loggingSettings;
  329. loggingSettings2Location = (void*)&SmallTablesBFlash2.loggingSettings;
  330. perCylinderFuelTrimsLocation = (void*)&SmallTablesBFlash.perCylinderFuelTrims;
  331. perCylinderFuelTrims2Location = (void*)&SmallTablesBFlash2.perCylinderFuelTrims;
  332. /* TablesC */
  333. MAFvsVoltageTableLocation = (void*) &SmallTablesCFlash.MAFvsVoltageTable;
  334. IATvsIgnitionTableLocation = (void*) &SmallTablesCFlash.IATvsIgnitionTable;
  335. CLTvsIgnitionTableLocation = (void*) &SmallTablesCFlash.CLTvsIgnitionTable;
  336. ETHvsIgnitionTableLocation = (void*) &SmallTablesCFlash.ETHvsIgnitionTable;
  337. EGOlutTableLocation = (void*) &SmallTablesCFlash.EGOlut;
  338. EGTlutTableLocation = (void*) &SmallTablesCFlash.EGTlut;
  339. AAPlutTableLocation = (void*) &SmallTablesCFlash.AAPlut;
  340. MAPlutTableLocation = (void*) &SmallTablesCFlash.MAPlut;
  341. fuelPressurelutTableLocation = (void*) &SmallTablesCFlash.fuelPressureLUT;
  342. rpmVersusIACStepsTableLocation = (void*) &SmallTablesCFlash.rpmVersusIACStepsTable;
  343. rpmVersusEngineTempTableLocation = (void*) &SmallTablesCFlash.rpmVersusEngineTempTable;
  344. fuelDeltaVersusPercentAdderTableLocation = (void*) &SmallTablesCFlash.fuelDeltaVersusPercentAdderTable;
  345. /* TablesD */
  346. // TODO
  347. /* filler defs */
  348. fillerALocation = (void*)&SmallTablesAFlash.filler;
  349. fillerA2Location = (void*)&SmallTablesAFlash2.filler;
  350. fillerBLocation = (void*)&SmallTablesBFlash.filler;
  351. fillerB2Location = (void*)&SmallTablesBFlash2.filler;
  352. fillerCLocation = (void*)&SmallTablesCFlash.filler;
  353. fillerC2Location = (void*)&SmallTablesCFlash2.filler;
  354. fillerDLocation = (void*)&SmallTablesDFlash.filler;
  355. fillerD2Location = (void*)&SmallTablesDFlash2.filler;
  356. }
  357. /**
  358. *
  359. */
  360. void initPagedRAMTune(){
  361. /* Tunable blocks */
  362. extern void* SmallTablesAFlashLocation;
  363. extern void* SmallTablesBFlashLocation;
  364. extern void* SmallTablesCFlashLocation;
  365. extern void* SmallTablesDFlashLocation;
  366. /*
  367. extern void* SmallTablesAFlash2Location;
  368. extern void* SmallTablesBFlash2Location;
  369. extern void* SmallTablesCFlash2Location;
  370. extern void* SmallTablesDFlash2Location;
  371. */
  372. /* Copy the tables from flash to RAM */
  373. RPAGE = RPAGE_TUNE_ONE;
  374. memcpy((void*)&TablesA, SmallTablesAFlashLocation, sizeof(mainTable));
  375. memcpy((void*)&TablesB, SmallTablesBFlashLocation, sizeof(mainTable));
  376. memcpy((void*)&TablesC, SmallTablesCFlashLocation, sizeof(mainTable));
  377. memcpy((void*)&TablesD, SmallTablesDFlashLocation, sizeof(mainTable));
  378. }
  379. /** @brief Buffer addresses of paged data
  380. *
  381. * Save the paged memory addresses to variables such that we can access them
  382. * from another paged block with no warnings.
  383. *
  384. * If you try to access paged data from the wrong place you get nasty warnings.
  385. * These calls to functions that live in the same page that they are addressing
  386. * prevent those warnings.
  387. *
  388. * @note Many thanks to Jean BĂ©langer for the inspiration/idea to do this!
  389. */
  390. void initAllPagedAddresses(){
  391. /* Setup pointers to lookup tables */
  392. initLookupAddresses();
  393. /* Setup pointers to the main tables */
  394. initFuelAddresses();
  395. initTimingAddresses();
  396. initTunableAddresses();
  397. }
  398. /** @brief Copies paged flash to RAM
  399. *
  400. * Take the tables and config from flash up to RAM to allow live tuning.
  401. *
  402. * For the main tables and other paged config we need to adjust
  403. * the RPAGE value to the appropriate one before copying up.
  404. *
  405. * This function is simply a delegator to the ones for each flash page. Each
  406. * one lives in the same paged space as the data it is copying up.
  407. */
  408. void initAllPagedRAM(){
  409. /* Setup the flash block pointers before copying flash to RAM using them */
  410. initAllPagedAddresses();
  411. /* Copy the tables up to their paged RAM blocks through the window from flash */
  412. initPagedRAMFuel();
  413. initPagedRAMTime();
  414. initPagedRAMTune();
  415. /* Default to page one for now, perhaps read the configured port straight out of reset in future? TODO */
  416. setupPagedRAM(TRUE); // probably something like (PORTA & TableSwitchingMask)
  417. }
  418. /* Initialise and set up all running variables that require a non-zero start value here */
  419. /* All other variables are initialised to zero by the premain built in code */
  420. void initVariables(){
  421. /* And the opposite for the other halves */
  422. CoreVars = &CoreVars_g;
  423. DerivedVars = &DerivedVars0;
  424. coreStatusA |= FUEL_PUMP_PRIME;
  425. }
  426. /** @brief Flash module setup
  427. *
  428. * Initialise configuration registers for the flash module to allow burning of
  429. * non-volatile flash memory from within the firmware.
  430. *
  431. * The FCLKDIV register can be written once only after reset, thus the lower
  432. * seven bits and the PRDIV8 bit must be set at the same time.
  433. *
  434. * We want to put the flash clock as high as possible between 150kHz and 200kHz
  435. *
  436. * The oscillator clock is 16MHz and because that is above 12.8MHz we will set
  437. * the PRDIV8 bit to further divide by 8 bits as per the manual.
  438. *
  439. * 16MHz = 16000KHz which pre-divided by 8 is 2000kHz
  440. *
  441. * 2000kHz / 200kHz = 10 thus we want to set the divide register to 10 or 0x0A
  442. *
  443. * Combining 0x0A with PRDIV8 gives us 0x4A (0x0A | 0x40 = 0x4A) so we use that
  444. *
  445. * @author Sean Keys
  446. *
  447. * @note If you use a different crystal lower than 12.8MHz PRDIV8 should not be set.
  448. *
  449. * @warning If the frequency you end up with is outside 150kHz - 200kHz you may
  450. * damage your flash module or get corrupt data written to it.
  451. */
  452. void initFlash(){
  453. FCLKDIV = 0x4A; /* Set the flash clock frequency */
  454. FPROT = 0xFF; /* Disable all flash protection */
  455. FSTAT = FSTAT | (PVIOL | ACCERR); /* Clear any errors */
  456. }
  457. /* Setup the sci module(s) that we need to use. */
  458. void initSCIStuff(){
  459. /* The alternative register set selector defaults to zero */
  460. // set the baud/data speed
  461. SCI0BD = Config.comSettings.SC0baudDivisor;
  462. // etc
  463. /* Switch to alternative register set? */
  464. // etc
  465. /* Switch back again? */
  466. /*
  467. * 0 = LOOPS (normal two wire operation)
  468. * 0 = SCISWAI (Wait mode on)
  469. * 0 = RSRC (if loops=1, int/ext wiring)
  470. * 0 = M MODE (9 data bits operation) Only set for use with parity bit.
  471. * 0 = WAKE (idle line wakeup)
  472. * 0 = ILT (idle line type count start pos)
  473. * 0 = PE (parity on)
  474. * 0 = PT (odd parity)
  475. *
  476. * 0x13 = ODD (default)
  477. * 0x12 = EVEN
  478. * 0x00 = NONE
  479. */
  480. SCI0CR1 = 0x00;
  481. /*
  482. * 0 = TIE (tx data empty isr disabled)
  483. * 0 = TCIE (tx complete isr disabled)
  484. * 1 = RIE (rx full isr enabled)
  485. * 0 = ILIE (idle line isr disabled)
  486. * 0 = TE (transmit disabled)
  487. * 1 = RE (receive enabled)
  488. * 0 = RWU (rx wake up normal)
  489. * 0 = SBK (send break off)
  490. */
  491. SCI0CR2 = 0x24;
  492. }
  493. /* Set up all the remaining interrupts */
  494. void initInterrupts(){
  495. /* IMPORTANT : Set the s12x vector base register (Thanks Karsten!!) */
  496. IVBR = 0xF7; /* Without this the interrupts will never find your code! */
  497. /* Set up the Real Time Interrupt */
  498. RTICTL = 0x81; /* 0b_1000_0001 0.125ms/125us period http://duckduckgo.com/?q=1+%2F+%2816MHz+%2F+%282+*+10^3%29+%29 */
  499. // RTICTL = 0xF9; /* 0b_1111_1001 0.125s/125ms period http://duckduckgo.com/?q=1+%2F+%2816MHz+%2F+%282*10^6%29+%29 */
  500. CRGINT |= (RTIE | LOCKIE | SCMIE); /* Enable the Real Time Interrupt, PLL Lock Interrupt, and Self Clock Mode Interrupt */
  501. CRGFLG = (RTIF | LOCKIF | SCMIF); /* Clear the RTI, LOCKI, and SCMI flags */
  502. RAMWPC |= AVIE; // Enable the access protection interrupt for XGATE RAM
  503. // set up port H for testing
  504. PPSH = ZEROS; // falling edge/pull up for all
  505. PIEH = ONES; // enable all pins interrupts
  506. PIFH = ONES; // clear all port H interrupt flags
  507. // TODO set up irq and xirq for testing
  508. // IRQCR for IRQ
  509. /* VReg API setup (only for wait mode? i think so) */
  510. // VREGAPIR = 0x09C3; /* For 500ms period : (500ms - 0.2ms) / 0.2ms = 0b100111000011 = 2499 */
  511. // VREGAPICL = 0x02; /* Enable the interrupt */
  512. // VREGAPICL = 0x04; /* Start the counter running */
  513. /* Writing a one to the flag will set it if it is unset, so best not to mess with it here as it probably starts off unset */
  514. /* LVI Low Voltage Interrupt enable */
  515. VREGCTRL = 0x02; // Counts bad power events for diagnosis reasons
  516. }