Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao environments: hidden assertions that ran inside the candidate's process now run in a trusted parent that reads observations from a low-UID worker; gold bytes are unchanged. 9 were excluded for scope (ROS2, oversize, security core), 3 are blocked because the source requires an exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6. Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the first 100 landed in 2f32624d. Claude reviewed 74/74 USABLE against current digests, canonical families, no ID or source overlap with main, plain public docs. Only the new task directories are taken; the stale pre-batch-20 copies of concur-release-capsule and resource-temp-link on the batch 21 branch and both branches' progress notes stay as main has them. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
3.3 KiB
C
158 lines
3.3 KiB
C
#include "adc.h"
|
|
|
|
static int width(const Adc *adc)
|
|
{
|
|
int result = 0;
|
|
int i;
|
|
for (i = 0; i < 8; ++i) {
|
|
result += (adc->mask >> i) & 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static int channel_for(const Adc *adc, int ordinal)
|
|
{
|
|
int target = ordinal % width(adc);
|
|
int channel;
|
|
for (channel = 0; channel < 8; ++channel) {
|
|
if ((adc->mask >> channel) & 1) {
|
|
if (target == 0) {
|
|
return channel;
|
|
}
|
|
--target;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void adc_reset(Adc *adc)
|
|
{
|
|
adc->vref = 3300000;
|
|
adc->mask = 255;
|
|
adc->rounds = 1;
|
|
adc->active = 0;
|
|
adc->used = 0;
|
|
}
|
|
|
|
int adc_encode(int channel, AdcFrame *out)
|
|
{
|
|
if (channel < 0 || channel >= 8) {
|
|
return ADC_RANGE;
|
|
}
|
|
*out = (AdcFrame){1, 128 + channel * 16, 0};
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_decode(int b0, int b1, int b2, int *out)
|
|
{
|
|
if (b0 < 0 || b0 > 255 || b1 < 0 || b1 > 255 || b2 < 0 || b2 > 255) {
|
|
return ADC_RANGE;
|
|
}
|
|
*out = (b1 % 4) * 256 + b2;
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_config(Adc *adc, int vref, int mask, int rounds)
|
|
{
|
|
if (vref < 1 || vref > 5000000 || mask < 1 || mask > 255 || rounds < 1 || rounds > 32) {
|
|
return ADC_RANGE;
|
|
}
|
|
if (adc->active) {
|
|
return ADC_BUSY;
|
|
}
|
|
adc->vref = vref;
|
|
adc->mask = mask;
|
|
adc->rounds = rounds;
|
|
adc->used = 0;
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_start(Adc *adc)
|
|
{
|
|
if (adc->active) {
|
|
return ADC_BUSY;
|
|
}
|
|
adc->used = 0;
|
|
adc->active = 1;
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_tx(const Adc *adc, AdcFrame *out)
|
|
{
|
|
if (!adc->active) {
|
|
return ADC_IDLE;
|
|
}
|
|
return adc_encode(channel_for(adc, adc->used), out);
|
|
}
|
|
|
|
int adc_feed(Adc *adc, int b0, int b1, int b2)
|
|
{
|
|
int value;
|
|
int result = adc_decode(b0, b1, b2, &value);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
if (!adc->active) {
|
|
return ADC_IDLE;
|
|
}
|
|
adc->samples[adc->used++] = value;
|
|
adc->active = adc->used != width(adc) * adc->rounds;
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_abort(Adc *adc)
|
|
{
|
|
if (!adc->active) {
|
|
return ADC_IDLE;
|
|
}
|
|
adc->active = 0;
|
|
return ADC_OK;
|
|
}
|
|
|
|
int adc_read(const Adc *adc, int channel, AdcReading *out)
|
|
{
|
|
int i;
|
|
AdcReading result = {0, 0, 1023, 0, 0, 0};
|
|
if (channel < 0 || channel > 7) {
|
|
return ADC_RANGE;
|
|
}
|
|
for (i = 0; i < adc->used; ++i) {
|
|
int value;
|
|
if (channel_for(adc, i) != channel) {
|
|
continue;
|
|
}
|
|
value = adc->samples[i];
|
|
++result.count;
|
|
result.sum += value;
|
|
if (value < result.minimum) {
|
|
result.minimum = value;
|
|
}
|
|
if (value > result.maximum) {
|
|
result.maximum = value;
|
|
}
|
|
result.last = value;
|
|
}
|
|
if (!result.count) {
|
|
return ADC_EMPTY;
|
|
}
|
|
result.microvolts = (int)(((uint64_t)result.sum * adc->vref) / (1023u * result.count));
|
|
*out = result;
|
|
return ADC_OK;
|
|
}
|
|
|
|
void adc_status(const Adc *adc, AdcStatus *out)
|
|
{
|
|
*out = (AdcStatus){adc->vref, adc->mask, adc->rounds, adc->active,
|
|
adc->active ? channel_for(adc, adc->used) : -1,
|
|
adc->used / width(adc), adc->used};
|
|
}
|
|
|
|
int adc_clear(Adc *adc)
|
|
{
|
|
if (adc->active) {
|
|
return ADC_BUSY;
|
|
}
|
|
adc->used = 0;
|
|
return ADC_OK;
|
|
}
|