MQX 4.0 3 adc channel opens crash the task

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

MQX 4.0 3 adc channel opens crash the task

Jump to solution
941 Views
albertcalpito
Contributor II

MQX 4.0     K20N512VLQ100

When I try to open 3 ADC channels, the calling task does not make it past the 3rd fopen call.

Opening 2 ADC channels works, but the 3rd seems to crash the task.

I have already made the appropriate defines for more than 2 adc channels:

    #define ADC_MAX_SW_CHANNELS       (3)

     #define ADC_CHANNELS_PER_ADC (3)

When I call the 3rd fopen(   fopen("adc0:4"  )    in my IAR debugger, I am able to get to the following line in the adt_kpdb.c file:

     PDB0_BASE_PTR->SC |= PDB_SC_LDOK_MASK; /* write new DLY value */

After this line is executed, the program seems to never return to the main_task.

Does anyone see any issues with the following code sequence?:

void my_main_task(uint_32 initial_data) 
{
  LWEVENT_STRUCT adc_event;
  MQX_FILE_PTR adc0_ID;
  MQX_FILE_PTR channel_id;
  ADC_INIT_CHANNEL_STRUCT adc0_params;
  ADC_INIT_STRUCT adc_init = { ADC_RESOLUTION_DEFAULT,     /* resolution */};

  _lwevent_create(&adc_event,0);


  adc0_ID = fopen("adc0:", (const char*)&adc_init);

  adc0_params.source = ADC0_SOURCE_AD8;  /* one of ADC_SOURCE_* definitions defined in ./<module_name>/adc_<module_name>.h - CPU specific header file */
  adc0_params.flags = (ADC_CHANNEL_MEASURE_LOOP | ADC_CHANNEL_START_TRIGGERED);  /* initialization flags, combination of ADC_CHANNEL_<FLAG_NAME> possibilities above */
  adc0_params.number_samples = 10;  /* number of samples in one run sequence, the number of samples needed to trigger event */
  adc0_params.time_offset = 50;    /* the time offset applied once after measurement on given channel is started, in microseconds */
  adc0_params.time_period = 100000;  /* the time slot between two samples in microseconds */
  adc0_params.range = 0x10000;  /* scale range of result (not used now) */
  adc0_params.buffer_size = 10;  /* ADC output buffer size, measured samples are stored here */
  adc0_params.trigger = 0x10000;  /* trigger mask that fires this channel */
  adc0_params.complete_event = &adc_event;  /* pointer to lwevent to be signaled after nuber_samples is measured */
  adc0_params.event_mask = 0x01;  /* event mask to be signalled in lwevent after nuber_samples is measured */


  channel_id = fopen("adc0:1" , (const char*)&adc0_params);  /*this will request to open this channel*/

  adc0_params.source = ADC0_SOURCE_AD9;  /* one of ADC_SOURCE_* definitions defined in ./<module_name>/adc_<module_name>.h - CPU specific header file */
  channel_id = fopen("adc0:2" , (const char*)&adc0_params);  /*this will request to open this channel*/

  adc0_params.source = ADC0_SOURCE_DM1;  /* one of ADC_SOURCE_* definitions defined in ./<module_name>/adc_<module_name>.h - CPU specific header file */
  channel_id = fopen("adc0:4" , (const char*)&adc0_params);  /*this will request to open this channel*/


  while(1)
  {
    _time_delay(1000);
  }


  _task_block();
}

Tags (4)
0 Kudos
1 Solution
471 Views
albertcalpito
Contributor II

Hello Monica,

Yes after following the thread I was able to get LWADC to work for me after making modifications for 2 adc devices.

Like another user, I also found that the example program with multiple channels did not work unless I called     _lwadc_init_input 

before every lwadc_read  call.

Because of this, I am not using a LWADC_STRUCT for every channel.  To get multiple channels to work, I do the following:

LWADC_VALUE get_adc_reading(uint_32 adc_channel )

{

  LWADC_STRUCT adc_file;

  LWADC_VALUE reading = 0;

  _mutex_lock(&adc_lock); /*wait until it is safe to update the gprms data*/

     

    _lwadc_init_input(&adc_file ,adc_channel);  /*first select the channel we want to read*/

    _lwadc_read_average(&adc_file, 10, &reading);  /*average out 10 readings*/

    // _lwadc_read_raw(lwadc_ptr,  &reading);

    //_lwadc_read(lwadc_ptr, &reading);  /*this converts the reading to mV*/

  

_mutex_unlock(&adc_lock);    /*after we are done updating, it is safe for users to read the gprmc data*/

 

     return reading;

}

I'm not sure if this is the correct way to use the LWADC, but it seems to be working as of now.

View solution in original post

0 Kudos
4 Replies
471 Views
Monica
Senior Contributor III

Hello albert,

was that thread helpful? We'd like to know, please keep us posted :smileywink:

Best regards,

Monica

0 Kudos
472 Views
albertcalpito
Contributor II

Hello Monica,

Yes after following the thread I was able to get LWADC to work for me after making modifications for 2 adc devices.

Like another user, I also found that the example program with multiple channels did not work unless I called     _lwadc_init_input 

before every lwadc_read  call.

Because of this, I am not using a LWADC_STRUCT for every channel.  To get multiple channels to work, I do the following:

LWADC_VALUE get_adc_reading(uint_32 adc_channel )

{

  LWADC_STRUCT adc_file;

  LWADC_VALUE reading = 0;

  _mutex_lock(&adc_lock); /*wait until it is safe to update the gprms data*/

     

    _lwadc_init_input(&adc_file ,adc_channel);  /*first select the channel we want to read*/

    _lwadc_read_average(&adc_file, 10, &reading);  /*average out 10 readings*/

    // _lwadc_read_raw(lwadc_ptr,  &reading);

    //_lwadc_read(lwadc_ptr, &reading);  /*this converts the reading to mV*/

  

_mutex_unlock(&adc_lock);    /*after we are done updating, it is safe for users to read the gprmc data*/

 

     return reading;

}

I'm not sure if this is the correct way to use the LWADC, but it seems to be working as of now.

0 Kudos
471 Views
Monica
Senior Contributor III

Great!

Thanks for sharing your workaround with Community, we're lucky to have you as a contributor! :smileygrin:

Hope you have a great day!

Monica

0 Kudos
471 Views
c0170
Senior Contributor III

Hello albert calpito,

please read this thread: A/D Driver Unable to Open Channel

It should provide some additional information why it fails. Myke here on the forum ported new LWADC to K20, it is officially supported only for K21 at the moment.

Regards,

c0170

0 Kudos