TC234_startup.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*****************************************************
  2. *
  3. * TC234_startup.c
  4. *
  5. * Description : Hello World in C, ANSI-style
  6. *
  7. */
  8. #include "TC234_startup.h"
  9. GPIO_config test_pin;
  10. static TimerVal tOn[3] = {700,700,700}; // container for TRANSISTOR ON TIMES
  11. static open_loop_driver open_loop; // container for open loop dirver
  12. static spi_config config_SPI; // container for SPI configuration
  13. static qspi_dma_rx_stucture TLI_dma_rec_structure; // container for raw_current values
  14. static qspi_dma_rx_stucture TLI_dma_rec_structure_copy; // copy of container for raw_current values
  15. static uint32 test_value[10];
  16. static uint32 test_rec_value[10]; // test array for DMA
  17. static sint32 position_raw; // raw position - from incremental encoder
  18. static sint16 pos_electrical; // electrical -position
  19. static uint32 torque_raw;
  20. static float32 torque;
  21. static uint32 linked_list_buffer[9]; // source address buffer for dma transmit buffer
  22. IfxGpt12_IncrEnc Inc_Enc_struct; // init struct for Incremental Encoder
  23. static float I_a,I_b,I_c ; // Phase Currents I_a I_b I_c
  24. static float32 I_imag,I_real; // I_d = I_real, I_q= I_imag
  25. //static float32 elAngleConst;
  26. static TFoc fo_controller; // current control container
  27. static float32 U_a,U_b,U_c; // T_on A,B,C proportional to UA UB UC
  28. static IfxVadc_Adc vadc_module;
  29. static IfxVadc_Adc_Channel Vadc_input_channel;
  30. IFX_INTERRUPT(vadcgroup0ISR, 0, CONTROL_ISR_PRIO);
  31. IFX_INTERRUPT(dma_tx_empty_isr, 0, ISR_PRIORITY_TX_EMPTY);
  32. IFX_INTERRUPT(qspi3_er_isr, 0, ISR_PRIORITY_QSPI3_ER);
  33. IFX_INTERRUPT(qspi3_rx_empty,0, ISR_PRIORITY_RX_EMPTY);
  34. IFX_INTERRUPT(qspi3_rx_interrupt,0, 101);
  35. IFX_INTERRUPT(ISR_Encoder_Gpt12, 0, INTERRUPT_ENCODER_GPT12);
  36. IFX_INTERRUPT(dma_test_isr, 0, DMA_TEST_PRIO);
  37. void ISR_Encoder_Gpt12(void)
  38. {
  39. IfxGpt12_IncrEnc_isrOnZeroPulse(&Inc_Enc_struct);
  40. __enable();
  41. }
  42. void dma_test_isr() {
  43. //set_GPIO(&test_pin) ;
  44. //clear_GPIO(&test_pin) ;
  45. IfxCpu_enableInterrupts();
  46. }
  47. void vadcgroup0ISR(void) {
  48. IfxCpu_disableInterrupts();
  49. //set_GPIO(&test_pin) ;
  50. TLI_dma_rec_structure_copy=TLI_dma_rec_structure; // copy Current raw data in working copy
  51. I_a=avarage_sensor_values(&(TLI_dma_rec_structure_copy.sensor1)); // get average current of last 50us
  52. I_b=avarage_sensor_values(&(TLI_dma_rec_structure_copy.sensor2)); // get average current of last 50us
  53. I_c=avarage_sensor_values(&(TLI_dma_rec_structure_copy.sensor3)); // get average current of last 50us
  54. trigger_vadc(&vadc_module,IfxVadc_ChannelId_0);
  55. T3Phase phase_currents= {I_a,I_b,I_c};
  56. position_raw=get_inc_encoder_position(&Inc_Enc_struct); // get raw position (mechanical) from incremental encoder
  57. pos_electrical = (sint16)get_electrical(position_raw); // convert mechanical position to electrical position
  58. if(Inc_Enc_struct.base.status.B.notSynchronised == TRUE ){
  59. // if incremental encoder has not passed the zero marker
  60. do_open_loop_step(&open_loop); //
  61. SpaceVectorModulation(open_loop.complex_ptr, open_loop.pwm_period_ticks, open_loop.tOn);
  62. set_three_channel_PWM(&Timer_driver,&PWM_driver, open_loop.tOn);
  63. CplxStdReal I_rot=fo_current_ctrl_step(&fo_controller, pos_electrical,&phase_currents); // dummy calculation
  64. I_imag=I_rot.imag;
  65. I_real=I_rot.real;
  66. }
  67. else {
  68. //FoC Step
  69. CplxStdReal I_rot=own_fo_current_ctrl_step(&fo_controller, pos_electrical,&phase_currents); // field oriented control
  70. SpaceVectorModulation(I_rot, open_loop.pwm_period_ticks, tOn);
  71. set_three_channel_PWM(&Timer_driver,&PWM_driver,tOn);
  72. I_imag=I_rot.imag;
  73. I_real=I_rot.real;
  74. }
  75. torque_raw= return_raw(&Vadc_input_channel);
  76. torque=convert_raw_to_voltage(torque_raw);
  77. U_a=open_loop.tOn[0];
  78. U_b=open_loop.tOn[1];
  79. U_c=open_loop.tOn[2];
  80. IfxCpu_enableInterrupts();
  81. }
  82. void dma_tx_empty_isr() {
  83. //set_GPIO(&test_pin);
  84. IfxCpu_enableInterrupts();
  85. //dma_Daisy_chain_Transaction_test();
  86. //clear_GPIO(&test_pin);
  87. //Qspi_SpiMaster_write_input_fifo(&(config_SPI.spiMasterChannel[0]));
  88. // IfxQspi_SpiMaster_isrTransmit(&config_SPI.spiMaster);
  89. //chHandle->base.txHandler(&chHandle->base);
  90. //handle->base.txCount++;
  91. }
  92. void qspi3_rx_empty() {
  93. //set_GPIO(&test_pin);
  94. IfxCpu_enableInterrupts();
  95. IfxQspi_SpiMaster_isrReceive(&config_SPI.spiMaster);
  96. // clear_GPIO(&test_pin);
  97. }
  98. void qspi3_er_isr() {
  99. IfxCpu_enableInterrupts();
  100. IfxQspi_SpiMaster_isrError(&config_SPI.spiMaster);
  101. }
  102. void qspi3_rx_interrupt() {
  103. Qspi_SpiMaster_write_input_fifo(&(config_SPI.spiMasterChannel[0]));
  104. uint8 max=Qspi_SpiMaster_get_rx_fill_level(&config_SPI.spiMasterChannel[0]);
  105. for(int i=0;i<max;i++) {
  106. test_value[i]= *(uint32*)Qspi_SpiMaster_get_rx_addr(&config_SPI.spiMasterChannel[0]) ;
  107. }
  108. if(test_value[0] >=0xA000) {
  109. set_GPIO(&test_pin);
  110. clear_GPIO(&test_pin);
  111. }
  112. }
  113. int main(void)
  114. {
  115. //test pin for debugging an measurement purposes
  116. test_pin.port=&MODULE_P02;
  117. test_pin.pin=0;
  118. LookUp_Init(); // init sin cos lookup table
  119. static TimerVal pwm_period_in_ticks;
  120. // static boolean LS_Status; //test for lockstepping - not implemented
  121. MYIfxScuCcu_init(&IfxScuCcu_defaultClockConfig); // Clock configuration - default clock config not working properly
  122. set_IO_start(); // set pins to default settings
  123. initGpio(&test_pin); // set testpin to Gernal purpose output
  124. set_GPIO(&test_pin); // testpin = 1
  125. clear_GPIO(&test_pin); //testpin = 0
  126. // Dummy values for SPI output
  127. // values dont matter
  128. config_SPI.spi3TxBuffer[0]=0x1111;
  129. config_SPI.spi3TxBuffer[1]=0x5555;
  130. config_SPI.spi3TxBuffer[2]=0x1111;
  131. config_SPI.spi3RxBuffer[0]=0x1234;
  132. config_SPI.spi3RxBuffer[1]=0x1234;
  133. config_SPI.spi3RxBuffer[2]=0x1234;
  134. // initialize UART
  135. init_uart_poll(UART_BAUDRATE);
  136. initPwm() ; // initiaize PWM wit GTM Module
  137. initSpi_TLI4970(&config_SPI); // initilize SPI interface
  138. pwm_period_in_ticks= get_pwm_period_ticks(&Timer_driver,20000.0);
  139. init_open_loop_driver (&open_loop,10.0,20, 1/20000.0, pwm_period_in_ticks); // initialize open Loop driver
  140. init_encoder(&Inc_Enc_struct); // initialize incremental encoder
  141. init_fo_controller(&fo_controller,20000); // init PI controller for I_d and I_q
  142. set_Iq_ref_current(&fo_controller,4.0); // set Iq in Ampere
  143. // const TimerVal period_in_ticks=PWM_driver.base.timer->period;
  144. // init vadc
  145. init_vadc_for_torque_sensor(&vadc_module,&Vadc_input_channel);
  146. spi_write_Dma_src_addr_buffer(linked_list_buffer,&config_SPI); // Fill linked list Buffer with values
  147. uint32 beacon_addr=Qspi_SpiMaster_get_bacon_entry(&config_SPI.spiMasterChannel[0]); // get BEACON-Register Addr
  148. uint32 data_enty_addr=Qspi_SpiMaster_get_data_entry(&config_SPI.spiMasterChannel[0]);// get DATAENTRY-Register Addr
  149. uint32 data_exit= Qspi_SpiMaster_get_rx_addr(&config_SPI.spiMasterChannel[0]); // get DATAEXIT-REGISTER ADDR
  150. init_dma_linked_list(IfxDma_ChannelId_5, linked_list_buffer,beacon_addr,data_enty_addr); // init dma transmit FSM
  151. init_dma_linked_list_read(IfxDma_ChannelId_6,&TLI_dma_rec_structure, data_exit); // init dma receive FSM
  152. //wait for current sensors
  153. polling_wait_ms(0.1);
  154. clear_GPIO(&test_pin);
  155. int iterator=0;
  156. // LS_Status=check_if_ls_enabled();
  157. // polling_wait_ms(0.1);
  158. // LS_Status=check_if_ls_occured();
  159. // polling_wait_ms(0.1);
  160. // inject_ls_error();
  161. // polling_wait_ms(0.1);
  162. // LS_Status=check_if_ls_occured();
  163. __enable(); // enable interrupts
  164. //Qspi_SpiMaster_write_input_fifo(&(config_SPI.spiMasterChannel[0]));
  165. init_dma_startup_linked_list(IfxDma_ChannelId_5); // start DMA FSM
  166. while(1) {
  167. //printf( "Hello world\n" );
  168. //Qspi_SpiMaster_write_input_fifo(&(config_SPI.spiMasterChannel[0]));
  169. if(iterator >=4) {
  170. // dma_daisy_chain_reset();
  171. iterator=0;
  172. }
  173. iterator++;
  174. _out_uart('O');
  175. polling_wait_ms(0.1);
  176. _out_uart('O');
  177. _out_uart('k');
  178. //__enable();
  179. }
  180. }
  181. void execute_dma_transmit(spi_config * config) {
  182. printf("Hello World\n");
  183. uint32 *test_addr = &test_value;
  184. // while (IfxQspi_SpiMaster_getStatus(&config->spiMasterChannel[0]) == SpiIf_Status_busy){
  185. // printf("Hello World\n");
  186. //}
  187. DMAIfxQspi_SpiMaster_exchange(&config->spiMasterChannel[0],test_rec_value,&config->spi3RxBuffer[0], 2,test_addr);
  188. }
  189. mystr test_komplex(mystr m) {
  190. mystr k;
  191. k.a= m.a*2;
  192. k.b= m.b*2;
  193. return k;
  194. }
  195. static void polling_wait_ms(float32 time)
  196. {
  197. uint32 stmCount = (uint32)(IfxScuCcu_getStmFrequency() * time);
  198. uint32 stmCountBegin = STM0_TIM0.U;
  199. while ((uint32)(STM0_TIM0.U - stmCountBegin) < stmCount)
  200. {
  201. __nop();
  202. }
  203. }