brazos-trapezoidal
main.c
Go to the documentation of this file.
1
15/*************************************************************************************************
16* *
17* Header files *
18* *
19*************************************************************************************************/
20
21#include "main.h"
22#include "includes.h"
23
24/*************************************************************************************************
25* *
26* Symbolic constants *
27* *
28*************************************************************************************************/
29
31#define SPI_CLK PIN_PC1
32#define SPI_SS PIN_PC0
33#define SPI_MISO PIN_PC3
34#define SPI_MOSI PIN_PC2
36#define SPI_CLK_CFG XBAR_CTL_CFG_1
37#define SPI_SS_CFG XBAR_CTL_CFG_1
38#define SPI_MISO_CFG XBAR_IN_SPI_CFG_IN_MISO_PC3
39#define SPI_MOSI_CFG XBAR_CTL_CFG_1
42#define UART0_TX PIN_PB4
43#define UART0_RX PIN_PB5
44#define UART1_TX PIN_PR4
45#define UART1_RX PIN_PR3
48#define TIM_CLK_DIV 1
49#define TIM1_PER 3020
52#define ADC_SAMPLE_PERIOD 7
55/*************************************************************************************************
56* *
57* Variables *
58* *
59*************************************************************************************************/
64volatile uint32_t TIM0_PER;
70volatile uint8_t Sync_Timer02;
71volatile uint8_t Saw_Up;
72uint16_t AdvDelay;
75uint32_t SPIData[5];
78volatile unsigned short TimerOut1s = 100;
79volatile uint8_t TimerOutFlashOperation = 0;
80volatile uint8_t count_10ms = 0;
81volatile uint8_t slot_10ms = false;
82volatile uint8_t Counter1KHz;
85volatile uint8_t DmaEnableRequest = 1;
88void *pTim0;
89void *pTim1;
90void *pTim2;
91void *pPwm0;
92void *pPwm1;
93void *pPwm3;
94void *pPwm2;
95void *pUart0;
96void *pUart1;
98#ifndef SIMU
99bool bTxDone = false;
100bool bRxDone = false;
101#endif
102
104volatile int32_t TestBldcSpd = 350;
105volatile int32_t TestBldcRamp = 50;
106volatile uint16_t TestBldcUpdtSpdTimer = 1000;
107volatile uint8_t TestBldcUpdtSpdReq = 0;
108unsigned char TestBldcOn = 0;
109unsigned char TestBldcOff;
111/*************************************************************************************************
112* *
113* Functions *
114* *
115*************************************************************************************************/
116
117void GpioXbarSetup(void);
118void AdcSetup(void);
119void DmaSetup(void);
120void TimerPwmSetup(uint8_t ADC_Advance, uint16_t ADC_Advance_Delay_Syslks);
121void TimerSync(uint8_t SawUp);
122void SpiInit(void);
123void UartSetup(void);
124void main_10ms(void);
125void main_count_1ms(void);
126
127#ifndef SIMU
128
134bool UART0Event(uint32_t event)
135{
136
137 /* TODO - make sure all events are handled safely
138 * UART_EVT_TX_COMPLETE
139UART_EVT_RX_COMPLETE
140UART_EVT_RX_ERROR
141UART_EVT_TX_ERROR
142UART_EVT_RX_OVER
143UART_EVT_TX_UNDER
144UART_EVT_RX_FE
145UART_EVT_RX_BRK
146UART_EVT_RX_PARITY_ERROR
147UART_EVT_TX_IDLE*/
148
149 if(event == UART_EVT_RX_FE)
150 {
152 }
153 else if (event == UART_EVT_TX_COMPLETE)
154 {
155 bTxDone = true;
156 }
157 else if (event == UART_EVT_RX_COMPLETE)
158 {
159 if(uart_getRxCount(pUart0) == SCI_HEADER_SIZE) {
161 }
162 else
163 {
164 hw_uart_write_rx_en(USART0, DISABLE);
165 hw_uart_write_rx_int_en(USART0, DISABLE);
166 bRxDone = true;
167 }
168 }
169 else {
170 while(1); /* Does not accept other event rather than the ones asserted in this function*/
171 }
172
173 return true;
174}
175
181bool UART1Event(uint32_t event)
182{
183 if (event & UART_EVT_TX_COMPLETE){}
184 /*bTxDone = true;*/
185 else if (event & UART_EVT_RX_COMPLETE){}
186 /*bRxDone = true;*/
187 else
188 while(1);
189
190 return true;
191}
192
193#endif /*#ifndef SIMU*/
194
200void SysTickCallBack(void) {
201#ifndef SIMU
202 /* CALL RATE AT 250 US*/
203 hw_systick_load(SysTick, SYSTICK_PER_CNT);
204#endif
205}
206
212void TIM0Event(uint32_t event){
213
214#ifndef SIMU
215 if (event == TIM_EVT_UNDER) {
216 if(Sync_Timer02) {
217 Sync_Timer02 = 0;
218 hw_tim_write_under_int_en(TIM0, DISABLE);
219 hw_tim_clear_int_sts_reg(TIM0);
221 hw_tim_write_over_int_en(TIM0, ENABLE);
222 }
223
224 TIM0->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
225 TIM0->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
226 TIM0->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
227 TIM0->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
228 }
229
230 if(event == TIM_EVT_OVER)
231 {
232 /* Reenabling DMA because at the point of sampling */
233 if(DmaEnableRequest) {
235
236 hw_tim_write_over_int_en(TIM2, DISABLE);
237 TIM2->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
238 TIM2->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
239 TIM2->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
240 TIM2->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
241 hw_tim_write_over_int_en(TIM2, ENABLE);
242
243 /* DEBUGGING
244 if(st_g.u2_pwm_duty == 1000)
245 st_g.u2_pwm_duty = 1200;
246 else
247 st_g.u2_pwm_duty = 1000;
248
249 PWM_LOADPWM[0](st_g.u2_pwm_duty,st_g.u2_pwm_duty,st_g.u2_pwm_duty);
250 */
251
252 hw_adc_clear_int_sts_data(ADC);
253 hw_adc_clear_int_sts_over(ADC);
254 hw_adc_clear_int_sts(ADC);
255 ADC->INT_STS_b.DATA = 0;
256 ADC->INT_STS_b.OVER = 0;
257
258 hw_dma_write_int_en_done_ch1(DMA, ENABLE); /* the request is made in a time basis of 250us */
259 }
260
261 /*
262 * CLEAR FLAGS OF OVER INTERRUPT
263 */
264 hw_tim_write_over_int_en(TIM0, DISABLE);
265 TIM0->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
266 TIM0->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
267 TIM0->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
268 TIM0->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
269 hw_tim_write_over_int_en(TIM0, ENABLE);
270
271 /*
272 * REENABLE ADC
273 */
274#if(0)
275 hw_adc_clr_seq_reset(ADC); // Reset sequence
276 hw_adc_write_count_enable(ADC, ENABLE); /* Count Sampling*/
277 hw_adc_write_continuous_enable(ADC, ENABLE); /* Count Sampling*/
278
279 hw_adc_clr_seq_reset(ADC); // Reset sequence
280 hw_adc_write_cnt_limit(ADC, (NUM_ADC_SAMPLES-1));
281 hw_adc_write_count_enable(ADC, ENABLE);
282#endif
283
284 }
285#endif
286}
287
293void TIM2Event(uint32_t event){
294#ifndef SIMU
295 TIM2->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
296 TIM2->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
297 TIM2->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
298 TIM2->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
299#endif
300}
301
307void PWM0Event(uint32_t event){
308#ifndef SIMU
309 if (event == PWM_EVT_EMPTY) {
310 }
311#endif
312}
313
319void PWM3Event(uint32_t event){
320}
321
328void PWM1Event(uint32_t event){
329}
330
336void PWM2Event(uint32_t event){
337}
338
344void DMA_Event(uint32_t event){
345#ifndef SIMU
346 unsigned short int da, db, dc;
347
348 /* CH1 EVENT: Data ready in the RAM */
349 if(event == DMA_EVT_CH1_DONE) {
350
351 hw_dma_write_int_en_done_ch1(DMA, DISABLE); /* Dma will be only reenabled after elapsing of 250us and over int*/
352
353 /* DEBUGGING */
354 /*gpio_set(SPI_SS)*/;
355
356 /* ADC Data ready */
358
359 if(Counter1KHz) {
360 Counter1KHz--;
361
362 if(Counter1KHz == 3) {
364 }
365 else if(Counter1KHz == 1) {
366 if(TestBldcOff){
367 TestBldcOff = 0;
368 TrapSless__SetSpd(0, 0);
369 }
370
371 if(TestBldcOn)
372 {
373 TestBldcOn = 0;
374 if((TestBldcSpd < IQ_TO_RPM(CAL_GET_REF_SPDMIN_RPM())) &&
375 (TrapSless__GetVars()->State < STATE_TRAP_CLOSEDLOOP)) {
376 TrapSless__SetSpd(IQ_TO_RPM(CAL_GET_REF_SPDMIN_RPM()), TestBldcRamp);
377 TestBldcUpdtSpdTimer = 3000; //After 3s update speed
379 }
380 else {
382 }
383 }
384
389 if(TrapSless__GetVars()->SpdTarget != 0)
391 }
392 }
393
395 }
396 else if(Counter1KHz == 0) {
397 }
398 }
399
400 if(!Counter1KHz) {
401 Counter1KHz = 16;
402 }
403
404 /* DEBUGGING */
405 /*
406 // Send data using SPI
407 SPIData[0] = ADC_Samples[ADC_IA];
408 SPIData[1] = ADC_Samples[ADC_IB];
409 SPIData[2] = ADC_Samples[ADC_IC];
410 SPIData[3] = ADC_Samples[ADC_VA];
411 SPIData[4] = ADC_Samples[ADC_VBUS];
412
413 // Signaling Chip Select that data will be sent
414 gpio_clear(SPI_SS);
415 spi_send (&SPIData, (sizeof(SPIData)-1), false);
416 */
417
418 /* DEBUGGING */
419 /*gpio_clear(SPI_SS);*/
420
421 /* AFTER LONG TIME SPENT IN DMA INT MAKE SURE TO CLEAR PREVIOUS REQUESTS OF TIM0 INTERRUPT*/
422 TIM0->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
423 TIM0->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
424 TIM0->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
425 TIM0->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
426
427 /* AFTER LONG TIME SPENT IN DMA INT MAKE SURE TO CLEAR PREVIOUS REQUESTS OF TIM2 INTERRUPT*/
428 TIM2->INT_STS_b.UNDER = CLEAR; /* You need to write 1 to clear */
429 TIM2->INT_STS_b.UNDER_MISSED = CLEAR; /* You need to write 1 to clear */
430 TIM2->INT_STS_b.OVER = CLEAR; /* You need to write 1 to clear */
431 TIM2->INT_STS_b.OVER_MISSED = CLEAR; /* You need to write 1 to clear */
432
434
435 /*
436 * REENABLE TIM0 OVER INTERRUPT */
437 hw_tim_write_over_int_en(TIM0, ENABLE);
438
439 hw_adc_clr_seq_reset(ADC); /* Reset adc sequence*/
440 hw_adc_write_continuous_enable(ADC, ENABLE); /* ENABLE DISABLE */
441 }
442#endif
443}
444
445
451void ErrorEvent(uint32_t event){
452}
453
459void WDogEvent(uint32_t event){
460 wdog_control(WDOG_CTL_RST_EN, ENABLE);
461 while (1);
462}
463
468void main_step(void) {
469 /* ADC Data ready */
470
471#ifdef SIMU
472 ADC_Samples[ADC_VBUS] = SRMCATOD__GETDCBUSVOLTAGE();
473 ADC_Samples[ADC_IA] = SRMCATOD__GETADC1();
474 ADC_Samples[ADC_IB] = SRMCATOD__GETADC2();
475 ADC_Samples[ADC_IC] = 0;
476 ADC_Samples[ADC_VA] = SRMCATOD__GETPOLE1();
477 ADC_Samples[ADC_VB] = SRMCATOD__GETPOLE2();
478 ADC_Samples[ADC_VC] = SRMCATOD__GETPOLE3();
479#endif
480
482}
483
484
489void main_init(void) {
490 /* Initialize CMU, XBAR, GPIO, timer, MSPI peripherals */
491#ifndef SIMU
492 xbar_initialize();
493 gpio_initialize(NULL);
494 /*spi_initialize(NULL);*/
495
496 //--------------------------------------------------------------------------
497 // --- Initialize program
502 IntegrationCalibration__Initialize();
503#endif
504
507#ifndef SIMU
508 LSI__DigitalOutputLEDs(LED_1,O_OFF);
509 LSI__DigitalOutputLEDs(LED_3,O_OFF);
510#endif
511
512#ifndef SIMU
513 DmaEnableRequest = 1;
514 AdcSetup();
515 DmaSetup();
516#endif
517
518#ifndef SIMU
519 TIM0_PER = ((uint32_t)20000000)/((uint32_t)16000);
520 AdvDelay = TIM0_PER + 83;
522 TimerPwmSetup(true, AdvDelay);
523 hw_adc_clr_seq_reset(ADC);
524#endif
525
526#if(0)
527 hw_adc_write_count_enable(ADC, ENABLE); /* Count Sampling*/
528 SpiInit();
529#endif
530
531#ifndef SIMU
532 UartSetup();
537 wdog_initialize(WDogEvent);
538 wdog_control(WDOG_CTL_UNLOCK, 0);
539 wdog_control(WDOG_CTL_LOAD, (64000000/6));
540 wdog_control(WDOG_CTL_RELOAD, 0);
541 wdog_control(WDOG_CTL_EN, ENABLE);
542 wdog_control(WDOG_CTL_RST_EN, ENABLE);
543 wdog_control(WDOG_CTL_LOCK, 0);
544 wdog_service();
545#endif
546
548 tick_initialize(NULL);
549 hw_tick_write_clk_src(TICK, TICK_CFG_CLK_SRC_HSOSC);
550 hw_tick_write_clk_div(TICK, TICK_CFG_CLK_DIV_MUL1);
551}
552
558void main_10ms(void)
559{
560 wdog_service();
561 if(TimerOut1s) {
562 TimerOut1s--;
563 }
564 else {
565 #ifndef SIMU
566 LSI__ToggleLEDs(LED_1);
567 LSI__ToggleLEDs(LED_3);
568 #endif
569 TimerOut1s = 100;
570 }
571
572 if(IntegrationCalibration__GetRequestFlashOperation())
573 {
574 if(TrapSless__GetVars()->State == STATE_TRAP_IDLE || TrapSless__GetVars()->State == STATE_TRAP_BOOT)
575 {
577 }
578 else if(!TimerOutFlashOperation)
579 {
582 }
583
586 else
587 {
588 IntegrationCalibration__Handler();
589
590 if((IntegrationCalibration__GetRequestFlashOperation() == INTCALIB_FLASH_OPERATION_POST_WRITE))
591 {
593 {
594 IntegrationCalibration__ResetFlashOperation();
595 //Reset micro after write parameters
596 while(1){};
597 }
598 }
599 }
600 }
601
603 if(bRxDone)
604 {
605 bRxDone = false;
607 }
610
611
612 slot_10ms = false;
613}
614
619{
620 /**************************************************************************************************
621 * Initialize the GPIO and CrossBar
622 * Debug Setup for the GPIO and CrossBar
623 * Continuous sampling with Override Enabled
624 *************************************************************************************************/
625
626#ifdef TIMER0
627 /* TIN0OUT on PA0 */
628 xbar_control(TIMER0, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_1);
629 xbar_control_cfg_pin_pull_down(TIMER0, XBAR_CTL_NO_PULL_DOWN);
630#endif
631#ifdef TIMER1
632 /* TIM1OUT on PA3 */
633 xbar_control(TIMER1, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_1);
634 xbar_control_cfg_pin_pull_down(TIMER1, XBAR_CTL_NO_PULL_DOWN);
635#endif
636#ifdef EXT_CLK
637 /* EXTCLK OUT on PC0 */
638 xbar_control(EXT_CLK, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_1);
639 xbar_control_cfg_pin_pull_down(EXT_CLK, XBAR_CTL_NO_PULL_DOWN);
640#endif
641
642 /* Initialize LED output */
643#ifdef LED0
644 xbar_control_cfg_pin_pull_up(LED0, XBAR_CTL_PULL_UP);
645 xbar_control_cfg_out_put(LED0, XBAR_CTL_CFG_GPIO);
646#endif
647#ifdef SWITCH0
648 xbar_control_cfg_pin_pull_up(SWITCH0, XBAR_CTL_PULL_UP);
649 gpio_control_cfg_dir(SWITCH0, INPUT);
650 gpio_control_int_pol(SWITCH0, RISING_EDGE);
651 gpio_control_int_type(SWITCH0, EDGE);
652 gpio_control_int_en(SWITCH0, DISABLE);
653#endif
654#ifdef SWITCH2
655 xbar_control_cfg_pin_pull_up(SWITCH2, XBAR_CTL_PULL_UP);
656 gpio_control_cfg_dir(SWITCH2, INPUT);
657 gpio_control_int_pol(SWITCH2, RISING_EDGE);
658 gpio_control_int_type(SWITCH2, EDGE);
659 gpio_control_int_en(SWITCH2, DISABLE);
660#endif
661
662 /* DEBUGGING */
663 /*xbar_control(SPI_CLK, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_4); //only for debugging pwm0L at pin of spiclk*/
664
665 /* DEBUGGING CAUSING PB1 PB1 brazos.4 led1 to reflect pwm1h (w) (config 2)*/
666 //xbar_control(PIN_PB1, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_2);
667}
668
672int main(void) {
673
674 main_init();
676
677 while(1)
678 {
679 if(slot_10ms)
680 main_10ms();
681 };
682
683 return 0;
684}
685
686
692{
693 while (1);
694}
695
700void AdcSetup(void) {
701#ifndef SIMU
702 uint32_t nControl, nControl1;
703 int h;
704//------------------------------ S12AD0 Configuration ------------------------------
705
706 /* ADC Clock selection and initialization */
707 cmu_control_sel_adc_clk(CMU_ADCCLK_CFG_SEL_HSOSC, CMU_ADCCLK_CFG_DIV_BY_1);
708
709#if ADC_SMPL_GPIO
710 adc_initialize(ADCEvent); // ADCEvent
711#else
712 adc_initialize(NULL); // ADCEvent
713#endif
714
715 for (h=0; h<( NUM_ADC_SAMPLES- 1) ; h++)
716 {
717 ADC_CTL_ptr[h] = 0x00000006;
729 }
730
731 ADC_CTL_ptr[NUM_ADC_SAMPLES-1] = 0x0000004D;
743 /* Set ADC XBAR register */
744 xbar_control_cfg_pin(ADC_CH0, XBAR_CTL_ANA);
745 xbar_control_cfg_pin_pull_down(ADC_CH0, XBAR_CTL_NO_PULL_DOWN);
746 xbar_control_cfg_pin(ADC_CH1, XBAR_CTL_ANA);
747 xbar_control_cfg_pin_pull_down(ADC_CH1, XBAR_CTL_NO_PULL_DOWN);
748 xbar_control_cfg_pin(ADC_CH2, XBAR_CTL_ANA);
749 xbar_control_cfg_pin_pull_down(ADC_CH2, XBAR_CTL_NO_PULL_DOWN);
750 xbar_control_cfg_pin(ADC_CH3, XBAR_CTL_ANA);
751 xbar_control_cfg_pin_pull_down(ADC_CH3, XBAR_CTL_NO_PULL_DOWN);
752 xbar_control_cfg_pin(ADC_CH4, XBAR_CTL_ANA);
753 xbar_control_cfg_pin_pull_down(ADC_CH4, XBAR_CTL_NO_PULL_DOWN);
754 xbar_control_cfg_pin(ADC_CH5, XBAR_CTL_ANA);
755 xbar_control_cfg_pin_pull_down(ADC_CH5, XBAR_CTL_NO_PULL_DOWN);
756 xbar_control_cfg_pin(ADC_CH6, XBAR_CTL_ANA);
757 xbar_control_cfg_pin_pull_down(ADC_CH6, XBAR_CTL_NO_PULL_DOWN);
758 xbar_control_cfg_pin(ADC_CH7, XBAR_CTL_ANA);
759 xbar_control_cfg_pin_pull_down(ADC_CH7, XBAR_CTL_NO_PULL_DOWN);
761 /* Set ADC CFG register */
762 hw_adc_write_reg_ext(ADC, ADC_REG_CAP_INTERNAL);
763 hw_adc_write_single_range_sel(ADC, ADC_CFG_SINGLE_RANGE_SEL_2xVREF);
764 hw_adc_write_int_ref_sel(ADC, ADC_INT_REF_VDDIO_BY_2);
765 hw_adc_write_ref_sel(ADC, ADC_REF_INTERNAL);
766 hw_adc_write_gain(ADC, ADC_CFG_GAIN_MUL1);
767 hw_adc_write_sample_clk(ADC, ADC_SAMPLE_PERIOD);
769 hw_adc_write_single_ch0(ADC, CH0_ENABLE);
770 hw_adc_write_single_ch1(ADC, CH1_ENABLE);
771 hw_adc_write_single_ch2(ADC, CH2_ENABLE);
772 hw_adc_write_single_ch3(ADC, CH3_ENABLE);
773 hw_adc_write_single_ch4(ADC, CH4_ENABLE);
774 hw_adc_write_single_ch5(ADC, CH5_ENABLE);
775 hw_adc_write_single_ch6(ADC, CH6_ENABLE);
776 hw_adc_write_single_ch7(ADC, CH7_ENABLE);
779 /* Set ADC CTL register */
780 hw_adc_write_over_enable(ADC, ENABLE);
781 hw_adc_write_continuous_enable(ADC, DISABLE);
782 hw_adc_write_count_enable(ADC, DISABLE);
784 hw_adc_write_cnt_limit(ADC, 0);
785 hw_adc_write_int_en_data(ADC, ENABLE);
787 /* ADC Triggers */
788 hw_adc_write_adc_trig_sel(ADC, ADC_CFG_TRIGGER_SEL_PWM0);
789 xbar_control_cfg_adc(XBAR_IN_ADC_CFG_TRIG_TYP_TIM, XBAR_IN_ADC_CFG_PIN_SEL_PA6, XBAR_IN_ADC_CFG_TIM_SEL_PWM0);
790 hw_adc_write_edge_trigger(ADC, ADC_CTL_EDGE_TRIG_RISE);
791 hw_adc_write_ahw_trigger(ADC, ENABLE);
792 hw_adc_write_continuous_enable(ADC, ENABLE);
794 NVIC_SetPriority(ADC_IRQn, ATOD_IRQ_PRIO);
795 hw_adc_write_ahw_trigger(ADC, ENABLE);
796 hw_adc_write_over_enable(ADC, DISABLE);
797 hw_adc_write_int_en_data(ADC, DISABLE);
798#endif
799}
800
801
806void DmaSetup(void) {
807#ifndef SIMU
808 uint32_t nControl, nControl1;
809 dma_initialize(DMA_Event, ErrorEvent);
810
811 NVIC_ClearPendingIRQ ( DMA_DONE_IRQn ) ;
812 NVIC_SetPriority(DMA_DONE_IRQn, 2);
813
814 /* ADC Configuration Control */
815 nControl = DMA_DESC_CFG_DST_INC_NONE |
816 DMA_DESC_CFG_DST_SIZE_WORD |
817 DMA_DESC_CFG_SRC_INC_WORD |
818 DMA_DESC_CFG_SRC_SIZE_WORD |
819 (0 << DMA_DESC_CFG_ARB_RATE_POW2_POS) |
820 ((NUM_ADC_SAMPLES-1) << DMA_DESC_CFG_COUNT_MINUS_1_POS) |
821 DMA_DESC_CFG_MODE_BASIC;
822 dma_setDescriptor( ePriDesc, eDMAChannel0, ( void *)(&ADC_CTL_ptr[NUM_ADC_SAMPLES-1]), (void *)&ADC->CTL, nControl);
823
824 /* ADC DATA Transfer */
825 nControl1 = DMA_DESC_CFG_DST_INC_HALFWORD |
826 DMA_DESC_CFG_DST_SIZE_HALFWORD |
827 DMA_DESC_CFG_SRC_INC_NONE |
828 DMA_DESC_CFG_SRC_SIZE_HALFWORD |
829 (0 << DMA_DESC_CFG_ARB_RATE_POW2_POS) |
830 ((NUM_ADC_SAMPLES-1) << DMA_DESC_CFG_COUNT_MINUS_1_POS) |
831 DMA_DESC_CFG_MODE_BASIC;
832 dma_setDescriptor( ePriDesc, eDMAChannel1, ( void *)&ADC->DATA, ( void *)(&ADC_Samples[NUM_ADC_SAMPLES-1]),nControl1);
833 hw_dma_write_int_en_done_ch1(DMA, ENABLE);
834
835 hw_dma_write_priority_set_ch0(DMA, ENABLE);
836 hw_dma_write_priority_set_ch1(DMA, DISABLE);
837
838 xbar_control_cfg_dma( eDMAChannel0, XBAR_IN_DMA_CFG_CH0_TRIG_TYP_REQ, XBAR_IN_DMA_CFG_CH0_TRIG_SEL_ADC_SAMP);
839 xbar_control_cfg_dma( eDMAChannel1, XBAR_IN_DMA_CFG_CH1_TRIG_TYP_REQ, XBAR_IN_DMA_CFG_CH1_TRIG_SEL_ADC_SAMP);
840 hw_dma_write_enable_set_ch0(DMA, ENABLE);
841 hw_dma_write_enable_set_ch1(DMA, ENABLE);
842
843 dma_control(DMA_CTL_ENABLE, ENABLE);
844 dma_control(DMA_CTL_AUTO_EN, ENABLE);
845#endif
846}
847
848
856void TimerPwmSetup(uint8_t ADC_Advance, uint16_t ADC_Advance_Delay_Syslks)
857{
858#ifndef SIMU
859 unsigned short int tim_threshold;
860
861 tim_initialize( eTim0, TIM0Event, &pTim0);
862 tim_initialize( eTim2, TIM2Event, &pTim2);
863 pwm_initialize( ePwm0, PWM0Event, &pPwm0);
864 pwm_initialize( ePwm1, PWM1Event, &pPwm1);
865 pwm_initialize( ePwm2, PWM2Event, &pPwm2);
866 pwm_initialize( ePwm3, PWM3Event, &pPwm3);
867
868 /* Configure TIM0 PWM Control */
869 NVIC_ClearPendingIRQ ( TIM_IRQ ) ;
870 NVIC_SetPriority(TIM_IRQ, TIM0_IRQ_PRIO);
871
872 hw_tim_write_per_reg(TIM0, TIM0_PER);
873 hw_tim_write_over_int_en(TIM0, ENABLE);
874 hw_tim_write_under_int_en(TIM0, DISABLE);
875 hw_tim_write_per_empty_int_en(TIM0, DISABLE);
876 hw_tim_write_per_under_int_en(TIM0, DISABLE);
877
878 /* Configure TIM2 ADC / Trigger Control */
879 hw_tim_write_per_reg(TIM2, TIM0_PER*2-1);
880 hw_tim_write_over_int_en(TIM2, DISABLE);
881 hw_tim_write_under_int_en(TIM2, DISABLE);
882 hw_tim_write_per_empty_int_en(TIM2, DISABLE);
883 hw_tim_write_per_under_int_en(TIM2, DISABLE);
884
885 /* TIM Scale for debug purposes */
886 hw_tim_write_clk_src(TIM0, TIM_CFG_CLK_SEL_SYS_CLK);
887 hw_tim_write_clk_src(TIM2, TIM_CFG_CLK_SEL_SYS_CLK);
888 switch(TIM_CLK_DIV) {
889 case 1:
890 hw_tim_write_clk_div(TIM0, TIM_CFG_CLK_DIV_MUL1 );
891 hw_tim_write_clk_div(TIM2, TIM_CFG_CLK_DIV_MUL1 );
892 break;
893 case 2:
894 hw_tim_write_clk_div(TIM0, TIM_CFG_CLK_DIV_DIV2);
895 hw_tim_write_clk_div(TIM2, TIM_CFG_CLK_DIV_DIV2);
896 break;
897 case 4:
898 hw_tim_write_clk_div(TIM0, TIM_CFG_CLK_DIV_DIV4);
899 hw_tim_write_clk_div(TIM2, TIM_CFG_CLK_DIV_DIV4);
900 break;
901 case 8:
902 hw_tim_write_clk_div(TIM0, TIM_CFG_CLK_DIV_DIV8);
903 hw_tim_write_clk_div(TIM2, TIM_CFG_CLK_DIV_DIV8);
904 break;
905 case 16:
906 hw_tim_write_clk_div(TIM0, TIM_CFG_CLK_DIV_DIV16);
907 hw_tim_write_clk_div(TIM2, TIM_CFG_CLK_DIV_DIV16);
908 break;
909 }
910 hw_tim_write_mode(TIM0, TIM_CFG_MOD_DIV_TRI);
911
912 if (ADC_Advance) {
913 hw_tim_write_mode(TIM2, TIM_CFG_MOD_DIV_SAW_DOWN);
914 Saw_Up = 0;
915 }
916 else
917 {
918 hw_tim_write_mode(TIM2, TIM_CFG_MOD_DIV_SAW_UP);
919 Saw_Up = 1;
920 }
921
922
923 /* Initiate Timer Sync */
924 hw_tim_clear_int_sts_reg(TIM0);
925 hw_tim_write_under_int_en(TIM0, ENABLE); // INT used to sync SAW UP/DWN onto the PU cycle
926
927 Sync_Timer02 = 1;
928
929 /* Configure Pwm output */
931
932#if(0)
933 //pwm1 debug
934 xbar_control(PWMDBG_HI, XBAR_CTL_PULL_DOWN_CFG, XBAR_CTL_NO_PULL_DOWN);
935 xbar_control(PWMDBG_HI, XBAR_CTL_PULL_UP_CFG, XBAR_CTL_NO_PULL_UP);
936 xbar_control(PWMDBG_HI, XBAR_CTL_OUT_DRIVE_CFG, XBAR_CTL_PUSH_PULL);
937 xbar_control(PWMDBG_HI, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_3);
938 xbar_control(PWMDBG_LO, XBAR_CTL_PULL_DOWN_CFG, XBAR_CTL_NO_PULL_DOWN);
939 xbar_control(PWMDBG_LO, XBAR_CTL_PULL_UP_CFG, XBAR_CTL_NO_PULL_UP);
940 xbar_control(PWMDBG_LO, XBAR_CTL_OUT_DRIVE_CFG, XBAR_CTL_PUSH_PULL);
941 xbar_control(PWMDBG_LO, XBAR_CTL_OUT_CFG, XBAR_CTL_CFG_4);
942#endif
943 /* pwm_deadtime = (unsigned short int)((dead_time_ns * SR_MTU_FREQUENCY)/BILLION);
944 pwm_deadtime = (unsigned short int)((500 * 40000000)/1000000000);*/
945 PWMDriver__ConfigPwm(40, ADC_Advance_Delay_Syslks);
946#endif
947}
948
955void TimerSync(uint8_t SawUp) {
956#ifndef SIMU
957 if(SawUp)
958 {
959 // TIM0 CNT ADDRESS 4110_0000
960 asm ( " mov r4, #4 "); // 0x4
961 asm ( " lsl r4, r4, #8 "); //0x400
962 asm ( " add r4, #17 "); // 0x400 or 0x011
963 asm ( " lsl r3, r4, #20 ");
964 // TIM2 CNT ADDRESS 4130_0000
965 asm ( " add r4, #2 "); // 0x411 add 0x002
966 asm ( " lsl r4, r4, #20 ");
967 // CNT Reset Data
968 asm ( " mov r5, #4 " ); // 0x5
969 asm ( " mov r6, #10 "); // 0x5
970 // Load TIM0
971 asm ( " str r5, [r3, #0] " ); //Set TIM0 CNT = 0x5
972 // Load TIM2
973 asm ( " str r6, [r4, #0] " ); //Set TIM2 CNT = 0x5
974 }
975 else
976 {
977 // TIM0 CNT ADDRESS 4110_0000
978 asm ( " mov r4, #4 "); // 0x4
979 asm ( " lsl r4, r4, #8 "); //0x400
980 asm ( " add r4, #17 "); // 0x400 or 0x011
981 asm ( " lsl r3, r4, #20 ");
982 // TIM2 CNT ADDRESS 4130_0000
983 asm ( " add r4, #2 "); // 0x411 add 0x002
984 asm ( " lsl r4, r4, #20 ");
985 // CNT Reset Data
986 asm ( " mov r5, #4 " ); // 0x5 //CLK 6/17/2021: fix the Sync for Saw Down
987 asm ( " mov r6, #2 " ); // 0x5
988 // Load TIM2
989 asm ( " str r6, [r4, #0] " ); //Set TIM2 CNT = 0x5
990 // Load TIM0
991 asm ( " str r5, [r3, #0] " ); //Set TIM0 CNT = 0x5
992 }
993#endif
994}
995
1000void SpiInit(void) {
1001#ifndef SIMU
1002 /***************************************************************/
1003 /* SPI CONFIG */
1004 /***************************************************************/
1005
1006 // Initialize Master SPI Bus
1007 // SCLK
1008 xbar_control(SPI_CLK, XBAR_CTL_OUT_CFG, SPI_CLK_CFG);
1009 //MOSI
1010 xbar_control(SPI_MOSI, XBAR_CTL_OUT_CFG, SPI_MOSI_CFG);
1011 // SS0 -
1012 xbar_control(SPI_SS, XBAR_CTL_OUT_CFG, SPI_SS_CFG);
1013 // MISO
1014 xbar_control_cfg_spi(XBAR_IN_SPI_CFG_IN_MISO_DISABLE);
1015
1016 xbar_control_cfg_pin_pull_down(SPI_CLK|SPI_MOSI|SPI_SS, XBAR_CTL_NO_PULL_UP);
1017 xbar_control_cfg_pin_pull_up(SPI_CLK|SPI_MOSI|SPI_SS, XBAR_CTL_PULL_UP);
1018 xbar_control_cfg_out_put(SPI_CLK|SPI_MOSI|SPI_SS, XBAR_CTL_OPEN_DRAIN);
1019
1020 GPIO__CONFIG_OUTPUT_PUSHPULL_NO_PULLUP(SPI_SS);
1021
1022// completely initialize the SPI module
1023 hw_spi_write_ss_int_en(SPI, DISABLE);
1024 hw_spi_write_tx_int_en(SPI, DISABLE);
1025 hw_spi_write_rx_int_en(SPI, DISABLE);
1026
1027 hw_spi_write_clk_phase(SPI, SPI_CFG_CPHA_0); // Standard CPHA sampling on rising edge
1028 hw_spi_write_clk_polarity(SPI, SPI_CFG_CPOL_IDLE_LOW); // Standard SPI with clock default of low.
1029 hw_spi_write_clk_div(SPI, SPI_CFG_CLK_DIV_BY_8);// 1:16 clock for high speed.
1030 hw_spi_write_clk_src(SPI, SPI_CFG_CLK_SRC_HSOSC); // uses internal High Speed clock.
1031 hw_spi_write_dir(SPI, SPI_CFG_DIR_MSB_FIRST); // Standard SPI shifting MSB out first.
1032 hw_spi_write_fsync(SPI, SPI_CFG_FSYNC_SEL_0); // Slave select for Slave number Zero
1033 hw_spi_write_mode(SPI, SPI_CFG_MODE_MASTER); // setup as master
1034 hw_spi_write_word_length(SPI, SPI_CFG_WL_8_BIT); // Standard SPI width of 8 bits.
1035
1036#endif
1037}
1038
1043void UartSetup(void)
1044{
1045#ifndef SIMU
1046 tim_initialize(eTim1, NULL, &pTim1);
1047 uart_initialize(eUsart1, UART1Event, &pUart1);
1049
1050 // Configure TIM1 for UART0 and UART1
1051 hw_tim_write_clk_src(TIM1, TIM_CFG_CLK_SEL_SYS_CLK); //40 MHz
1052 hw_tim_write_clk_div(TIM1, TIM_CFG_CLK_DIV_MUL1); //x1
1053 hw_tim_write_per_reg(TIM1, TIM1_PER); //Periodo
1054
1055 // Configure UART1
1056 hw_uart_write_baud_gen_tim(USART1, UART_CFG_BRG_TIM1);
1057 uart_control(pUart1, UART_CTL_PARITY_BIT, UART_PARITY_NONE);
1058 hw_uart_write_stop_bit(USART1, 1);
1059 hw_uart_write_word_len(USART1, 8);
1060 xbar_control_cfg_out_put(UART1_TX, XBAR_CTL_CFG_1); //
1061 gpio_control_cfg_dir(UART1_RX, INPUT);
1062 xbar_control_cfg_uart(eUsart1, XBAR_IN_USART1_CFG_CLK_NONE, XBAR_IN_USART1_CFG_RX_PC7);
1063
1064 // Configure UART0
1065 hw_uart_write_baud_gen_tim(USART0, UART_CFG_BRG_TIM1);
1066 uart_control(pUart0, UART_CTL_PARITY_BIT, UART_PARITY_NONE);
1067 hw_uart_write_stop_bit(USART0, 1);
1068 hw_uart_write_word_len(USART0, 8);
1069 xbar_control_cfg_out_put(UART0_TX, XBAR_CTL_CFG_1); //
1070 gpio_control_cfg_dir(UART0_RX, INPUT);
1071 xbar_control_cfg_uart(eUsart0, XBAR_IN_USART0_CFG_CLK_NONE, XBAR_IN_USART0_CFG_RX_PB5);
1072
1073 hw_tim_write_mode(TIM1, TIM_CFG_MOD_MUL_SAW_UP);
1074 NVIC_EnableIRQ(TIM1_IRQn);
1075 NVIC_SetPriority(TIM1_IRQn, TIM1_IRQ_PRIO);
1076#endif
1077}
1078
1079
1085{
1086 count_10ms--;
1087
1088 if(!count_10ms)
1089 {
1090 count_10ms = 10;
1091 slot_10ms = true;
1092 }
1093}
1094
1100void main__Start_Motor(int16_t speed, int16_t accel)
1101{
1102 if(speed < 0) {
1103 TestBldcSpd = -speed;
1104 TrapSless__GetVars()->dir = ROT_CCW;
1105 }
1106 else {
1107 TestBldcSpd = speed;
1108 TrapSless__GetVars()->dir = ROT_CW;
1109 }
1110 TestBldcRamp = accel;
1111 TestBldcOn = 1;
1112}
1113
1118{
1119 TestBldcOff = 1;
1120}
1121
1127{
1128 return pUart0;
1129}
void API006MemoryRW__Initialize(void)
It Initializes the module API006MemoryRW and its variables.
void API023MotorControl__Handler(void)
API0023 Handler.
void API023MotorControl__Initialize(void)
It Initializes the module API023MotorControl and its variables.
#define ADC_IC
#define ADC_CH0
#define ADC_IA
#define ADC_CH2
#define ADC_CH4
#define ADC_CH5
#define ADC_VC
#define ENABLE
#define CLEAR
#define ADC_CH3
#define ADC_CH1
#define ADC_VB
#define ADC_IB
#define DISABLE
#define CH0_ENABLE
#define ADC_VA
#define ADC_CH7
#define ADC_CH6
#define NUM_ADC_SAMPLES
#define ADC_VBUS
uint32_t customUart_initialize(UsartInstance_Enum e_uart, USART_SignalEvent_t cb_event, void **pp_uart)
Definition: CustomUart.c:66
void LSI__ToggleLEDs(LED_NAME_TYPE led)
Method to toggle the LEDs values.
Definition: LSI.c:209
void LSI__DigitalOutputLEDs(LED_NAME_TYPE led, unsigned char value)
Method to set the LEDs values.
Definition: LSI.c:192
void LSI__Initialize(void)
It Initializes the module LSI and its variables.
Definition: LSI.c:49
void Micro__Initialize(void)
It Initializes the module Micro and its variables.
Definition: Micro.c:33
void PWMDriver__ConfigOutput(void)
Configures the PWM output pins.
Definition: PWMDriver.c:88
void PWMDriver__ConfigPwm(uint16_t pwm_deadtime, uint16_t ADC_Advance_Delay_Syslks)
Configures the PWM output peripheral.
Definition: PWMDriver.c:152
void SerialBuffer__NewMessageReceived(void)
Method to be called when a new message is received.
Definition: SerialBuffer.c:358
void SerialBuffer__DropFrameReceived(void)
Method to drop frame receiver per error.
Definition: SerialBuffer.c:426
bool SerialBuffer__GetTXBufferIsEmpty(void)
Returns if the TX buffer is Empty.
Definition: SerialBuffer.c:263
void SerialBuffer__HandlerTX(void)
Serial Buffer TX Handler.
Definition: SerialBuffer.c:135
#define SCI_HEADER_SIZE
void TimerInterrupt__Initialize(void)
This method initializes TimerInterrupt module.
#define SYSTICK_PER_CNT
void TrapSless__SlowHandler()
Handler for Trapezoidal control - 1KHz call rate.
Definition: TrapSless.c:694
void TrapSless__SetSpd(int32_t spd_rpm, int32_t ramp_rpm)
BLDC motor rotation command.
Definition: TrapSless.c:899
TRAPSLESS_PATTERN_TYPE * TrapSless__GetVars(void)
Obtaine the trapezoidal variables.
Definition: TrapSless.c:1116
void TrapSless__Init(void)
Initialization of TrapSless module.
Definition: TrapSless.c:184
void TrapSless__Handler()
Handler for Trapezoidal control - 16KHz call rate.
Definition: TrapSless.c:437
void Tsp__FastHandler(void)
Definition: Tsp.c:132
void Tsp__Initialize(void)
It Initializes the module Tsp and its variables.
Definition: Tsp.c:73
void ErrorEvent(uint32_t event)
Error Event function for Dma Passed as argument of dma_initialize function;.
Definition: main.c:451
void * pTim1
Definition: main.c:89
volatile uint8_t DmaEnableRequest
Definition: main.c:85
#define SPI_CLK_CFG
Definition: main.c:36
#define UART0_TX
Definition: main.c:42
void PWM0Event(uint32_t event)
Pwm0 Event function Passed as argument of pwm_initialize function;.
Definition: main.c:307
void TimerPwmSetup(uint8_t ADC_Advance, uint16_t ADC_Advance_Delay_Syslks)
Pwm configuration function. Called by main_init function.
Definition: main.c:856
uint32_t ADC_CTL_ptr[NUM_ADC_SAMPLES]
Definition: main.c:62
bool UART0Event(uint32_t event)
Uart0 Event function.
Definition: main.c:134
void TimerSync(uint8_t SawUp)
Timer sync up function between TIM0 and TIM2. Called by TIM0Event function.
Definition: main.c:955
void * pPwm2
Definition: main.c:94
void UartSetup(void)
Uart setup funtion. Called by main_init function.
Definition: main.c:1043
#define SPI_SS_CFG
Definition: main.c:37
void PWM1Event(uint32_t event)
Pwm1 Event function Passed as argument of pwm_initialize function;.
Definition: main.c:328
void AdcSetup(void)
Adc configuration function. Called by main_init function.
Definition: main.c:700
void main_count_1ms(void)
Main 1ms handler inside dma event function. Called by DMA_Event function.
Definition: main.c:1084
#define UART1_RX
Definition: main.c:45
unsigned char TestBldcOff
Definition: main.c:109
volatile int32_t TestBldcRamp
Definition: main.c:105
void SpiInit(void)
Spi init function - (debug purposes). Called by main_init function.
Definition: main.c:1000
void TIM2Event(uint32_t event)
Tim2 Event function Passed as argument of tim_initialize function;.
Definition: main.c:293
void main_10ms(void)
Main handler of 10ms. Called at a while(1) infinite loop at main(). Time controlled by a flag - slot_...
Definition: main.c:558
void HardFault_Handler(void)
Hard fault handler. Cause reset due while(1)
Definition: main.c:691
void * pPwm0
Definition: main.c:91
void main_init(void)
Main initialization function . Executes at start of main function.
Definition: main.c:489
volatile uint8_t count_10ms
Definition: main.c:80
volatile uint32_t TIM0_PER
Definition: main.c:64
void * pTim2
Definition: main.c:90
uint16_t AdvDelay
Definition: main.c:72
void WDogEvent(uint32_t event)
Watchdog Event function Passed as argument of wdog_initialize function;.
Definition: main.c:459
void GpioXbarSetup(void)
Gpio and crossbar function initialization .
Definition: main.c:618
volatile uint8_t slot_10ms
Definition: main.c:81
uint16_t ADC_Samples[NUM_ADC_SAMPLES]
Definition: main.c:61
void * pUart0
Definition: main.c:95
volatile unsigned short TimerOut1s
Definition: main.c:78
void main_step(void)
Main step function (FOR SIMULATION ONLY). Executes at control rate of 4KHz.
Definition: main.c:468
void * pTim0
Definition: main.c:88
void * main__pointerUart0()
Get the uart0 configuration pointer. Used by Serial Buffer module.
Definition: main.c:1126
volatile uint8_t Counter1KHz
Definition: main.c:82
void main__Stop_Motor(void)
Main motor stop function.
Definition: main.c:1117
volatile uint8_t Saw_Up
Definition: main.c:71
#define SPI_MOSI
Definition: main.c:34
#define SPI_SS
Definition: main.c:32
void * pPwm1
Definition: main.c:92
int main(void)
Main function.
Definition: main.c:672
volatile uint16_t TestBldcUpdtSpdTimer
Definition: main.c:106
volatile uint8_t TimerOutFlashOperation
Definition: main.c:79
bool UART1Event(uint32_t event)
Uart1 Event function.
Definition: main.c:181
bool bRxDone
Definition: main.c:100
void main__Start_Motor(int16_t speed, int16_t accel)
Main motor start function.
Definition: main.c:1100
bool bTxDone
Definition: main.c:99
#define TIM1_PER
Definition: main.c:49
void PWM2Event(uint32_t event)
Pwm2 Event function Passed as argument of pwm_initialize function;.
Definition: main.c:336
#define TIM_CLK_DIV
Definition: main.c:48
unsigned char TestBldcOn
Definition: main.c:108
void DMA_Event(uint32_t event)
Dma Event function Passed as argument of dma_initialize function;.
Definition: main.c:344
volatile uint8_t TestBldcUpdtSpdReq
Definition: main.c:107
void * pUart1
Definition: main.c:96
#define ADC_SAMPLE_PERIOD
Definition: main.c:52
void PWM3Event(uint32_t event)
Pwm3 Event function Passed as argument of pwm_initialize function;.
Definition: main.c:319
volatile uint8_t Sync_Timer02
Definition: main.c:70
#define UART1_TX
Definition: main.c:44
#define UART0_RX
Definition: main.c:43
void DmaSetup(void)
Dma configuration function. Called by main_init function.
Definition: main.c:806
void SysTickCallBack(void)
Systick call back function Passed as argument of systick_initialize function at TimerInterrut....
Definition: main.c:200
uint32_t SPIData[5]
Definition: main.c:75
#define SPI_CLK
Definition: main.c:31
void * pPwm3
Definition: main.c:93
#define SPI_MOSI_CFG
Definition: main.c:39
volatile int32_t TestBldcSpd
Definition: main.c:104
void TIM0Event(uint32_t event)
Tim0 Event function Passed as argument of tim_initialize function;.
Definition: main.c:212
Header file for main.h.c.
#define ATOD_IRQ_PRIO
Definition: main.h:21