tests/drivers/adc: move to ztest to actually test

This TC is only exercising the API, as we don't have a feedback loop
mechanism to verify whichever values are fed to the ADC.

Fixed the loop to complete after 10 runs; on each run, print the
values and actually report the difference between them. With no inputs
connected (aka: floating), they should be reporting noise relatively
close to the previous reading, so we might want to use this delta as a
testing pattern (assert if the delta is higher than some value, but
I've seen variations as high as 40M units). For now, the test is just
happy with being able to read them.

The buffer has been re-typed to uint32_t so we can iterate over it
without casting tricks -- it requires then only a single cast when
initializing sample.buffer (which shall be a void* anyway).

Duplicated the buffer, so we can flip/flop between two buffers to
compare against the entries read in the previous run.

v4: fixed missed warnings

Change-Id: If6b48b92231007202d74f5c042f6d0cf3fdcb60a
Signed-off-by: Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
This commit is contained in:
Inaky Perez-Gonzalez 2016-11-04 23:20:53 -07:00 committed by Anas Nashif
commit 9e1df6f21f
4 changed files with 53 additions and 25 deletions

View file

@ -1,2 +1,3 @@
CONFIG_ZTEST=y
CONFIG_ADC=y
CONFIG_ADC_DEBUG=y

View file

@ -1 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test
obj-y = main.o

View file

@ -15,8 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <zephyr.h>
#include <ztest.h>
#include <device.h>
#include <misc/byteorder.h>
@ -38,15 +37,14 @@
* A4 Channel 14
*/
#define CHANNEL 10
#define BUFFER_SIZE 40
#define BUFFER_SIZE 10
static uint8_t seq_buffer[BUFFER_SIZE];
static uint32_t seq_buffer[2][BUFFER_SIZE];
static struct adc_seq_entry sample = {
.sampling_delay = 12,
.channel_id = CHANNEL,
.buffer = seq_buffer,
.buffer_length = BUFFER_SIZE,
.buffer_length = BUFFER_SIZE * sizeof(seq_buffer[0][0])
};
static struct adc_seq_table table = {
@ -54,36 +52,64 @@ static struct adc_seq_table table = {
.num_entries = 1,
};
static void _print_sample_in_hex(uint8_t *buf, uint32_t length)
static void _print_sample_in_hex(const uint32_t *buf, uint32_t length)
{
const uint32_t *top;
printk("Buffer content:\n");
for (; length > 0; length -= 4, buf += 4) {
printk("0x%x ", *((uint32_t *)buf));
}
for (top = buf + length; buf < top; buf++)
printk("0x%x ", *buf);
printk("\n");
}
void main(void)
static long _abs(long x)
{
struct device *adc;
return x < 0 ? -x : x;
}
printk("ADC sample started on %s\n", ADC_DEVICE_NAME);
static void adc_test(void)
{
int result = TC_FAIL;
struct device *adc;
unsigned int loops = 10;
unsigned int bufi0 = ~0, bufi;
adc = device_get_binding(ADC_DEVICE_NAME);
if (!adc) {
printk("Cannot get adc controller\n");
return;
}
assert_not_null(adc, "Cannot get adc controller\n");
adc_enable(adc);
while (1) {
if (adc_read(adc, &table) != 0) {
printk("Sampling could not proceed, an error occurred\n");
} else {
printk("Sampling is done\n");
_print_sample_in_hex(seq_buffer, BUFFER_SIZE);
while (loops--) {
bufi = loops & 0x1;
/* .buffer should be void * ... */
sample.buffer = (void *) seq_buffer[bufi];
result = adc_read(adc, &table);
assert_equal(result, 0, "Sampling could not proceed, "
"an error occurred\n");
printk("loop %u: sampling done to buffer #%u\n", loops, bufi);
_print_sample_in_hex(seq_buffer[bufi], BUFFER_SIZE);
if (bufi0 != ~0) {
unsigned int cnt;
long delta;
for (cnt = 0; cnt < BUFFER_SIZE; cnt++) {
delta = _abs((long)seq_buffer[bufi][cnt]
- seq_buffer[bufi0][cnt]);
printk("loop %u delta %u = %ld\n",
loops, cnt, delta);
}
}
k_sleep(SLEEPTIME);
bufi0 = bufi;
}
adc_disable(adc);
}
void test_main(void)
{
ztest_test_suite(_adc_test,
ztest_unit_test(adc_test));
ztest_run_test_suite(_adc_test);
}

View file

@ -1,4 +1,3 @@
[test]
build_only = true
tags = apps
platform_whitelist = galileo arduino_101_sss quark_d2000_crb
platform_whitelist = galileo arduino_101_sss quark_d2000_crb quark_se_c1000_ss_devboard