How to use the ADC on StartKit?

All technical discussions and projects around startKIT
User avatar
MatCat
Active Member
Posts: 40
Joined: Wed Dec 18, 2013 11:51 am
Contact:

How to use the ADC on StartKit?

Post by MatCat »

How exactly does one use the ADC? I tried simply reading 1A however it always returns 0, even with 3.3v applied.


User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

Example usage of ADCs on startKIT is available in workspace attached.

Sethu.
Attachments
adc_startKIT.zip
(7.69 KiB) Downloaded 1423 times
adc_startKIT.zip
(7.69 KiB) Downloaded 1423 times
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

Example usage of ADCs on startKIT is available in workspace attached.
Wow! that example isn't exactly easy for newbies to get their heads around, it also doesn't seem to use the standard init functions that Xmos provides for interfacing the ADC. Is there not an example like the U series adc but for startkit?

*Update - Sorry my bad spoke to soon, this is a really neat new way of doing things but some comments and explanation in the code would go a long way to help explaining what is happening and how to use these methods generally. Maybe tucking away (or abstracting/hiding) some of the lower level ASM init stuff might also be a good idea. It is still a fairly scary example for a newbie however ;-)

regards
Al
User avatar
XMatt
XCore Addict
Posts: 147
Joined: Tue Feb 23, 2010 6:55 pm

Post by XMatt »

Folknology wrote:
Example usage of ADCs on startKIT is available in workspace attached.
*Update - Sorry my bad spoke to soon, this is a really neat new way of doing things but some comments and explanation in the code would go a long way to help explaining what is happening and how to use these methods generally. Maybe tucking away (or abstracting/hiding) some of the lower level ASM init stuff might also be a good idea. It is still a fairly scary example for a newbie however ;-)

regards
Al
Yep we are intending to tidy this up and post some examples, the extra setup code that is required for allowing tile 1 to use the ADC on tile 0 for startKIT will most likely move to its own module (as it is pretty low level setup which wont concern most) but people should be able to get started with this.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

After setting configs for the ADC you then perform 6 discarded samples:

Code: Select all

for (int i = 0; i < 6; i++) {
       t += 65;
       adc_sample @ t <: 1;
       t += 65;
       adc_sample @ t <: 0;
     }
Can you explain why that is needed and would this change at all if one wanted to use all four channels on the startkit (guesses):

Code: Select all

...
write_periph_32(adc_tile, 2, 0x0, 1, data);
write_periph_32(adc_tile, 2, 0x4, 1, data);
write_periph_32(adc_tile, 2, 0x8, 1, data);
write_periph_32(adc_tile, 2, 0xC, 1, data);
...

for (int i = 0; i < 6; i++) {
     t += 65;
     adc_sample @ t <: 1;
     t += 65;
     adc_sample @ t <: 0;
}

?

*Update I think I got it, these are the calibration samples, it needs 6?

P.S. Why is the sample pulse width set for 650ns rather than 400ns

regards
Al
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

BTW does anyone mind if I port the U_Series adc support over to a startkit version and proceed each function call with 'sk_' e.g.:

Code: Select all

/**
 * Configure and enable the requested ADCs. Will also perform the calibration
 * pulses so that the ADCs are ready to provide data.
 *
 * sk_adc_enable() also checks that the configuration is valid and will raise a
 * trap if attempting to incorrectly configure the ADCs.
 *
 * \param periph_tile  The identifier of the tile containing the ADCs
 * \param adc_chan     The chanend to which all ADC samples will be sent.
 * \param trigger_port The port connected to the ADC trigger pin.
 * \param config       The configuration to be used.
 *
 * \return ADC_OK on success and one of the return codes in adc_return_t on an error.
 */
void sk_adc_enable(tileref periph_tile, chanend adc_chan, out port trigger_port, const_adc_config_ref_t config);

/**
 * Disable all of the ADCs.
 */
void sk_adc_disable_all(tileref periph_tile);

/**
 * Causes the ADC to take one sample. This function is intended to be used with
 * sk_adc_read(). If used with sk_adc_read_packet() then this function must be called
 * enough times to ensure that an entire data packet will be available before
 * the sk_adc_read_packet() is called.
 *
 * \param trigger_port The port connected to the ADC trigger pin.
 */
void sk_adc_trigger(out port trigger_port);

