fsl_pwm integer conversion failure?

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

fsl_pwm integer conversion failure?

630 Views
choix361
Contributor II

The processor is RT1064.

Release Name: MCUXpresso Software Development Kit (SDK)
Release Version: 25.12.00

In fsl_pwm.c line 790,

pulseCnt = pulseEndCnt - base->SM[subModuleSync].INIT + 1U;

Each term is uint16_t.

 

When pulseEndCnt < base->SM[subModuleSync].INIT, the result can be negative as int32_t. pulseCnt(uint16_t) will take the result and upper two bytes would be truncated. What would be the proper casting to keep the intention?

 

 

0 Kudos
Reply
3 Replies

603 Views
mayliu1
NXP Employee
NXP Employee

Hi @choix361 ,

Thank you so much for your interest in our products and for using our community.

 

Please notes that RT1064 eFlexPWM count-related registers are defined as signed 16-bit values.

1.png

I suggest you can try to do the arithmetic in int32_t , and only then convert to uint16_t after range checking.

int32_t pulseCntCalc = (int32_t)(int16_t)pulseEndCnt - (int32_t)(int16_t)base->SM[subModuleSync].INIT + 1;

 

if(( pulseCntCalc < 0) || (pulseCntCalc > UINT16_MAX))

{

// you can print log and handle invalid range

}

else

{

pulseCnt = (uint16_t)pulseCntCalc;

}

Wish it helps you

Best Regards

MayLiu

0 Kudos
Reply

587 Views
choix361
Contributor II

if(( pulseCntCalc < 0) || (pulseCntCalc > UINT16_MAX))

{

    // you can print log and handle invalid range

}

 

I saw many cases fall into if statement. Is it ok to intentionally truncate the data here?

Or set PulseCnt to certain fixed value?

if((pulseCntCalc < 0) || (pulseCntCalc > UINT16_MAX))
{
    //Intentionally truncate data
    pulseCntCalc = pulseCntCalc & 0x0000FFFF;
    pulseCnt = (uint16_t)pulseCntCalc;
}

0 Kudos
Reply

463 Views
mayliu1
NXP Employee
NXP Employee
I don’t recommend truncating the value. It’s better to first identify the root cause of why it exceeds the valid range.
0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2338796%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3Efsl_pwm%20integer%20conversion%20failure%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2338796%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThe%20processor%20is%20RT1064.%3C%2FP%3E%3CP%3ERelease%20Name%3A%20MCUXpresso%20Software%20Development%20Kit%20(SDK)%3CBR%20%2F%3ERelease%20Version%3A%2025.12.00%3C%2FP%3E%3CP%3EIn%20fsl_pwm.c%20line%20790%2C%3C%2FP%3E%3CP%3EpulseCnt%20%3D%20pulseEndCnt%20-%20base-%26gt%3BSM%5BsubModuleSync%5D.INIT%20%2B%201U%3B%3C%2FP%3E%3CP%3EEach%20term%20is%20uint16_t.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWhen%20pulseEndCnt%20%26lt%3B%26nbsp%3Bbase-%26gt%3BSM%5BsubModuleSync%5D.INIT%2C%20the%20result%20can%20be%20negative%20as%20int32_t.%20pulseCnt(uint16_t)%20will%20take%20the%20result%20and%20upper%20two%20bytes%20would%20be%20truncated.%20What%20would%20be%20the%20proper%20casting%20to%20keep%20the%20intention%3F%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2339483%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20fsl_pwm%20integer%20conversion%20failure%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2339483%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3Eif((%20pulseCntCalc%20%26lt%3B%200)%20%7C%7C%20(pulseCntCalc%20%26gt%3B%20UINT16_MAX))%3C%2FP%3E%3CP%3E%7B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%2F%2F%20you%20can%20print%20log%20and%20handle%20invalid%20range%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EI%20saw%20many%20cases%20fall%20into%20%3CEM%3E%3CSTRONG%3Eif%20statement%3C%2FSTRONG%3E%3C%2FEM%3E.%20Is%20it%20ok%20to%20intentionally%20truncate%20the%20data%20here%3F%3C%2FP%3E%3CP%3EOr%20set%20PulseCnt%20to%20certain%20fixed%20value%3F%3C%2FP%3E%3CP%3Eif((pulseCntCalc%20%26lt%3B%200)%20%7C%7C%20(pulseCntCalc%20%26gt%3B%20UINT16_MAX))%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%2F%2FIntentionally%20truncate%20data%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20pulseCntCalc%20%3D%20pulseCntCalc%20%26amp%3B%200x0000FFFF%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20pulseCnt%20%3D%20(uint16_t)pulseCntCalc%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2339141%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20fsl_pwm%20integer%20conversion%20failure%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2339141%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F192846%22%20target%3D%22_blank%22%3E%40choix361%3C%2FA%3E%26nbsp%3B%EF%BC%8C%3C%2FP%3E%0A%3CP%3EThank%20you%20so%20much%20for%20your%20interest%20in%20our%20products%20and%20for%20using%20our%20community.%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditormayliu1_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EPlease%20notes%20that%20RT1064%20eFlexPWM%20count-related%20registers%20are%20defined%20as%26nbsp%3Bsigned%2016-bit%26nbsp%3Bvalues.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%221.png%22%20style%3D%22width%3A%20749px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%221.png%22%20style%3D%22width%3A%20749px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%221.png%22%20style%3D%22width%3A%20749px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F380332iDE9457499BCB69EB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%221.png%22%20alt%3D%221.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EI%20suggest%20you%20can%20try%20to%20do%20the%20arithmetic%20in%26nbsp%3Bint32_t%26nbsp%3B%2C%20and%20only%20then%20convert%20to%26nbsp%3Buint16_t%26nbsp%3Bafter%20range%20checking.%3C%2FP%3E%0A%3CDIV%3E%0A%3CDIV%3E%0A%3CP%3Eint32_t%20pulseCntCalc%20%3D%20(int32_t)(int16_t)pulseEndCnt%20-%20(int32_t)(int16_t)base-%26gt%3BSM%5BsubModuleSync%5D.INIT%20%2B%201%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3Eif((%20pulseCntCalc%20%26lt%3B%200)%20%7C%7C%20(pulseCntCalc%20%26gt%3B%20UINT16_MAX))%3C%2FP%3E%0A%3CP%3E%7B%3C%2FP%3E%0A%3CP%3E%2F%2F%20you%20can%20print%20log%20and%20handle%20invalid%20range%3C%2FP%3E%0A%3CP%3E%7D%3C%2FP%3E%0A%3CP%3Eelse%3C%2FP%3E%0A%3CP%3E%7B%3C%2FP%3E%0A%3CP%3EpulseCnt%20%3D%20(uint16_t)pulseCntCalc%3B%3C%2FP%3E%0A%3CP%3E%7D%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EWish%20it%20helps%20you%3C%2FP%3E%0A%3CP%3EBest%20Regards%3C%2FP%3E%0A%3CP%3EMayLiu%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2344885%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20fsl_pwm%20integer%20conversion%20failure%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2344885%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CDIV%3EI%20don%E2%80%99t%20recommend%20truncating%20the%20value.%20It%E2%80%99s%20better%20to%20first%20identify%20the%20root%20cause%20of%20why%20it%20exceeds%20the%20valid%20range.%3C%2FDIV%3E%3C%2FLINGO-BODY%3E