// PicoC: adc_insertsamples(id, arr, idx, count); // Function parameters: // 1. id - ADC channel ID. // 2. arr - array to write samples to. Values at arr[idx] // to arr[idx + count -1] will be overwritten with samples // (or 0 if not enough samples are available). // 3. idx - first index to use in the array for writing // samples. // 4. count - number of samples to return. If not enough // samples are available (after blocking, if enabled) // remaining values will be 0; static void adc_insertsamples(pstate *p, val *r, val **param, int n) { unsigned id, i, startidx; int *arr; u16 bcnt, count; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); arr = param[1]->Val->Pointer; startidx = param[2]->Val->UnsignedInteger; if (startidx < 0) return pmod_error("idx must be >= 0"); count = param[3]->Val->UnsignedInteger; if (count == 0) return pmod_error("count must be > 0"); bcnt = adc_wait_samples(id, count); for (i = startidx; i < (count + startidx); i++) { if (i < bcnt + startidx) arr[i] = adc_get_processed_sample(id); else // zero-out values where we don't have enough samples arr[i] = 0; } }
// Helper to get timeout and timer_id values. void cmn_get_timeout_data(timer_data_type *timeout, unsigned *timer_id, val **param, int timeout_index, int timer_id_index) { *timeout = param[timeout_index]->Val->UnsignedLongInteger; if (*timeout < 0 || *timeout > PLATFORM_TIMER_INF_TIMEOUT) pmod_error("invalid timeout value"); *timer_id = param[timer_id_index]->Val->UnsignedInteger; if (*timer_id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available()) pmod_error("the system timer is not implemented on this platform"); }
// PicoC: adc_sample(id, count); // In PicoC, you could write an array and // use an iteration to init and setup a // list of channels. static void adc_sample(pstate *p, val *r, val **param, int n) { unsigned id, count; int res; count = param[1]->Val->UnsignedInteger; if ((count == 0) || count & (count - 1)) return pmod_error("count must be power of 2 and > 0"); id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) return pmod_error("sampling setup failed"); platform_adc_start_sequence(); r->Val->Integer = res; }
// PicoC: adc_setsmoothing(id, length); static void adc_setsmoothing(pstate *p, val *r, val **param, int n) { unsigned id, length, res; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); length = param[1]->Val->UnsignedInteger; if (!(length & (length - 1))) { res = platform_adc_set_smoothing(id, length); if (res == PLATFORM_ERR) { return pmod_error("Buffer allocation failed."); } else { r->Val->UnsignedInteger = res; return; } } else { return pmod_error("Length must be power of 2"); } }
// PicoC: realclock = adc_setclock(id, freq, timer_id); static void adc_setclock(pstate *p, val *r, val **param, int n) { s32 sfreq; // signed version for negative checking u32 freq; unsigned id, timer_id = 0; id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); sfreq = param[1]->Val->LongInteger; if (sfreq < 0) return pmod_error("frequency must be 0 or positive"); freq = (u32) sfreq; if (freq > 0) { timer_id = param[2]->Val->UnsignedInteger; MOD_CHECK_ID(timer, timer_id); MOD_CHECK_RES_ID(adc, id, timer, timer_id); } platform_adc_set_timer(id, timer_id); freq = platform_adc_set_clock(id, freq); r->Val->UnsignedLongInteger = freq; }