/**
 * Trigger the ADC enough times to complete a packet.
 *
 * \param trigger_port The port connected to the ADC trigger pin.
 * \param config       The ADC ocnfiguration.
 */
void sk_adc_trigger_packet(out port trigger_port, const_adc_config_ref_t config);

/**
 * A selectable function to read an ADC sample from the chanend. Any
 * control tokens due to packetization will be discarded silently.
 *
 * Note that the sk_adc_trigger function must have been called
 * before this function will return any data.
 *
 * Note that the configuration must be the same as that used when
 * enabling the ADCs.
 *
 * \param adc_chan     The chanend to which all ADC samples will be sent.
 * \param config       The ADC configuration.
 * \param data         The word to place the data in.
 *
 */
#ifdef __XC__
#pragma select handler
#endif
void sk_adc_read(chanend adc_chan,
              const_adc_config_ref_t config,
              REFERENCE_PARAM(unsigned int, data));

/**
 * A selectable function to read a packet of ADC samples from the chanend.
 *
 * Note that the sk_adc_trigger_packet function must have been called
 * before this function will return any data.
 *
 * Note that the configuration must be the same as that used when
 * enabling the ADCs.
 *
 * \param adc_chan     The chanend to which all ADC samples will be sent.
 * \param config       The ADC configuration.
 * \param data         The buffer to place the returned data in. Each
 *                     sample will be placed in a separate word. The
 *                     buffer must be big enough to store all the data
 *                     that will be read (samples_per_packet words).
 *
 */
#ifdef __XC__
#pragma select handler
#endif
void sk_adc_read_packet(chanend adc_chan,
              const_adc_config_ref_t config,
              unsigned int data[]);
I will change the trigger port to 1A and max adc channels to 4, as well as adding in the adc network support. it could be included as module_startkit_adc or some such.

P.S. I notice that the new Analog tile chips have something similar and use 'at_' before the calls, It would be possible to use this along with some #IFDEF __STARTKIT__ or some such to create a unified version, but that would also probably require XS1_MAX_NUM_ADC and PORT_ADC_TRIGGER to be specified inside platform.h or maybe the XN file perhaps?

regards
Al
User avatar
MatCat
Active Member
Posts: 40
Joined: Wed Dec 18, 2013 11:51 am
Contact:

Post by MatCat »

I couldn't get this example to work at all. When I debug it it seems to hang up on the 5th LED and first read_sswitch_reg call, I tried putting breakpoints in various places, stepping through the code and is as far as it will go.

Where is read/write_sswitch_reg documented?

The example codes really need to be commented much better at line by line level to good a good idea of whats going on, further reference material to read. Would be nice to take all of the raw hex values and put them in DEFINE so they make more sense as well.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm
Contact:

Post by Folknology »

MatCat wrote:I couldn't get this example to work at all. When I debug it it seems to hang up on the 5th LED and first read_sswitch_reg call, I tried putting breakpoints in various places, stepping through the code and is as far as it will go.

Where is read/write_sswitch_reg documented?

The example codes really need to be commented much better at line by line level to good a good idea of whats going on, further reference material to read. Would be nice to take all of the raw hex values and put them in DEFINE so they make more sense as well.
It seems to be running fine for me, better double check the code, did you run it verbatim or modify/add to it?

regards
Al
User avatar
MatCat
Active Member
Posts: 40
Joined: Wed Dec 18, 2013 11:51 am
Contact:

Post by MatCat »

Folknology wrote:
MatCat wrote:I couldn't get this example to work at all. When I debug it it seems to hang up on the 5th LED and first read_sswitch_reg call, I tried putting breakpoints in various places, stepping through the code and is as far as it will go.

Where is read/write_sswitch_reg documented?

The example codes really need to be commented much better at line by line level to good a good idea of whats going on, further reference material to read. Would be nice to take all of the raw hex values and put them in DEFINE so they make more sense as well.
It seems to be running fine for me, better double check the code, did you run it verbatim or modify/add to it?

regards
Al
Verbatim, just downloaded and ran
User avatar
MatCat
Active Member
Posts: 40
Joined: Wed Dec 18, 2013 11:51 am
Contact:

Post by MatCat »

When I debug it, and pause it it is hung up on this:

000105cc: outt (r2r) res[r4], r1 *
Post Reply