main
John Lauer
Publish assumption-based R11 assembly study, locked firmware and new review footage
d61d6cb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
/* Output-locked USB bring-up image. This is NOT the production hardware adapter.
* Never link the Nucleo board.c: its LED is our PA5 load-enable output. */
#include "stm32g0xx_hal.h"
#include "tusb.h"
#include "load_protocol.h"
#include "fan_input.h"
#include "fan_pwm.h"
#include "instrument_input.h"
#include "adc_input.h"
#include <stdio.h>
#include <math.h>
#if !defined(ADOM_BRINGUP_OUTPUT_LOCK) || ADOM_BRINGUP_OUTPUT_LOCK != 1
#error This bring-up adapter must remain output locked
#endif
#ifndef ADOM_LAB_BOOT_ID
#error Supply a lab-session boot identity; this is not production replay protection
#endif
bool load_clock_init(void);
char load_device_serial[33];
static load_core core;
static load_protocol protocol;
static char response[LOAD_REPLY_BYTES];
static bool initialized, connected;
/* Raw BSRR writes avoid HAL dependencies on the emergency path. */
static void hard_off(void *unused) {
(void)unused;
GPIOA->BSRR = (uint32_t)(GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_8) << 16;
load_fan_pwm_full(); /* reclaim GPIO even if a bench test previously enabled AF */
GPIOB->BSRR = (uint32_t)GPIO_PIN_3 << 16; /* assert clear: OPEN DRAIN */
}
static void safe_gpio_init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
hard_off(NULL); /* preload LOW before switching modes */
GPIO_InitTypeDef gpio = {0};
gpio.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_8;
gpio.Mode = GPIO_MODE_OUTPUT_PP; gpio.Pull = GPIO_NOPULL;
gpio.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &gpio);
gpio.Pin = GPIO_PIN_3; gpio.Mode = GPIO_MODE_OUTPUT_OD;
HAL_GPIO_Init(GPIOB, &gpio); /* never drive the fault-clear net high */
gpio.Pin = GPIO_PIN_0; gpio.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOB, &gpio); /* divided USB VBUS, external divider */
}
void load_fatal(void) {
__disable_irq(); hard_off(NULL);
for (;;) { __WFI(); }
}
void HardFault_Handler(void) { load_fatal(); }
void NMI_Handler(void) { load_fatal(); }
void SysTick_Handler(void) { HAL_IncTick(); }
uint32_t tusb_time_millis_api(void) { return HAL_GetTick(); }
void USB_UCPD1_2_IRQHandler(void) { tusb_int_handler(0, true); }
void _init(void) {}
static void disconnect(void) {
hard_off(NULL);
if (initialized) load_protocol_disconnect(&protocol);
connected = false;
}
void tud_umount_cb(void) { disconnect(); }
void tud_suspend_cb(bool remote_wakeup_en) { (void)remote_wakeup_en; disconnect(); }
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
(void)itf; (void)rts; if (!dtr) disconnect();
}
int main(void) {
safe_gpio_init(); /* do not wait for oscillator or USB setup to deassert enable */
if (HAL_Init() != HAL_OK || !load_clock_init()) load_fatal();
if (!load_fan_input_init()) load_fatal();
(void)load_adc_input_init(); /* nominal ADC; no board calibration */
(void)load_instrument_input_init(); /* stay diagnostic/OFF if devices absent */
__HAL_RCC_SYSCFG_CLK_ENABLE(); __HAL_RCC_PWR_CLK_ENABLE();
GPIO_InitTypeDef gpio = {0};
gpio.Pin = GPIO_PIN_11 | GPIO_PIN_12;
gpio.Mode = GPIO_MODE_INPUT; gpio.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &gpio);
__HAL_RCC_USB_CLK_ENABLE(); HAL_PWREx_EnableVddUSB();
snprintf(load_device_serial, sizeof(load_device_serial), "ADOM%08lX%08lX%08lX",
(unsigned long)HAL_GetUIDw0(), (unsigned long)HAL_GetUIDw1(), (unsigned long)HAL_GetUIDw2());
load_init(&core, HAL_GetTick());
if (!load_protocol_init(&protocol, &core, load_device_serial, ADOM_LAB_BOOT_ID, hard_off, NULL)) load_fatal();
initialized = true;
tusb_rhport_init_t init = {.role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_FULL};
if (!tusb_init(0, &init)) load_fatal();
bool attached = false;
tud_disconnect();
for (;;) {
hard_off(NULL); /* unconditional: no path in this image can enable load */
bool vbus = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == GPIO_PIN_SET;
if (vbus != attached) {
attached = vbus;
if (vbus) tud_connect(); else { tud_disconnect(); disconnect(); }
}
tud_task();
load_instrument_input_poll();
load_adc_input_poll();
bool online = vbus && tud_cdc_connected();
if (!online && connected) disconnect();
connected = online;
load_sample sample = {.now_ms = HAL_GetTick(), .volts = NAN, .amps = NAN,
.case_c = NAN, .case_margin_c = NAN, .temperature_qualified = false,
.fan_rpm = NAN, .assembly_qualified = false,
.sensors_valid = false, .hw_ok = false, .latch_q = false};
bool current_ok=load_instrument_input_current(&sample.amps,&sample.sample_ms);
uint32_t adc_stamp=0;
bool adc_ok=load_adc_input_read(&sample.volts,&sample.case_c,&adc_stamp);
uint32_t sampled_now=HAL_GetTick();
if(adc_ok&&(!current_ok||(uint32_t)(sampled_now-adc_stamp)>(uint32_t)(sampled_now-sample.sample_ms)))sample.sample_ms=adc_stamp;
/* Numeric diagnostics are nominal; sensors_valid stays false until actual
board/sensor calibration and acquisition timing are qualified. */
sample.fan_rpm_valid=load_fan_input_read(&sample.fan_rpm,&sample.last_tach_ms);
sample.now_ms=HAL_GetTick();
load_protocol_poll(&protocol, &sample);
/* Bounded ingress; never consume a request without room for its full reply. */
for (unsigned n = 0; online && n < 64 && tud_cdc_available() &&
tud_cdc_write_available() >= LOAD_REPLY_BYTES; ++n) {
uint8_t byte;
if (tud_cdc_read(&byte, 1) != 1) break;
size_t len = load_protocol_byte(&protocol, byte, &sample, response, sizeof(response));
if (len && tud_cdc_write(response, (uint32_t)len) != len) disconnect();
}
tud_cdc_write_flush();
}
}