HVBLDC_Sensored-DevInit_F2803x.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. //============================================================================
  2. // FILE: HVBLDC_Sensored-DevInit_F2803x.c
  3. // TITLE: Device initialization for F2803x series
  4. //============================================================================
  5. /***************************
  6. Note that the CLA is not used nor initialized in this project. Please download
  7. the Peripheral Files and Example Projects collection (SPRC832) and look at the
  8. cla_adc project for information on how to use and configure the CLA.
  9. ***************************/
  10. #include "PeripheralHeaderIncludes.h"
  11. // Functions that will be run from RAM need to be assigned to
  12. // a different section. This section will then be mapped to a load and
  13. // run address using the linker cmd file.
  14. #pragma CODE_SECTION(InitFlash, "ramfuncs");
  15. #define Device_cal (void (*)(void))0x3D7C80
  16. void DeviceInit(void);
  17. void PieCntlInit(void);
  18. void PieVectTableInit(void);
  19. void WDogDisable(void);
  20. void PLLset(Uint16);
  21. void ISR_ILLEGAL(void);
  22. //--------------------------------------------------------------------
  23. // Configure Device for target Application Here
  24. //--------------------------------------------------------------------
  25. void DeviceInit(void)
  26. {
  27. WDogDisable(); // Disable the watchdog initially
  28. DINT; // Global Disable all Interrupts
  29. IER = 0x0000; // Disable CPU interrupts
  30. IFR = 0x0000; // Clear all CPU interrupt flags
  31. // Switch to Internal Oscillator 1 and turn off all other clock
  32. // sources to minimize power consumption
  33. /* EALLOW;
  34. SysCtrlRegs.CLKCTL.bit.INTOSC1OFF = 0;
  35. SysCtrlRegs.CLKCTL.bit.OSCCLKSRCSEL=0; // Clk Src = INTOSC1
  36. SysCtrlRegs.CLKCTL.bit.XCLKINOFF=1; // Turn off XCLKIN
  37. SysCtrlRegs.CLKCTL.bit.XTALOSCOFF=1; // Turn off XTALOSC
  38. SysCtrlRegs.CLKCTL.bit.INTOSC2OFF=1; // Turn off INTOSC2
  39. EDIS;*/
  40. //外部20M晶振
  41. EALLOW;
  42. SysCtrlRegs.CLKCTL.bit.XTALOSCOFF = 0; // Turn on XTALOSC
  43. SysCtrlRegs.CLKCTL.bit.XCLKINOFF = 1; // Turn off XCLKIN
  44. SysCtrlRegs.CLKCTL.bit.OSCCLKSRC2SEL = 0; // Switch to external clock
  45. SysCtrlRegs.CLKCTL.bit.OSCCLKSRCSEL = 1; // Switch from INTOSC1 to INTOSC2/ext clk
  46. SysCtrlRegs.CLKCTL.bit.WDCLKSRCSEL = 0; // Clock Watchdog off of INTOSC1
  47. SysCtrlRegs.CLKCTL.bit.INTOSC2OFF = 1; // Turn off INTOSC2
  48. SysCtrlRegs.CLKCTL.bit.INTOSC1OFF = 0; // Leave INTOSC1 on
  49. EDIS;
  50. // SYSTEM CLOCK speed based on internal oscillator = 10 MHz
  51. // 0xC = 60 MHz (12)
  52. // 0xB = 55 MHz (11)
  53. // 0xA = 50 MHz (10)
  54. // 0x9 = 45 MHz (9)
  55. // 0x8 = 40 MHz (8)
  56. // 0x7 = 35 MHz (7)
  57. // 0x6 = 30 MHz (6)
  58. // 0x5 = 25 MHz (5)
  59. // 0x4 = 20 MHz (4)
  60. // 0x3 = 15 MHz (3)
  61. // 0x2 = 10 MHz (2)
  62. PLLset(0x6); // choose from options above
  63. // Initialise interrupt controller and Vector Table
  64. // to defaults for now. Application ISR mapping done later.
  65. PieCntlInit();
  66. PieVectTableInit();
  67. EALLOW; // below registers are "protected", allow access.
  68. // LOW SPEED CLOCKS prescale register settings
  69. SysCtrlRegs.LOSPCP.all = 0x0002; // Sysclk / 4 (15 MHz)
  70. SysCtrlRegs.XCLK.bit.XCLKOUTDIV=2;
  71. // ADC CALIBRATION
  72. //---------------------------------------------------
  73. // The Device_cal function, which copies the ADC & oscillator calibration values
  74. // from TI reserved OTP into the appropriate trim registers, occurs automatically
  75. // in the Boot ROM. If the boot ROM code is bypassed during the debug process, the
  76. // following function MUST be called for the ADC and oscillators to function according
  77. // to specification.
  78. SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // Enable ADC peripheral clock
  79. (*Device_cal)(); // Auto-calibrate from TI OTP
  80. SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 0; // Return ADC clock to original state
  81. // PERIPHERAL CLOCK ENABLES
  82. //---------------------------------------------------
  83. // If you are not using a peripheral you may want to switch
  84. // the clock off to save power, i.e. set to =0
  85. //
  86. // Note: not all peripherals are available on all 280x derivates.
  87. // Refer to the datasheet for your particular device.
  88. SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // ADC
  89. //------------------------------------------------
  90. SysCtrlRegs.PCLKCR3.bit.COMP1ENCLK = 1; // COMP1
  91. SysCtrlRegs.PCLKCR3.bit.COMP2ENCLK = 1; // COMP2
  92. SysCtrlRegs.PCLKCR3.bit.COMP3ENCLK = 1; // COMP3
  93. //------------------------------------------------
  94. SysCtrlRegs.PCLKCR1.bit.ECAP1ENCLK = 1; //eCAP1
  95. //------------------------------------------------
  96. SysCtrlRegs.PCLKCR0.bit.ECANAENCLK= 1; // eCAN-A
  97. //------------------------------------------------
  98. SysCtrlRegs.PCLKCR1.bit.EQEP1ENCLK = 1; // eQEP1
  99. //------------------------------------------------
  100. SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 1; // ePWM1
  101. SysCtrlRegs.PCLKCR1.bit.EPWM2ENCLK = 1; // ePWM2
  102. SysCtrlRegs.PCLKCR1.bit.EPWM3ENCLK = 1; // ePWM3
  103. SysCtrlRegs.PCLKCR1.bit.EPWM4ENCLK = 1; // ePWM4
  104. SysCtrlRegs.PCLKCR1.bit.EPWM5ENCLK = 1; // ePWM5
  105. SysCtrlRegs.PCLKCR1.bit.EPWM6ENCLK = 1; // ePWM6
  106. SysCtrlRegs.PCLKCR1.bit.EPWM7ENCLK = 1; // ePWM7
  107. SysCtrlRegs.PCLKCR0.bit.HRPWMENCLK = 0; // HRPWM
  108. //------------------------------------------------
  109. SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 0; // I2C
  110. //------------------------------------------------
  111. SysCtrlRegs.PCLKCR0.bit.LINAENCLK = 0; // LIN-A
  112. //------------------------------------------------
  113. SysCtrlRegs.PCLKCR3.bit.CLA1ENCLK = 0; // CLA1
  114. //------------------------------------------------
  115. SysCtrlRegs.PCLKCR0.bit.SCIAENCLK = 1; // SCI-A
  116. //------------------------------------------------
  117. SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 0; // SPI-A
  118. SysCtrlRegs.PCLKCR0.bit.SPIBENCLK = 0; // SPI-B
  119. //------------------------------------------------
  120. SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Enable TBCLK
  121. //------------------------------------------------
  122. //--------------------------------------------------------------------------------------
  123. // GPIO (GENERAL PURPOSE I/O) CONFIG
  124. //--------------------------------------------------------------------------------------
  125. //-----------------------
  126. // QUICK NOTES on USAGE:
  127. //-----------------------
  128. // If GpioCtrlRegs.GP?MUX?bit.GPIO?= 1, 2 or 3 (i.e. Non GPIO func), then leave
  129. // rest of lines commented
  130. // If GpioCtrlRegs.GP?MUX?bit.GPIO?= 0 (i.e. GPIO func), then:
  131. // 1) uncomment GpioCtrlRegs.GP?DIR.bit.GPIO? = ? and choose pin to be IN or OUT
  132. // 2) If IN, can leave next to lines commented
  133. // 3) If OUT, uncomment line with ..GPACLEAR.. to force pin LOW or
  134. // uncomment line with ..GPASET.. to force pin HIGH or
  135. //--------------------------------------------------------------------------------------
  136. //--------------------------------------------------------------------------------------
  137. // GPIO-00 - PIN FUNCTION = PWM1A
  138. GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
  139. // GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // 1=OUTput, 0=INput
  140. // GpioDataRegs.GPACLEAR.bit.GPIO0 = 1; // uncomment if --> Set Low initially
  141. // GpioDataRegs.GPASET.bit.GPIO0 = 1; // uncomment if --> Set High initially
  142. //--------------------------------------------------------------------------------------
  143. // GPIO-01 - PIN FUNCTION = PWM1B
  144. GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; // 0=GPIO, 1=EPWM1B, 2=Resv, 3=COMP1OUT
  145. // GpioCtrlRegs.GPADIR.bit.GPIO1 = 1; // 1=OUTput, 0=INput
  146. // GpioDataRegs.GPACLEAR.bit.GPIO1 = 1; // uncomment if --> Set Low initially
  147. // GpioDataRegs.GPASET.bit.GPIO1 = 1; // uncomment if --> Set High initially
  148. //--------------------------------------------------------------------------------------
  149. // GPIO-02 - PIN FUNCTION = PWM2A
  150. GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // 0=GPIO, 1=EPWM2A, 2=Resv, 3=Resv
  151. // GpioCtrlRegs.GPADIR.bit.GPIO2 = 0; // 1=OUTput, 0=INput
  152. // GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // uncomment if --> Set Low initially
  153. // GpioDataRegs.GPASET.bit.GPIO2 = 1; // uncomment if --> Set High initially
  154. //--------------------------------------------------------------------------------------
  155. // GPIO-03 - PIN FUNCTION = PWM2B
  156. GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1; // 0=GPIO, 1=EPWM2B, 2=SPISOMI-A, 3=COMP2OUT
  157. // GpioCtrlRegs.GPADIR.bit.GPIO3 = 0; // 1=OUTput, 0=INput
  158. // GpioDataRegs.GPACLEAR.bit.GPIO3 = 1; // uncomment if --> Set Low initially
  159. // GpioDataRegs.GPASET.bit.GPIO3 = 1; // uncomment if --> Set High initially
  160. //--------------------------------------------------------------------------------------
  161. // GPIO-04 - PIN FUNCTION = PWM3A
  162. GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // 0=GPIO, 1=EPWM3A, 2=Resv, 3=Resv
  163. // GpioCtrlRegs.GPADIR.bit.GPIO4 = 0; // 1=OUTput, 0=INput
  164. // GpioDataRegs.GPACLEAR.bit.GPIO4 = 1; // uncomment if --> Set Low initially
  165. // GpioDataRegs.GPASET.bit.GPIO4 = 1; // uncomment if --> Set High initially
  166. //--------------------------------------------------------------------------------------
  167. // GPIO-05 - PIN FUNCTION = PWM3B
  168. GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1; // 0=GPIO, 1=EPWM3B, 2=SPISIMO-A, 3=ECAP1
  169. // GpioCtrlRegs.GPADIR.bit.GPIO5 = 0; // 1=OUTput, 0=INput
  170. // GpioDataRegs.GPACLEAR.bit.GPIO5 = 1; // uncomment if --> Set Low initially
  171. // GpioDataRegs.GPASET.bit.GPIO5 = 1; // uncomment if --> Set High initially
  172. //--------------------------------------------------------------------------------------
  173. // GPIO-06 - PIN FUNCTION = PWM4A
  174. GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 1; // 0=GPIO, 1=EPWM4A, 2=SYNCI, 3=SYNCO
  175. // GpioCtrlRegs.GPADIR.bit.GPIO6 = 0; // 1=OUTput, 0=INput
  176. // GpioDataRegs.GPACLEAR.bit.GPIO6 = 1; // uncomment if --> Set Low initially
  177. // GpioDataRegs.GPASET.bit.GPIO6 = 1; // uncomment if --> Set High initially
  178. //--------------------------------------------------------------------------------------
  179. // GPIO-07 - PIN FUNCTION = PWM4B
  180. GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 1; // 0=GPIO, 1=EPWM4B, 2=SCIRX-A, 3=Resv
  181. // GpioCtrlRegs.GPADIR.bit.GPIO7 = 0; // 1=OUTput, 0=INput
  182. // GpioDataRegs.GPACLEAR.bit.GPIO7 = 1; // uncomment if --> Set Low initially
  183. // GpioDataRegs.GPASET.bit.GPIO7 = 1; // uncomment if --> Set High initially
  184. //--------------------------------------------------------------------------------------
  185. // GPIO-08 - PIN FUNCTION = --Spare--
  186. GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 1; // 0=GPIO, 1=EPWM5A, 2=Resv, 3=ADCSOC-A
  187. // GpioCtrlRegs.GPADIR.bit.GPIO8 = 0; // 1=OUTput, 0=INput
  188. // GpioDataRegs.GPACLEAR.bit.GPIO8 = 1; // uncomment if --> Set Low initially
  189. // GpioDataRegs.GPASET.bit.GPIO8 = 1; // uncomment if --> Set High initially
  190. //--------------------------------------------------------------------------------------
  191. // GPIO-09 - PIN FUNCTION = Clear Fault
  192. GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0; // 0=GPIO, 1=EPWM5B, 2=LINTX-A, 3=Resv
  193. GpioCtrlRegs.GPADIR.bit.GPIO9 = 1; // 1=OUTput, 0=INput
  194. // GpioDataRegs.GPACLEAR.bit.GPIO9 = 1; // uncomment if --> Set Low initially
  195. GpioDataRegs.GPASET.bit.GPIO9 = 1; // uncomment if --> Set High initially
  196. //--------------------------------------------------------------------------------------
  197. // GPIO-10 - PIN FUNCTION = --Spare--
  198. GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1; // 0=GPIO, 1=EPWM6A, 2=Resv, 3=ADCSOC-B
  199. // GpioCtrlRegs.GPADIR.bit.GPIO10 = 0; // 1=OUTput, 0=INput
  200. // GpioDataRegs.GPACLEAR.bit.GPIO10 = 1; // uncomment if --> Set Low initially
  201. // GpioDataRegs.GPASET.bit.GPIO10 = 1; // uncomment if --> Set High initially
  202. //--------------------------------------------------------------------------------------
  203. // GPIO-11 - PIN FUNCTION = --Spare--
  204. GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1; // 0=GPIO, 1=EPWM6B, 2=LINRX-A, 3=Resv
  205. // GpioCtrlRegs.GPADIR.bit.GPIO11 = 0; // 1=OUTput, 0=INput
  206. // GpioDataRegs.GPACLEAR.bit.GPIO11 = 1; // uncomment if --> Set Low initially
  207. // GpioDataRegs.GPASET.bit.GPIO11 = 1; // uncomment if --> Set High initially
  208. //--------------------------------------------------------------------------------------
  209. // GPIO-12 - PIN FUNCTION = TZ1
  210. GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 1; // 0=GPIO, 1=TZ1, 2=SCITX-A, 3=SPISIMO-B
  211. GpioCtrlRegs.GPADIR.bit.GPIO12 = 0; // 1=OUTput, 0=INput
  212. // GpioDataRegs.GPACLEAR.bit.GPIO12 = 0; // uncomment if --> Set Low initially
  213. // GpioDataRegs.GPASET.bit.GPIO12 = 1; // uncomment if --> Set High initially
  214. //--------------------------------------------------------------------------------------
  215. // GPIO-13 - PIN FUNCTION = --Spare--
  216. GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // 0=GPIO, 1=TZ2, 2=Resv, 3=SPISOMI-B
  217. GpioCtrlRegs.GPADIR.bit.GPIO13 = 0; // 1=OUTput, 0=INput
  218. // GpioDataRegs.GPACLEAR.bit.GPIO13 = 1; // uncomment if --> Set Low initially
  219. // GpioDataRegs.GPASET.bit.GPIO13 = 1; // uncomment if --> Set High initially
  220. //--------------------------------------------------------------------------------------
  221. // GPIO-14 - PIN FUNCTION = --Spare--
  222. GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 0; // 0=GPIO, 1=TZ3, 2=LINTX-A, 3=SPICLK-B
  223. GpioCtrlRegs.GPADIR.bit.GPIO14 = 0; // 1=OUTput, 0=INput
  224. // GpioDataRegs.GPACLEAR.bit.GPIO14 = 1; // uncomment if --> Set Low initially
  225. // GpioDataRegs.GPASET.bit.GPIO14 = 1; // uncomment if --> Set High initially
  226. //--------------------------------------------------------------------------------------
  227. // GPIO-15 - PIN FUNCTION = OCP
  228. GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 0; // 0=GPIO, 1=TZ1, 2=LINRX-A, 3=SPISTE-B
  229. GpioCtrlRegs.GPADIR.bit.GPIO15 = 0; // 1=OUTput, 0=INput
  230. // GpioDataRegs.GPACLEAR.bit.GPIO15 = 1; // uncomment if --> Set Low initially
  231. // GpioDataRegs.GPASET.bit.GPIO15 = 1; // uncomment if --> Set High initially
  232. //--------------------------------------------------------------------------------------
  233. //--------------------------------------------------------------------------------------
  234. // GPIO-16 - PIN FUNCTION = SPISIMO-A fault
  235. GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 0; // 0=GPIO, 1=SPISIMO-A, 2=Resv, 3=TZ2
  236. GpioCtrlRegs.GPADIR.bit.GPIO16 = 0; // 1=OUTput, 0=INput
  237. // GpioDataRegs.GPACLEAR.bit.GPIO16 = 1; // uncomment if --> Set Low initially
  238. // GpioDataRegs.GPASET.bit.GPIO16 = 1; // uncomment if --> Set High initially
  239. //--------------------------------------------------------------------------------------
  240. // GPIO-17 - PIN FUNCTION = SPISOMI-A
  241. GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 0; // 0=GPIO, 1=SPISOMI-A, 2=Resv, 3=TZ3
  242. // GpioCtrlRegs.GPADIR.bit.GPIO17 = 0; // 1=OUTput, 0=INput
  243. // GpioDataRegs.GPACLEAR.bit.GPIO17 = 1; // uncomment if --> Set Low initially
  244. // GpioDataRegs.GPASET.bit.GPIO17 = 1; // uncomment if --> Set High initially
  245. //--------------------------------------------------------------------------------------
  246. // GPIO-18 - PIN FUNCTION = SPICLK-A
  247. GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // 0=GPIO, 1=SPICLK-A, 2=LINTX-A, 3=XCLKOUT
  248. // GpioCtrlRegs.GPADIR.bit.GPIO18 = 1; // 1=OUTput, 0=INput
  249. // GpioDataRegs.GPACLEAR.bit.GPIO18 = 1; // uncomment if --> Set Low initially
  250. // GpioDataRegs.GPASET.bit.GPIO18 = 1; // uncomment if --> Set High initially
  251. //--------------------------------------------------------------------------------------
  252. // GPIO-19 - PIN FUNCTION = SPISTE-A
  253. GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // 0=GPIO, 1=SPISTE-A, 2=LINRX-A, 3=ECAP1
  254. // GpioCtrlRegs.GPADIR.bit.GPIO19 = 0; // 1=OUTput, 0=INput
  255. // GpioDataRegs.GPACLEAR.bit.GPIO19 = 1; // uncomment if --> Set Low initially
  256. // GpioDataRegs.GPASET.bit.GPIO19 = 1; // uncomment if --> Set High initially
  257. //--------------------------------------------------------------------------------------
  258. // GPIO-20 - PIN FUNCTION = EQEPA-1
  259. GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 1; // 0=GPIO, 1=EQEPA-1, 2=Resv, 3=COMP1OUT
  260. // GpioCtrlRegs.GPADIR.bit.GPIO20 = 0; // 1=OUTput, 0=INput
  261. // GpioDataRegs.GPACLEAR.bit.GPIO20 = 1; // uncomment if --> Set Low initially
  262. // GpioDataRegs.GPASET.bit.GPIO20 = 1; // uncomment if --> Set High initially
  263. //--------------------------------------------------------------------------------------
  264. // GPIO-21 - PIN FUNCTION = EQEPB-1
  265. GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 1; // 0=GPIO, 1=EQEPB-1, 2=Resv, 3=COMP2OUT
  266. // GpioCtrlRegs.GPADIR.bit.GPIO21 = 0; // 1=OUTput, 0=INput
  267. // GpioDataRegs.GPACLEAR.bit.GPIO21 = 1; // uncomment if --> Set Low initially
  268. // GpioDataRegs.GPASET.bit.GPIO21 = 1; // uncomment if --> Set High initially
  269. //--------------------------------------------------------------------------------------
  270. // GPIO-22 - PIN FUNCTION = GPIO LED低电平有效
  271. GpioCtrlRegs.GPAMUX2.bit.GPIO22 = 0; // 0=GPIO, 1=EQEPS-1, 2=Resv, 3=LINTX-A
  272. GpioCtrlRegs.GPADIR.bit.GPIO22 = 1; // 1=OUTput, 0=INput
  273. // GpioDataRegs.GPACLEAR.bit.GPIO22 = 1; // uncomment if --> Set Low initially
  274. GpioDataRegs.GPASET.bit.GPIO22 = 1; // uncomment if --> Set High initially
  275. //--------------------------------------------------------------------------------------
  276. // GPIO-23 - PIN FUNCTION = EQEPI-1
  277. GpioCtrlRegs.GPAMUX2.bit.GPIO23 = 1; // 0=GPIO, 1=EQEPI-1, 2=Resv, 3=LINRX-A
  278. // GpioCtrlRegs.GPADIR.bit.GPIO23 = 0; // 1=OUTput, 0=INput
  279. // GpioDataRegs.GPACLEAR.bit.GPIO23 = 1; // uncomment if --> Set Low initially
  280. // GpioDataRegs.GPASET.bit.GPIO23 = 1; // uncomment if --> Set High initially
  281. //--------------------------------------------------------------------------------------
  282. // GPIO-24 - PIN FUNCTION = Hall1 使用cap就不能都IO口
  283. GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 0; // 0=GPIO, 1=ECAP1, 2=Resv, 3=SPISIMO-B
  284. GpioCtrlRegs.GPADIR.bit.GPIO24 = 0; // 1=OUTput, 0=INput
  285. //GpioCtrlRegs.GPAQSEL2.bit.GPIO24 = 0; // Synch to SYSCLKOUT GPIO24 (CAP1)
  286. //GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0;
  287. // GpioCtrlRegs.GPAPUD.bit.GPIO24 =0;
  288. // GpioDataRegs.GPACLEAR.bit.GPIO24 = 1; // uncomment if --> Set Low initially
  289. // GpioDataRegs.GPASET.bit.GPIO24 = 1; // uncomment if --> Set High initially
  290. //--------------------------------------------------------------------------------------
  291. // GPIO-25 - PIN FUNCTION = Hall2
  292. GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=SPISOMI-B
  293. GpioCtrlRegs.GPADIR.bit.GPIO25 = 0; // 1=OUTput, 0=INput
  294. // GpioCtrlRegs.GPAPUD.bit.GPIO25 =0;
  295. // GpioDataRegs.GPACLEAR.bit.GPIO25 = 1; // uncomment if --> Set Low initially
  296. // GpioDataRegs.GPASET.bit.GPIO25 = 1; // uncomment if --> Set High initially
  297. //--------------------------------------------------------------------------------------
  298. // GPIO-26 - PIN FUNCTION = Hall3
  299. GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=SPICLK-B
  300. GpioCtrlRegs.GPADIR.bit.GPIO26 = 0; // 1=OUTput, 0=INput
  301. // GpioCtrlRegs.GPAPUD.bit.GPIO26 =0;
  302. // GpioDataRegs.GPACLEAR.bit.GPIO26 = 1; // uncomment if --> Set Low initially
  303. // GpioDataRegs.GPASET.bit.GPIO26 = 1; // uncomment if --> Set High initially
  304. //--------------------------------------------------------------------------------------
  305. // GPIO-27 - PIN FUNCTION = --Spare--
  306. GpioCtrlRegs.GPAMUX2.bit.GPIO27 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=SPISTE-B
  307. GpioCtrlRegs.GPADIR.bit.GPIO27 = 0; // 1=OUTput, 0=INput
  308. // GpioDataRegs.GPACLEAR.bit.GPIO27 = 1; // uncomment if --> Set Low initially
  309. // GpioDataRegs.GPASET.bit.GPIO27 = 1; // uncomment if --> Set High initially
  310. //--------------------------------------------------------------------------------------
  311. // GPIO-28 - PIN FUNCTION = SCI-RX
  312. GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; // 0=GPIO, 1=SCIRX-A, 2=I2CSDA-A, 3=TZ2
  313. // GpioCtrlRegs.GPADIR.bit.GPIO28 = 1; // 1=OUTput, 0=INput
  314. // GpioDataRegs.GPACLEAR.bit.GPIO28 = 1; // uncomment if --> Set Low initially
  315. // GpioDataRegs.GPASET.bit.GPIO28 = 1; // uncomment if --> Set High initially
  316. //--------------------------------------------------------------------------------------
  317. // GPIO-29 - PIN FUNCTION = SCI-TX
  318. GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; // 0=GPIO, 1=SCITXD-A, 2=I2CSCL-A, 3=TZ3
  319. // GpioCtrlRegs.GPADIR.bit.GPIO29 = 0; // 1=OUTput, 0=INput
  320. // GpioDataRegs.GPACLEAR.bit.GPIO29 = 1; // uncomment if --> Set Low initially
  321. // GpioDataRegs.GPASET.bit.GPIO29 = 1; // uncomment if --> Set High initially
  322. //--------------------------------------------------------------------------------------
  323. // GPIO-30 - PIN FUNCTION = CANRX-A
  324. GpioCtrlRegs.GPAMUX2.bit.GPIO30 = 1; // 0=GPIO, 1=CANRX-A, 2=Resv, 3=Resv
  325. GpioCtrlRegs.GPADIR.bit.GPIO30 = 0; // 1=OUTput, 0=INput
  326. // GpioDataRegs.GPACLEAR.bit.GPIO30 = 1; // uncomment if --> Set Low initially
  327. // GpioDataRegs.GPASET.bit.GPIO30 = 1; // uncomment if --> Set High initially
  328. //--------------------------------------------------------------------------------------
  329. // GPIO-31 - PIN FUNCTION = CANTX-A
  330. GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 1; // 0=GPIO, 1=CANTX-A, 2=Resv, 3=Resv
  331. GpioCtrlRegs.GPADIR.bit.GPIO31 = 1; // 1=OUTput, 0=INput
  332. // GpioDataRegs.GPACLEAR.bit.GPIO31 = 1; // uncomment if --> Set Low initially
  333. // GpioDataRegs.GPASET.bit.GPIO31 = 1; // uncomment if --> Set High initially
  334. //--------------------------------------------------------------------------------------
  335. //--------------------------------------------------------------------------------------
  336. // GPIO-32 - PIN FUNCTION = I2CSDA-A
  337. GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 0; // 0=GPIO, 1=I2CSDA-A, 2=SYNCI, 3=ADCSOCA
  338. GpioCtrlRegs.GPBDIR.bit.GPIO32 = 1; // 1=OUTput, 0=INput
  339. // GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1; // uncomment if --> Set Low initially
  340. GpioDataRegs.GPBSET.bit.GPIO32 = 1; // uncomment if --> Set High initially
  341. //--------------------------------------------------------------------------------------
  342. // GPIO-33 - PIN FUNCTION = I2CSCL-A
  343. GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 0; // 0=GPIO, 1=I2CSCL-A, 2=SYNCO, 3=ADCSOCB
  344. GpioCtrlRegs.GPBDIR.bit.GPIO33 = 1; // 1=OUTput, 0=INput
  345. // GpioDataRegs.GPBCLEAR.bit.GPIO33 = 1; // uncomment if --> Set Low initially
  346. GpioDataRegs.GPBSET.bit.GPIO33 = 1; // uncomment if --> Set High initially
  347. //--------------------------------------------------------------------------------------
  348. // GPIO-34 - PIN FUNCTION = LED3 on controlCARD
  349. // GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=Resv
  350. // GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1; // 1=OUTput, 0=INput
  351. // GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1; // uncomment if --> Set Low initially
  352. // GpioDataRegs.GPBSET.bit.GPIO34 = 1; // uncomment if --> Set High initially
  353. //--------------------------------------------------------------------------------------
  354. //--------------------------------------------------------------------------------------
  355. // GPIO 35-38 are defaulted to JTAG usage, and are not shown here to enforce JTAG debug
  356. // usage.
  357. //--------------------------------------------------------------------------------------
  358. //--------------------------------------------------------------------------------------
  359. // GPIO-39 - PIN FUNCTION = GPIO
  360. GpioCtrlRegs.GPBMUX1.bit.GPIO39 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=Resv
  361. GpioCtrlRegs.GPBDIR.bit.GPIO39 = 1; // 1=OUTput, 0=INput
  362. // GpioDataRegs.GPBCLEAR.bit.GPIO39 = 1; // uncomment if --> Set Low initially
  363. // GpioDataRegs.GPBSET.bit.GPIO39 = 1; // uncomment if --> Set High initially
  364. //--------------------------------------------------------------------------------------
  365. // GPIO-40 - PIN FUNCTION = EPWM7A
  366. GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // 0=GPIO, 1=EPWM7A, 2=Resv, 3=Resv
  367. // GpioCtrlRegs.GPBDIR.bit.GPIO40 = 0; // 1=OUTput, 0=INput
  368. // GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1; // uncomment if --> Set Low initially
  369. // GpioDataRegs.GPBSET.bit.GPIO40 = 1; // uncomment if --> Set High initially
  370. //--------------------------------------------------------------------------------------
  371. // GPIO-41 - PIN FUNCTION = EPWM7B
  372. GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 1; // 0=GPIO, 1=EPWM7B, 2=Resv, 3=Resv
  373. // GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1; // 1=OUTput, 0=INput
  374. // GpioDataRegs.GPBCLEAR.bit.GPIO41 = 1; // uncomment if --> Set Low initially
  375. // GpioDataRegs.GPBSET.bit.GPIO41 = 1; // uncomment if --> Set High initially
  376. //--------------------------------------------------------------------------------------
  377. // GPIO-42 - PIN FUNCTION = LED2
  378. GpioCtrlRegs.GPBMUX1.bit.GPIO42 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=COMP1OUT
  379. GpioCtrlRegs.GPBDIR.bit.GPIO42 = 1; // 1=OUTput, 0=INput
  380. GpioDataRegs.GPBCLEAR.bit.GPIO42 = 1; // uncomment if --> Set Low initially
  381. // GpioDataRegs.GPBSET.bit.GPIO42 = 1; // uncomment if --> Set High initially
  382. //--------------------------------------------------------------------------------------
  383. // GPIO-43 - PIN FUNCTION = --Spare--
  384. GpioCtrlRegs.GPBMUX1.bit.GPIO43 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=COMP2OUT
  385. GpioCtrlRegs.GPBDIR.bit.GPIO43 = 0; // 1=OUTput, 0=INput
  386. // GpioDataRegs.GPBCLEAR.bit.GPIO43 = 1; // uncomment if --> Set Low initially
  387. // GpioDataRegs.GPBSET.bit.GPIO43 = 1; // uncomment if --> Set High initially
  388. //--------------------------------------------------------------------------------------
  389. // GPIO-44 - PIN FUNCTION = LED1
  390. GpioCtrlRegs.GPBMUX1.bit.GPIO44 = 0; // 0=GPIO, 1=Resv, 2=Resv, 3=Resv
  391. GpioCtrlRegs.GPBDIR.bit.GPIO44 = 1; // 1=OUTput, 0=INput
  392. GpioDataRegs.GPBCLEAR.bit.GPIO44 = 1; // uncomment if --> Set Low initially
  393. // GpioDataRegs.GPBSET.bit.GPIO44 = 1; // uncomment if --> Set High initially
  394. //--------------------------------------------------------------------------------------
  395. //--------------------------------------------------------------------------------------
  396. EDIS; // Disable register access
  397. }
  398. //============================================================================
  399. // NOTE:
  400. // IN MOST APPLICATIONS THE FUNCTIONS AFTER THIS POINT CAN BE LEFT UNCHANGED
  401. // THE USER NEED NOT REALLY UNDERSTAND THE BELOW CODE TO SUCCESSFULLY RUN THIS
  402. // APPLICATION.
  403. //============================================================================
  404. void WDogDisable(void)
  405. {
  406. EALLOW;
  407. SysCtrlRegs.WDCR= 0x0068;
  408. EDIS;
  409. }
  410. // This function initializes the PLLCR register.
  411. //void InitPll(Uint16 val, Uint16 clkindiv)
  412. void PLLset(Uint16 val)
  413. {
  414. volatile Uint16 iVol;
  415. // Make sure the PLL is not running in limp mode
  416. if (SysCtrlRegs.PLLSTS.bit.MCLKSTS != 0)
  417. {
  418. EALLOW;
  419. // OSCCLKSRC1 failure detected. PLL running in limp mode.
  420. // Re-enable missing clock logic.
  421. SysCtrlRegs.PLLSTS.bit.MCLKCLR = 1;
  422. EDIS;
  423. // Replace this line with a call to an appropriate
  424. // SystemShutdown(); function.
  425. asm(" ESTOP0"); // Uncomment for debugging purposes
  426. }
  427. // DIVSEL MUST be 0 before PLLCR can be changed from
  428. // 0x0000. It is set to 0 by an external reset XRSn
  429. // This puts us in 1/4
  430. if (SysCtrlRegs.PLLSTS.bit.DIVSEL != 0)
  431. {
  432. EALLOW;
  433. SysCtrlRegs.PLLSTS.bit.DIVSEL = 0;
  434. EDIS;
  435. }
  436. // Change the PLLCR
  437. if (SysCtrlRegs.PLLCR.bit.DIV != val)
  438. {
  439. EALLOW;
  440. // Before setting PLLCR turn off missing clock detect logic
  441. SysCtrlRegs.PLLSTS.bit.MCLKOFF = 1;
  442. SysCtrlRegs.PLLCR.bit.DIV = val;
  443. EDIS;
  444. // Optional: Wait for PLL to lock.
  445. // During this time the CPU will switch to OSCCLK/2 until
  446. // the PLL is stable. Once the PLL is stable the CPU will
  447. // switch to the new PLL value.
  448. //
  449. // This time-to-lock is monitored by a PLL lock counter.
  450. //
  451. // Code is not required to sit and wait for the PLL to lock.
  452. // However, if the code does anything that is timing critical,
  453. // and requires the correct clock be locked, then it is best to
  454. // wait until this switching has completed.
  455. // Wait for the PLL lock bit to be set.
  456. // The watchdog should be disabled before this loop, or fed within
  457. // the loop via ServiceDog().
  458. // Uncomment to disable the watchdog
  459. WDogDisable();
  460. while(SysCtrlRegs.PLLSTS.bit.PLLLOCKS != 1) {}
  461. EALLOW;
  462. SysCtrlRegs.PLLSTS.bit.MCLKOFF = 0;
  463. EDIS;
  464. }
  465. //divide down SysClk by 2 to increase stability
  466. EALLOW;
  467. SysCtrlRegs.PLLSTS.bit.DIVSEL = 2;
  468. EDIS;
  469. }
  470. // This function initializes the PIE control registers to a known state.
  471. //
  472. void PieCntlInit(void)
  473. {
  474. // Disable Interrupts at the CPU level:
  475. DINT;
  476. // Disable the PIE
  477. PieCtrlRegs.PIECTRL.bit.ENPIE = 0;
  478. // Clear all PIEIER registers:
  479. PieCtrlRegs.PIEIER1.all = 0;
  480. PieCtrlRegs.PIEIER2.all = 0;
  481. PieCtrlRegs.PIEIER3.all = 0;
  482. PieCtrlRegs.PIEIER4.all = 0;
  483. PieCtrlRegs.PIEIER5.all = 0;
  484. PieCtrlRegs.PIEIER6.all = 0;
  485. PieCtrlRegs.PIEIER7.all = 0;
  486. PieCtrlRegs.PIEIER8.all = 0;
  487. PieCtrlRegs.PIEIER9.all = 0;
  488. PieCtrlRegs.PIEIER10.all = 0;
  489. PieCtrlRegs.PIEIER11.all = 0;
  490. PieCtrlRegs.PIEIER12.all = 0;
  491. // Clear all PIEIFR registers:
  492. PieCtrlRegs.PIEIFR1.all = 0;
  493. PieCtrlRegs.PIEIFR2.all = 0;
  494. PieCtrlRegs.PIEIFR3.all = 0;
  495. PieCtrlRegs.PIEIFR4.all = 0;
  496. PieCtrlRegs.PIEIFR5.all = 0;
  497. PieCtrlRegs.PIEIFR6.all = 0;
  498. PieCtrlRegs.PIEIFR7.all = 0;
  499. PieCtrlRegs.PIEIFR8.all = 0;
  500. PieCtrlRegs.PIEIFR9.all = 0;
  501. PieCtrlRegs.PIEIFR10.all = 0;
  502. PieCtrlRegs.PIEIFR11.all = 0;
  503. PieCtrlRegs.PIEIFR12.all = 0;
  504. }
  505. void PieVectTableInit(void)
  506. {
  507. int16 i;
  508. PINT *Dest = &PieVectTable.TINT1;
  509. EALLOW;
  510. for(i=0; i < 115; i++)
  511. *Dest++ = &ISR_ILLEGAL;
  512. EDIS;
  513. // Enable the PIE Vector Table
  514. PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
  515. }
  516. interrupt void ISR_ILLEGAL(void) // Illegal operation TRAP
  517. {
  518. // Insert ISR Code here
  519. // Next two lines for debug only to halt the processor here
  520. // Remove after inserting ISR Code
  521. asm(" ESTOP0");
  522. for(;;);
  523. }
  524. // This function initializes the Flash Control registers
  525. // CAUTION
  526. // This function MUST be executed out of RAM. Executing it
  527. // out of OTP/Flash will yield unpredictable results
  528. void InitFlash(void)
  529. {
  530. EALLOW;
  531. //Enable Flash Pipeline mode to improve performance
  532. //of code executed from Flash.
  533. FlashRegs.FOPT.bit.ENPIPE = 1;
  534. // CAUTION
  535. //Minimum waitstates required for the flash operating
  536. //at a given CPU rate must be characterized by TI.
  537. //Refer to the datasheet for the latest information.
  538. //Set the Paged Waitstate for the Flash
  539. FlashRegs.FBANKWAIT.bit.PAGEWAIT = 3;
  540. //Set the Random Waitstate for the Flash
  541. FlashRegs.FBANKWAIT.bit.RANDWAIT = 3;
  542. //Set the Waitstate for the OTP
  543. FlashRegs.FOTPWAIT.bit.OTPWAIT = 5;
  544. // CAUTION
  545. //ONLY THE DEFAULT VALUE FOR THESE 2 REGISTERS SHOULD BE USED
  546. FlashRegs.FSTDBYWAIT.bit.STDBYWAIT = 0x01FF;
  547. FlashRegs.FACTIVEWAIT.bit.ACTIVEWAIT = 0x01FF;
  548. EDIS;
  549. //Force a pipeline flush to ensure that the write to
  550. //the last register configured occurs before returning.
  551. asm(" RPT #7 || NOP");
  552. }
  553. // This function will copy the specified memory contents from
  554. // one location to another.
  555. //
  556. // Uint16 *SourceAddr Pointer to the first word to be moved
  557. // SourceAddr < SourceEndAddr
  558. // Uint16* SourceEndAddr Pointer to the last word to be moved
  559. // Uint16* DestAddr Pointer to the first destination word
  560. //
  561. // No checks are made for invalid memory locations or that the
  562. // end address is > then the first start address.
  563. void MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr)
  564. {
  565. while(SourceAddr < SourceEndAddr)
  566. {
  567. *DestAddr++ = *SourceAddr++;
  568. }
  569. return;
  570. }
  571. //===========================================================================
  572. // End of file.
  573. //===========================================================================