How to use the ADC on StartKit?
Posted: Fri Jan 10, 2014 1:14 am
How exactly does one use the ADC? I tried simply reading 1A however it always returns 0, even with 3.3v applied.
The community of XMOS. The place to be when developing with XMOS technology.
https://www.xcore.com:443/
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?Example usage of ADCs on startKIT is available in workspace attached.
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.Folknology wrote:*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 ;-)Example usage of ADCs on startKIT is available in workspace attached.
regards
Al
Code: Select all
for (int i = 0; i < 6; i++) {
t += 65;
adc_sample @ t <: 1;
t += 65;
adc_sample @ t <: 0;
}
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;
}
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[]);
It seems to be running fine for me, better double check the code, did you run it verbatim or modify/add to it?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.
Verbatim, just downloaded and ranFolknology wrote:It seems to be running fine for me, better double check the code, did you run it verbatim or modify/add to it?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.
regards
Al