/* * Copyright (c) 2021, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Texas Instruments Incorporated nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "ti_msp_dl_config.h" /* This results in approximately 0.5s of delay assuming 80MHz CPU_CLK */ #define DELAY (40000000) unsigned int ACnt; // Variable: speed control period counter unsigned int BCnt; // Variable: speed control period tick unsigned int CCnt; unsigned int DCnt; unsigned int ECnt,FCnt,GCnt; unsigned int HALLACnt,HALLBCnt,HALLCCnt; unsigned int DAC_value; unsigned int SPEED_edgeCapture,TIPIN_edgeCapture; //#define DAC12_REF_VOLTAGE_mV (3300) //#define DAC12_OUTPUT_VOLTAGE_mV (1650) volatile bool gCheckADC; unsigned int i = 0; #define RESULT_SIZE (64) volatile uint16_t gAdcResult0[RESULT_SIZE]; //OPA0 OUTPUT volatile uint16_t gAdcResult1[RESULT_SIZE]; // PA25 pcb temp volatile uint16_t gAdcResult2[RESULT_SIZE];// PA24 motor temp volatile uint16_t gAdcResult3[RESULT_SIZE]; // PB24 zhuanba volatile uint16_t gAdcResult4[RESULT_SIZE];// PB20 bus current volatile uint16_t gAdcResult5[RESULT_SIZE]; // PA14 BAT VOLTAGE volatile uint16_t gAdcResult6[RESULT_SIZE]; //OPA1 OUTPUT volatile uint16_t gAdcResult7[RESULT_SIZE]; //PB19 A1_6 Liju void SysTick_Handler(void) // 20ms { ACnt++; // DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); } void SPEED_INST_IRQHandler(void) { switch (DL_TimerG_getPendingInterrupt(SPEED_INST)) { case DL_TIMERG_IIDX_CC0_DN: SPEED_edgeCapture = DL_Timer_getCaptureCompareValue(SPEED_INST, DL_TIMER_CC_0_INDEX); break; case DL_TIMER_IIDX_ZERO: DCnt++; break; default: break; } } void TAPIN_INST_IRQHandler(void) { switch (DL_TimerG_getPendingInterrupt(TAPIN_INST)) { case DL_TIMERG_IIDX_CC0_DN: TIPIN_edgeCapture = DL_Timer_getCaptureCompareValue(TAPIN_INST, DL_TIMER_CC_0_INDEX); break; case DL_TIMER_IIDX_ZERO: ECnt++; break; default: break; } } void GROUP1_IRQHandler(void) { /* * Get the pending interrupt for the GPIOA port and store for * comparisons later */ uint32_t gpioA = DL_GPIO_getEnabledInterruptStatus(GPIOA, HALL_HALLA_PIN | HALL_HALLB_PIN | HALL_HALLC_PIN); /* * Bitwise AND the pending interrupt with the pin you want to check, * then check if it is equal to the pins. Clear the interrupt status. */ if ((gpioA & HALL_HALLA_PIN) == HALL_HALLA_PIN) { // HALLACnt = DL_Timer_getCaptureCompareValue(TIMG12, DL_TIMER_CC_0_INDEX); // HALLACnt++; DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLA_PIN); } if ((gpioA & HALL_HALLB_PIN) == HALL_HALLB_PIN) { HALLBCnt++; DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLB_PIN); } if ((gpioA & HALL_HALLC_PIN) == HALL_HALLC_PIN) { HALLCCnt++; DL_GPIO_clearInterruptStatus(GPIOA, HALL_HALLC_PIN); } } /* Check for the last result to be loaded then change boolean */ void ADC12_0_INST_IRQHandler(void) { switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) { case DL_ADC12_IIDX_MEM5_RESULT_LOADED: gCheckADC = true; DL_GPIO_setPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); // DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); delay_cycles(1000); gAdcResult0[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0); gAdcResult1[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_1); gAdcResult2[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_2); gAdcResult3[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_3); gAdcResult4[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_4); gAdcResult5[i] = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_5); gAdcResult6[i] = DL_ADC12_getMemResult(ADC12_1_INST, DL_ADC12_MEM_IDX_0); DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); gAdcResult7[i] = DL_ADC12_getMemResult(ADC12_1_INST, DL_ADC12_MEM_IDX_1); DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); i++; if (i >= RESULT_SIZE) { // __BKPT(0); i = 0; } else{ ;/*No action required*/ } break; default: break; } } void MOTOR_PWM_INST_IRQHandler(void) { switch (DL_TimerA_getPendingInterrupt(MOTOR_PWM_INST)) { case DL_TIMERG_IIDX_LOAD: GCnt++; break; case DL_TIMER_IIDX_ZERO: FCnt++; break; default: break; } } int main(void) { SYSCFG_DL_init(); // NVIC_EnableIRQ(TIMER_1MS_INST_INT_IRQN); NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN); NVIC_EnableIRQ(SPEED_INST_INT_IRQN); NVIC_EnableIRQ(TAPIN_INST_INT_IRQN); // NVIC_EnableIRQ(HALL_INT_IRQN); //HALL GPIO NVIC_EnableIRQ(MOTOR_PWM_INST_INT_IRQN); //MOTOR PWM DL_TimerA_startCounter(TIMA0); DL_TimerG_startCounter(SPEED_INST); DL_TimerG_startCounter(TAPIN_INST); DL_GPIO_clearPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); DL_GPIO_setPins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); DL_GPIO_togglePins(OUTPUT_CAN_STB_PORT, OUTPUT_CAN_STB_PIN); // // /* Set output voltage: // * DAC value (12-bits) = DesiredOutputVoltage x 4095 // * ----------------------- // * ReferenceVoltage // */ // DAC_value = (DAC12_OUTPUT_VOLTAGE_mV * 4095) / DAC12_REF_VOLTAGE_mV; DL_DAC12_output12(DAC0, 2048); DL_DAC12_enable(DAC0); while (1) { CCnt++; delay_cycles(DELAY); DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 1875, DL_TIMER_CC_0_INDEX); DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 1250, DL_TIMER_CC_2_INDEX); DL_TimerA_setCaptureCompareValue(MOTOR_PWM_INST, 625, DL_TIMER_CC_3_INDEX); DL_TimerG_setCaptureCompareValue(LIGHT_PWM_INST, 100, DL_TIMER_CC_0_INDEX); DL_TimerG_setCaptureCompareValue(LIGHT_PWM_INST, 100, DL_TIMER_CC_1_INDEX); } }