FTM Quadrature Decoder for S32K144

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FTM Quadrature Decoder for S32K144

3,128 Views
antperlag1
Contributor I

Hello,

I am using a phase encoder AB and I need to know the position of the pulses in order to know the direction of the motors (forward or backward). I have thought to use ftm_qd but there isn´t any example about it and how to configure the module in the processor expert.

Any solution or idea of how can I develop it?

Thanks.

0 Kudos
Reply
7 Replies

2,760 Views
kaihao_ying
Contributor I

Hello,

   I also have this problem and i want to use the SDK componments flexTimer QD for quadrature decoder operation. I want to calcalate the direction and speed of a motor and there is no example about how to use it. Did you find out a way or are there anyone else could help? 

Thanks a lot!

0 Kudos
Reply

2,760 Views
cjf6221
Contributor I

Hello Kaihao,

The motor position can be successfully measured by GPIO (using two GPIOs to sample the A and B signals) and I have successfully implemented it.  But the speed calculation has a large error under this case. That's why I truned to use QD. 

I would like to use QD to measure the motor position, but failed. I am still on the way to figure it out and realize it during this month. If using QD, the direction can be acheved by the code as below. It works well. 

Motor_Dir_Encoder = ((FTM2->QDCTRL)>>2)&0x1; // QUADIR is the bit that indicates the counting direction.

pastedImage_1.png

Hope it helps you.

Best

Jianfei Chen

0 Kudos
Reply

2,760 Views
kaihao_ying
Contributor I

Hi Jianfei,

   Thanks for your advice, and it really helped a lot.

   I have figured out using FTM_QD module to calculate the phase of the AB signals. 

   Thank you!

 

Best Wishes

Kaihao

0 Kudos
Reply

2,760 Views
cjf6221
Contributor I

Hello Kaihao,

Good job !

So what kind of interrupt do you use ? Counter overflow or FTM Channel interrupt ? I used FTM2_QD to measure every rising or falling edge of A and B by using counter overflow interrupt. But I am still troubled by the interrupt configuration. 

Below is my configuration for FTM2_QD. Please provide some help. Thanks. By the way, my Wechat ID is cjf715822 and email address is cjf6221@gmail.com. 

void FTM2_Quadrature_Decoder_Mode()
{
/* Enable clock for PORTD */
PCC->PCCn[PCC_PORTD_INDEX] = PCC_PCCn_CGC_MASK;
/* Select and enable clock for FTM2 */
PCC->PCCn[PCC_FLEXTMR2_INDEX] = PCC_PCCn_PCS(6) | PCC_PCCn_CGC_MASK;

PORTD->PCR[10] = PORT_PCR_MUX(3); // Set PTD10 for FTM2 - Phase B input
PORTD->PCR[11] = PORT_PCR_MUX(3); // Set PTD11 for FTM2 - Phase A input

FSL_NVIC->ISER[FTM2_IRQn / 32] |= (1 << (FTM2_IRQn % 32)); // Enable FTM2 counter overflow interrupt
// FSL_NVIC->ICPR[116/32] = 1 << (116% 32);
FSL_NVIC->ISER[116/32] = 1 << (116% 32);
// FSL_NVIC->IP[116] = 0;

FTM2->MODE = FTM_MODE_WPDIS_MASK | FTM_MODE_INIT_MASK | FTM_MODE_FTMEN_MASK;

//Enable FTM2 module (0x00000005): D2(WPDIS) and D0(FTMEN) are set.

FTM2->MOD = FTM_MOD_MOD(4096); // overflow after totally 4096 rising/falling edges
FTM2->CNT = 0;  
FTM2->CNTIN = FTM_CNTIN_INIT(0);

// FTM2->QDCTRL= FTM_QDCTRL_QUADEN_MASK; // Enable QD mode
FTM2->QDCTRL= 0x000000C1; // Enable QD mode and input filter mode
FTM2->SC |= FTM_SC_CLKS(1) | FTM_SC_PS(2) | FTM_SC_CPWMS_MASK | FTM_SC_TOIE_MASK;

// FTM_SC_CLKS(1): select FTM as input timer.
// FTM_SC_PS(2): clock source FTM, prescaler 2^2, clock frequency = 112MHz/4 = 28MHz
// FTM_SC_TOIE_MASK: enable timer overflow interruption
// FTM_SC_CPWMS_MASK; D5(CPWMS) is set to enable up-down counting mode
}

Below is the overflow interrupt function.

void FTM2_Ovf_Reload_IRQHandler()  
{
PTC->PTOR |= 1<<9; // Toggle PTC9 for scope display
FTM2_ISR_Counter++;
Temp_EncoderB = FTM2->CONTROLS[0].CnV; //store the captured C0V value (Counter's vaule)
Temp_EncoderA = FTM2->CONTROLS[1].CnV; //store the captured C1V value (Counter's vaule)
FTM2->CONTROLS[0].CnSC &= ~FTM_CnSC_CHF_MASK; //clear Ch0 flag D7(CHF) is required to be reset after reading
FTM2->CONTROLS[1].CnSC &= ~FTM_CnSC_CHF_MASK; //clear Ch1 flag D7(CHF) is required to be reset after reading
Count_Encoder = FTM2->CNT;
FTM2->SC &= ~FTM_SC_TOF_MASK; // Clear timer overflow flag D9(TOF) is reset

0 Kudos
Reply

2,760 Views
dianabatrlova
NXP TechSupport
NXP TechSupport

Hello Rong,

You should use S32_NVIC instead of FLS_NVIC in the code (it was used in older projects)

S32_NVIC->ICPR[3] = 1 << (116 % 32);
S32_NVIC->ISER[3] = 1 << (116 % 32);
S32_NVIC->IP[116] = 0x00;

If you were able to compile the project with FLS_NVIC code you have probably wrong header file. In that case, I recommend you to create a new project.

I hope it helps.

Best Regards,

Diana

0 Kudos
Reply

2,760 Views
dianabatrlova
NXP TechSupport
NXP TechSupport

Hello,

I would like to recommend the application note Features and Operation Modes of FlexTimer Module on S32K: https://www.nxp.com/docs/en/application-note/AN5303.pdf 

See the section "3.7. Quadrature decoder mode".

The SW of this app note does not use SDK, however, I believe it helps: AN5303SW 

Also, the section "45.5.27 Quadrature Decoder Mode" and  "Figure 45-6. FTM Block Diagram" in the RM rev 11. can be useful. 

Best Regards,

Diana

0 Kudos
Reply

2,760 Views
kaihao_ying
Contributor I

I have see the documents and i understood the way how it calculate the speed and direction. However, i still do not know how to use the SDK's module Flextimer_QD for specicic project, could you give me some guide on how to use it or show me some examples. Thank you so much.

0 Kudos
Reply