amos 73a5240295 Batches 21 and 18: add 74 calibrated tasks (40 Bos redo, 34 HWE code-gen)
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>
2026-11-02 10:23:12 +08:00

228 lines
4.5 KiB
C

#include "adc.h"
static int byte_in_range(int value)
{
return value >= 0 && value <= 255;
}
static int first_selected(int mask)
{
int channel;
for (channel = 0; channel < 8; ++channel) {
if ((mask & (1 << channel)) != 0) {
return channel;
}
}
return -1;
}
static int following_selected(int mask, int current)
{
int channel;
for (channel = current + 1; channel < 8; ++channel) {
if ((mask & (1 << channel)) != 0) {
return channel;
}
}
return -1;
}
static void reset_bucket(AdcBucket *bucket)
{
bucket->count = 0;
bucket->sum = 0;
bucket->minimum = 0;
bucket->maximum = 0;
bucket->last = 0;
}
static void reset_results(Adc *adc)
{
int channel;
for (channel = 0; channel < 8; ++channel) {
reset_bucket(&adc->channel[channel]);
}
adc->cursor = -1;
adc->completed = 0;
adc->frames = 0;
}
void adc_reset(Adc *adc)
{
adc->vref = 3300000;
adc->mask = 255;
adc->rounds = 1;
adc->active = 0;
reset_results(adc);
}
int adc_encode(int channel, AdcFrame *out)
{
if (channel < 0 || channel > 7) {
return ADC_RANGE;
}
out->b0 = 1;
out->b1 = (8 | channel) << 4;
out->b2 = 0;
return ADC_OK;
}
int adc_decode(int b0, int b1, int b2, int *out)
{
if (!byte_in_range(b0)) {
return ADC_RANGE;
}
if (!byte_in_range(b1)) {
return ADC_RANGE;
}
if (!byte_in_range(b2)) {
return ADC_RANGE;
}
*out = ((b1 & 3) << 8) | b2;
return ADC_OK;
}
int adc_config(Adc *adc, int vref, int mask, int rounds)
{
if (vref < 1 || vref > 5000000) {
return ADC_RANGE;
}
if (mask < 1 || mask > 255) {
return ADC_RANGE;
}
if (rounds < 1 || rounds > 32) {
return ADC_RANGE;
}
if (adc->active) {
return ADC_BUSY;
}
adc->vref = vref;
adc->mask = mask;
adc->rounds = rounds;
reset_results(adc);
return ADC_OK;
}
int adc_start(Adc *adc)
{
if (adc->active) {
return ADC_BUSY;
}
reset_results(adc);
adc->active = 1;
adc->cursor = first_selected(adc->mask);
return ADC_OK;
}
int adc_tx(const Adc *adc, AdcFrame *out)
{
if (!adc->active) {
return ADC_IDLE;
}
return adc_encode(adc->cursor, out);
}
static void append_sample(AdcBucket *bucket, int raw)
{
if (bucket->count == 0) {
bucket->minimum = raw;
bucket->maximum = raw;
} else {
if (raw < bucket->minimum) {
bucket->minimum = raw;
}
if (raw > bucket->maximum) {
bucket->maximum = raw;
}
}
bucket->count += 1;
bucket->sum += raw;
bucket->last = raw;
}
static void advance_scan(Adc *adc)
{
int next;
adc->frames += 1;
next = following_selected(adc->mask, adc->cursor);
if (next >= 0) {
adc->cursor = next;
return;
}
adc->completed += 1;
if (adc->completed == adc->rounds) {
adc->active = 0;
adc->cursor = -1;
return;
}
adc->cursor = first_selected(adc->mask);
}
int adc_feed(Adc *adc, int b0, int b1, int b2)
{
int raw;
int result = adc_decode(b0, b1, b2, &raw);
if (result != ADC_OK) {
return result;
}
if (!adc->active) {
return ADC_IDLE;
}
append_sample(&adc->channel[adc->cursor], raw);
advance_scan(adc);
return ADC_OK;
}
int adc_abort(Adc *adc)
{
if (!adc->active) {
return ADC_IDLE;
}
adc->active = 0;
adc->cursor = -1;
return ADC_OK;
}
int adc_read(const Adc *adc, int channel, AdcReading *out)
{
const AdcBucket *bucket;
uint64_t numerator;
uint64_t denominator;
if (channel < 0 || channel > 7) {
return ADC_RANGE;
}
bucket = &adc->channel[channel];
if (bucket->count == 0) {
return ADC_EMPTY;
}
out->count = bucket->count;
out->sum = bucket->sum;
out->minimum = bucket->minimum;
out->maximum = bucket->maximum;
out->last = bucket->last;
numerator = (uint64_t)(bucket->sum / bucket->count) * (uint64_t)adc->vref;
denominator = 1023;
out->microvolts = (int)(numerator / denominator);
return ADC_OK;
}
void adc_status(const Adc *adc, AdcStatus *out)
{
out->vref = adc->vref;
out->mask = adc->mask;
out->rounds = adc->rounds;
out->active = adc->active;
out->next_channel = adc->cursor;
out->completed_rounds = adc->completed;
out->frames = adc->frames;
}
int adc_clear(Adc *adc)
{
if (adc->active) {
return ADC_BUSY;
}
reset_results(adc);
return ADC_OK;
}