Hello NXP Team,
i can convert ADC values without issue using the example coming with the SDK.
i have a requirement that when the controller receive a CAN message it should do an ADC conversion and return back the result in other CAN message.
the MCU do this in first attempt,and i get a CAN respond back with the correct ADC values, but when i send the command the second time the MCU stops at a hard fault (ADC0_IRQHandler).
here is my code for ADC
void adc1_init(void)
{
/* Enable the floating point unit et both CPACR[CP11] and CPACR[CP10] to Full Access - 0b11 */
S32_SCB->CPACR |= (S32_SCB_CPACR_CP10_MASK | S32_SCB_CPACR_CP11_MASK);
/* Get ADC max value from the resolution */
if (adConv1_ConvConfig0.resolution == ADC_RESOLUTION_8BIT)
adcMax = (uint16_t) (1 << 8);
else if (adConv1_ConvConfig0.resolution == ADC_RESOLUTION_10BIT)
adcMax = (uint16_t) (1 << 10);
else
adcMax = (uint16_t) (1 << 12);
ADC_DRV_ConfigConverter(INST_ADCONV1, &adConv1_ConvConfig0);
ADC_DRV_AutoCalibration(INST_ADCONV1);
INT_SYS_SetPriority(ADC0_IRQn,configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
}
void TEST_KL15_AD(uint16_t *adcVal)
{
float adcValue = 0.0f;
uint16_t adcRawValue = 0U;
/* Configure ADC channel and software trigger a conversion */
ADC_DRV_ConfigChan(INST_ADCONV1, 0U, &adConv_KL15_ch10);
/* Wait for the conversion to be done */
ADC_DRV_WaitConvDone(INST_ADCONV1);
/* Store the channel result into a local variable */
ADC_DRV_GetChanResult(INST_ADCONV1, 0U, &adcRawValue);
/* Process the result to get the value in volts */
adcValue = ((float) adcRawValue / adcMax) * (ADC_VREFH - ADC_VREFL);
printf("KL15_AD: %f\r\n", adcValue);
*adcVal = FLOAT_TO_INT(adcValue);
}
i task call the adc1_init() , and in the loop we read if we have a valid CAN message in the mailbox.
void canCyclic(void *pvParameters)
{
(void)pvParameters;
// initialize the SBC and CAN driver
can_driver_init();
// initialize ADC
adc1_init();
while(1)
{
can_read_MailBox();
vTaskDelay(10/portTICK_PERIOD_MS);
}
}
void can_read_MailBox(){
flexcan_msgbuff_t recvBuff;
status_t rx_ret_value = STATUS_UNSUPPORTED;
uint8_t RX_MAILBOX = 0;
/* Start receiving data in RX_MAILBOX. */
rx_ret_value = FLEXCAN_DRV_Receive(INST_CANCOM1, RX_MAILBOX, &recvBuff);
if(rx_ret_value == STATUS_SUCCESS)
{
can_rx_interpreter(recvBuff);
}
}
void can_rx_interpreter(flexcan_msgbuff_t rxCanMessage)
{
switch(rxCanMessage.msgId)
{
case Read_TCU_Analog_IN:
uint8_t dataOut[8];
memset(dataOut, 0, 8);
uint16_t kl15_ad_value = 0U;
TEST_KL15_AD(&kl15_ad_value);
dataOut[0] = (kl15_ad_value >> & 0xFF;
dataOut[1] = (kl15_ad_value) & 0xFF;
can_Transmit_Results(2,TCU_Analog_IN_D,dataOut,8);
break;
default:
printf("id not supported \r\n");
break;
}
}
Thank you.
it turn out that the problem reside in this line
*adcVal = (uint16_t)(adcValue);
the compiler does not give error, but when executed the runtime stops !
is there a convenient way to convert from Float to uint ?
Thank you.