empty.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2021, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "ti_msp_dl_config.h"
  33. /* This results in approximately 0.5s of delay assuming 80MHz CPU_CLK */
  34. #define DELAY (40000000)
  35. unsigned int ACnt; // Variable: speed control period counter
  36. unsigned int BCnt; // Variable: speed control period tick
  37. unsigned int CCnt;
  38. unsigned int DCnt;
  39. unsigned int ECnt,FCnt,GCnt;
  40. unsigned int HALLACnt,HALLBCnt,HALLCCnt;
  41. unsigned int DAC_value;
  42. unsigned int SPEED_edgeCapture,TIPIN_edgeCapture;
  43. //#define DAC12_REF_VOLTAGE_mV (3300)
  44. //#define DAC12_OUTPUT_VOLTAGE_mV (1650)
  45. volatile bool gCheckADC;
  46. unsigned int i = 0;
  47. #define RESULT_SIZE (64)
  48. volatile uint16_t gAdcResult0[RESULT_SIZE]; //OPA0 OUTPUT
  49. volatile uint16_t gAdcResult1[RESULT_SIZE]; // PA25 pcb temp
  50. volatile uint16_t gAdcResult2[RESULT_SIZE];// PA24 motor temp
  51. volatile uint16_t gAdcResult3[RESULT_SIZE]; // PB24 zhuanba
  52. volatile uint16_t gAdcResult4[RESULT_SIZE];// PB20 bus current
  53. volatile uint16_t gAdcResult5[RESULT_SIZE]; // PA14 BAT VOLTAGE
  54. volatile uint16_t gAdcResult6[RESULT_SIZE]; //OPA1 OUTPUT
  55. volatile uint16_t gAdcResult7[RESULT_SIZE]; //PB19 A1_6 Liju
  56. void SysTick_Handler(void) // 20ms
  57. {
  58. ACnt++;
  59. // DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  60. }
  61. void SPEED_INST_IRQHandler(void)
  62. {
  63. switch (DL_TimerG_getPendingInterrupt(SPEED_INST))
  64. {
  65. case DL_TIMERG_IIDX_CC0_DN:
  66. SPEED_edgeCapture = DL_Timer_getCaptureCompareValue(SPEED_INST, DL_TIMER_CC_0_INDEX);
  67. break;
  68. case DL_TIMER_IIDX_ZERO:
  69. DCnt++;
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. void TAPIN_INST_IRQHandler(void)
  76. {
  77. switch (DL_TimerG_getPendingInterrupt(TAPIN_INST))
  78. {
  79. case DL_TIMERG_IIDX_CC0_DN:
  80. TIPIN_edgeCapture = DL_Timer_getCaptureCompareValue(TAPIN_INST, DL_TIMER_CC_0_INDEX);
  81. break;
  82. case DL_TIMER_IIDX_ZERO:
  83. ECnt++;
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. void GROUP1_IRQHandler(void)
  90. {
  91. /*
  92. * Get the pending interrupt for the GPIOA port and store for
  93. * comparisons later
  94. */
  95. uint32_t gpioA = DL_GPIO_getEnabledInterruptStatus(GPIOA,
  96. HALL_HALLA_PIN | HALL_HALLB_PIN | HALL_HALLC_PIN);
  97. /*
  98. * Bitwise AND the pending interrupt with the pin you want to check,
  99. * then check if it is equal to the pins. Clear the interrupt status.
  100. */
  101. if ((gpioA & HALL_HALLA_PIN) == HALL_HALLA_PIN)
  102. {
  103. // HALLACnt = DL_Timer_getCaptureCompareValue(TIMG12, DL_TIMER_CC_0_INDEX);
  104. // HALLACnt++;
  105. DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLA_PIN);
  106. }
  107. if ((gpioA & HALL_HALLB_PIN) == HALL_HALLB_PIN)
  108. {
  109. HALLBCnt++;
  110. DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLB_PIN);
  111. }
  112. if ((gpioA & HALL_HALLC_PIN) == HALL_HALLC_PIN)
  113. {
  114. HALLCCnt++;
  115. DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLC_PIN);
  116. }
  117. }
  118. /* Check for the last result to be loaded then change boolean */
  119. void ADC12_0_INST_IRQHandler(void)
  120. {
  121. switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) {
  122. case DL_ADC12_IIDX_MEM5_RESULT_LOADED:
  123. gCheckADC = true;
  124. DL_GPIO_setPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  125. // DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  126. delay_cycles(1000);
  127. gAdcResult0[i] =
  128. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0);
  129. gAdcResult1[i] =
  130. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_1);
  131. gAdcResult2[i] =
  132. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_2);
  133. gAdcResult3[i] =
  134. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_3);
  135. gAdcResult4[i] =
  136. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_4);
  137. gAdcResult5[i] =
  138. DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_5);
  139. gAdcResult6[i] =
  140. DL_ADC12_getMemResult(ADC12_1_INST, DL_ADC12_MEM_IDX_0);
  141. DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  142. gAdcResult7[i] =
  143. DL_ADC12_getMemResult(ADC12_1_INST, DL_ADC12_MEM_IDX_1);
  144. DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  145. i++;
  146. if (i >= RESULT_SIZE) {
  147. // __BKPT(0);
  148. i = 0;
  149. }
  150. else{
  151. ;/*No action required*/
  152. }
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. void MOTOR_PWM_INST_IRQHandler(void)
  159. {
  160. switch (DL_TimerA_getPendingInterrupt(MOTOR_PWM_INST))
  161. {
  162. case DL_TIMERG_IIDX_LOAD:
  163. GCnt++;
  164. break;
  165. case DL_TIMER_IIDX_ZERO:
  166. FCnt++;
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. int main(void)
  173. {
  174. SYSCFG_DL_init();
  175. // NVIC_EnableIRQ(TIMER_1MS_INST_INT_IRQN);
  176. NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
  177. NVIC_EnableIRQ(SPEED_INST_INT_IRQN);
  178. NVIC_EnableIRQ(TAPIN_INST_INT_IRQN); //
  179. NVIC_EnableIRQ(HALL_INT_IRQN); //HALL GPIO
  180. NVIC_EnableIRQ(MOTOR_PWM_INST_INT_IRQN); //MOTOR PWM
  181. DL_TimerA_startCounter(TIMA0);
  182. DL_TimerG_startCounter(SPEED_INST);
  183. DL_TimerG_startCounter(TAPIN_INST);
  184. DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  185. DL_GPIO_setPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  186. DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN);
  187. //
  188. // /* Set output voltage:
  189. // * DAC value (12-bits) = DesiredOutputVoltage x 4095
  190. // * -----------------------
  191. // * ReferenceVoltage
  192. // */
  193. // DAC_value = (DAC12_OUTPUT_VOLTAGE_mV * 4095) / DAC12_REF_VOLTAGE_mV;
  194. DL_DAC12_output12(DAC0, 2048);
  195. DL_DAC12_enable(DAC0);
  196. while (1)
  197. {
  198. CCnt++;
  199. delay_cycles(DELAY);
  200. DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 1875, DL_TIMER_CC_0_INDEX);
  201. DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 1250, DL_TIMER_CC_2_INDEX);
  202. DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 625, DL_TIMER_CC_3_INDEX);
  203. DL_TimerG_setCaptureCompareValue(LIGHT_PWM_INST, 100, DL_TIMER_CC_0_INDEX);
  204. DL_TimerG_setCaptureCompareValue(LIGHT_PWM_INST, 100, DL_TIMER_CC_1_INDEX);
  205. }
  206. }