ShiboSoftwareDev/am69-cm5

A 55×40 mm 10-layer CM5 carrier board integrating an AM69A98 SoC, 8‑GiB LPDDR4, 32‑GB eMMC, CM5 connectors, clocking, extensive decoupling/EMI filtering, and HDI DDR/ground escape routing.

Version
0.0.7-engineering-draft-20261107-manual-ddr0
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone ShiboSoftwareDev/am69-cm5  # download the package files into ./am69-cm5
tsci add ShiboSoftwareDev/am69-cm5    # add it to your project as @tsci/ShiboSoftwareDev.am69-cm5

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

firmware/boot-controller/main.c

/* AM69 always-on controller. Board pin contract: BootController.tsx. */
#include "vendor/stm32g031xx.h"
#include "policy.h"
#include "protocol.h"
#include "pd_contract.h"

uint32_t SystemCoreClock=16000000;
static volatile uint32_t milliseconds,tach_edges;
static volatile uint8_t rx[64],rx_write,rx_read;
static volatile bool rx_error;
static EcPolicy policy;
static EcProtocol protocol;
static bool runtime_active,straps_active;

static void mode(GPIO_TypeDef *port,unsigned pin,unsigned value) {
  port->MODER=(port->MODER&~(3u<<(2*pin)))|(value<<(2*pin));
  port->PUPDR&=~(3u<<(2*pin));
}
static void alternate(GPIO_TypeDef *port,unsigned pin,unsigned af) {
  unsigned shift=(pin&7)*4;
  port->AFR[pin/8]=(port->AFR[pin/8]&~(15u<<shift))|(af<<shift);
  mode(port,pin,2);
}
static void delay_us(unsigned us) { for(volatile unsigned n=0;n<us*4;n++)__NOP(); }
#if EC_PI_BOARD
// Dedicated G031 PB8=SCL, PB3=SDA; never share boot, UART or fan pins.
static PdQualification pd_qualification;
static bool pd_allowed;
static uint32_t pd_sample_at;
static void pd_line(unsigned pin,bool high) {
  GPIOB->BSRR=1u<<(pin+(high?0:16));
}
static bool pd_clock(bool high) {
  pd_line(8,high);
  if(high){unsigned budget=200;while(!(GPIOB->IDR&(1u<<8))&&--budget)delay_us(5);if(!budget)return false;}
  delay_us(7);return true;
}
static void pd_stop(void) {
  pd_line(3,false);(void)pd_clock(true);pd_line(3,true);delay_us(7);
}
static bool pd_start(void) {
  pd_line(3,true);if(!pd_clock(true)||!(GPIOB->IDR&(1u<<3)))return false;
  pd_line(3,false);delay_us(7);return pd_clock(false);
}
static bool pd_write_byte(uint8_t b) {
  for(unsigned i=0;i<8;i++) {
    pd_line(3,!!(b&0x80u));b<<=1;
    if(!pd_clock(true)||!pd_clock(false))return false;
  }
  pd_line(3,true);if(!pd_clock(true))return false;
  bool ack=!(GPIOB->IDR&(1u<<3));(void)pd_clock(false);return ack;
}
static bool pd_read_reg(uint8_t reg,uint8_t *bytes,unsigned count) {
  bool ok=pd_start()&&pd_write_byte(0x50)&&pd_write_byte(reg)&&pd_start()&&pd_write_byte(0x51);
  for(unsigned i=0;ok&&i<count;i++) {
    uint8_t b=0;pd_line(3,true);
    for(unsigned k=0;k<8;k++) {
      if(!pd_clock(true)){ok=false;break;}
      b=(uint8_t)((b<<1)|!!(GPIOB->IDR&(1u<<3)));(void)pd_clock(false);
    }
    if(!ok)break;
    bytes[i]=b;pd_line(3,i+1==count);ok=pd_clock(true)&&pd_clock(false);pd_line(3,true);
  }
  pd_stop();return ok;
}
static bool pd_read_word(uint8_t reg,uint32_t *out) {
  uint8_t b[4];if(!pd_read_reg(reg,b,4))return false;
  *out=(uint32_t)b[0]|((uint32_t)b[1]<<8)|((uint32_t)b[2]<<16)|((uint32_t)b[3]<<24);return true;
}
static void pd_init(void) {
  GPIOB->BSRR=(1u<<8)|(1u<<3)|(1u<<21); // Reset released (active high).
  GPIOB->OTYPER|=(1u<<8)|(1u<<3);
  mode(GPIOB,8,1);mode(GPIOB,3,1);mode(GPIOB,5,1);
  mode(GPIOB,1,0);mode(GPIOB,2,0);
  GPIOC->BSRR=1u<<30;mode(GPIOC,14,1); // Hardware pulldown also holds power off.
}
static void pd_poll(uint32_t now) {
  if((uint32_t)(now-pd_sample_at)<10)return;
  pd_sample_at=now;
  PdSnapshot s={.sink_enabled=!(GPIOB->IDR&(1u<<1))};
  s.transfer_ok=pd_read_reg(0x29,&s.pe_before,1)&&pd_read_word(0x91,&s.rdo_before)
    &&pd_read_reg(0x0e,&s.port,1)&&pd_read_reg(0x15,&s.typec,1)
    &&pd_read_reg(0x10,&s.monitor,1)&&pd_read_reg(0x70,&s.pdo_count,1)
    &&pd_read_word(0x85,&s.pdo1)&&pd_read_word(0x91,&s.rdo_after)&&pd_read_reg(0x29,&s.pe_after,1);
  s.sink_enabled=s.sink_enabled&&!(GPIOB->IDR&(1u<<1));
  pd_allowed=pd_qualify(&pd_qualification,&s,milliseconds);
  GPIOC->BSRR=1u<<(pd_allowed?14:30);
}
#endif
void SysTick_Handler(void) { ++milliseconds; }
void EXTI4_15_IRQHandler(void) {
  if(EXTI->FPR1&EXTI_FPR1_FPIF9){EXTI->FPR1=EXTI_FPR1_FPIF9;++tach_edges;}
}
void USART2_IRQHandler(void) {
  uint32_t f=USART2->ISR;
  if(f&(USART_ISR_ORE|USART_ISR_FE|USART_ISR_NE|USART_ISR_PE)) {
    USART2->ICR=USART_ICR_ORECF|USART_ICR_FECF|USART_ICR_NECF|USART_ICR_PECF;rx_error=true;
  }
  while(USART2->ISR&USART_ISR_RXNE_RXFNE) {
    uint8_t b=(uint8_t)USART2->RDR,next=(uint8_t)((rx_write+1)&63);
    if(next==rx_read)rx_error=true;else{rx[rx_write]=b;rx_write=next;}
  }
}
static void uart_stop(void) {
  NVIC_DisableIRQ(USART2_IRQn);USART2->CR1=0;
  rx_read=rx_write=0;protocol.used=0;rx_error=false;
  mode(GPIOA,2,3);mode(GPIOA,3,3);
}
static void uart_start(void) {
  GPIOA->OTYPER&=~((1u<<2)|(1u<<3));
  alternate(GPIOA,2,1);alternate(GPIOA,3,1);
  USART2->BRR=(SystemCoreClock+57600)/115200;
  USART2->ICR=USART_ICR_ORECF|USART_ICR_FECF|USART_ICR_NECF|USART_ICR_PECF;
  USART2->CR1=USART_CR1_UE|USART_CR1_TE|USART_CR1_RE|USART_CR1_RXNEIE_RXFNEIE;
  NVIC_ClearPendingIRQ(USART2_IRQn);NVIC_EnableIRQ(USART2_IRQn);
}
static void release_straps(void) {
  for(unsigned pin=0;pin<5;pin++)mode(GPIOA,pin,3);
  straps_active=false;
}
static void set_straps(EcBoot boot) {
  uint16_t mcu=ec_mcu_boot_word(boot);uint8_t main=ec_main_boot_word(boot);
  unsigned values=((mcu>>3)&7)|((unsigned)((main>>4)&1)<<3)|((unsigned)((main>>6)&1)<<4);
  GPIOA->OTYPER|=31u;
  GPIOA->BSRR=values|((31u&~values)<<16);
  for(unsigned pin=0;pin<5;pin++)mode(GPIOA,pin,1);
  straps_active=true;
}
static void fan(unsigned percent) {
  // PB7 AF2 is TIM17_CH1N. Complementary output inverts PWM1.
  TIM17->CCR1=(100u-percent)*640u/100u;
  alternate(GPIOB,7,2);
}
static void hardware_sync(void) {
  if(!policy.runtime_bus&&runtime_active){uart_stop();release_straps();runtime_active=false;}
  if(!policy.run) {
    // Release boot/UART drivers before the latch presets and CPU rails fall.
    if(!policy.straps&&straps_active)release_straps();
    GPIOA->BSRR=1u<<21;
  }
  if(policy.straps&&!straps_active)set_straps(policy.boot);
  if(policy.run)GPIOA->BSRR=1u<<5;
  if(policy.runtime_bus&&!runtime_active) {
    release_straps();
    // Explicit edge even at 100% fan duty: this latches runtime mux selection.
    GPIOB->BSRR=1u<<23;mode(GPIOB,7,1);delay_us(10);GPIOB->BSRR=1u<<7;delay_us(10);
    uart_start();runtime_active=true;
  }
  GPIOA->BSRR=policy.key_irq?1u<<28:1u<<12;
  fan(policy.fan_percent);
}
static bool adc_read(unsigned channel,uint16_t *value) {
  ADC1->ISR=ADC_ISR_CCRDY;ADC1->CHSELR=1u<<channel;
  unsigned budget=30000;while(!(ADC1->ISR&ADC_ISR_CCRDY)&&--budget){}
  if(!budget)return false;
  ADC1->ISR=ADC_ISR_EOC|ADC_ISR_EOS;ADC1->CR|=ADC_CR_ADSTART;
  budget=30000;while(!(ADC1->ISR&ADC_ISR_EOC)&&--budget){}
  if(!budget)return false;
  *value=(uint16_t)ADC1->DR;return true;
}
static bool adc_init(void) {
  ADC1->CR=ADC_CR_ADVREGEN;delay_us(30);ADC1->CFGR2=ADC_CFGR2_CKMODE_1;
  ADC1_COMMON->CCR=ADC_CCR_VREFEN;ADC1->SMPR=ADC_SMPR_SMP1;
  ADC1->CR|=ADC_CR_ADCAL;
  unsigned budget=30000;while((ADC1->CR&ADC_CR_ADCAL)&&--budget){}
  if(!budget)return false;
  delay_us(5);ADC1->ISR=ADC_ISR_ADRDY;ADC1->CR|=ADC_CR_ADEN;
  budget=30000;while(!(ADC1->ISR&ADC_ISR_ADRDY)&&--budget){}
  return budget!=0;
}
static bool supply(bool *por_high) {
  uint16_t ref,raw;
  if(!adc_read(13,&ref)||!ref||!adc_read(7,&raw))return false;
  uint32_t vdda=(uint32_t)*(const uint16_t *)0x1fff75aa*3000u/ref;
  if(vdda<2700||vdda>3600)return false;
  uint32_t por_mv=(uint32_t)raw*vdda/4095u;
  if(por_mv>=1200)*por_high=true;else if(por_mv<=800)*por_high=false;
  return true;
}
static bool tx(const uint8_t *bytes,unsigned count) {
  uint32_t began=milliseconds;
  for(unsigned i=0;i<count;i++) {
    while(!(USART2->ISR&USART_ISR_TXE_TXFNF))if((uint32_t)(milliseconds-began)>10)return false;
    USART2->TDR=bytes[i];
  }
  while(!(USART2->ISR&USART_ISR_TC))if((uint32_t)(milliseconds-began)>10)return false;
  return true;
}
static void watchdog_start(void) {
  RCC->CSR|=RCC_CSR_LSION;
  IWDG->KR=0xcccc;IWDG->KR=0x5555;IWDG->PR=3;IWDG->RLR=499;
  // Approximately 500 ms at nominal 32 kHz LSI; do not feed from interrupts.
  while(IWDG->SR){}IWDG->KR=0xaaaa;
}
static void hardware_init(void) {
  RCC->CR&=~RCC_CR_HSIDIV;SCB->VTOR=0x08000000;
  RCC->IOPENR|=RCC_IOPENR_GPIOAEN|RCC_IOPENR_GPIOBEN|RCC_IOPENR_GPIOCEN;
  RCC->APBENR1|=RCC_APBENR1_USART2EN;
  RCC->APBENR2|=RCC_APBENR2_ADCEN|RCC_APBENR2_TIM17EN|RCC_APBENR2_SYSCFGEN;
  // Shared package pads: select PA11/PA12, PB0, PB6, PB7 and PB9 only.
  SYSCFG->CFGR1&=~(SYSCFG_CFGR1_PA11_RMP|SYSCFG_CFGR1_PA12_RMP);
  for(unsigned pin=0;pin<16;pin++) {
    if(pin!=13&&pin!=14)mode(GPIOA,pin,3);
    mode(GPIOB,pin,3);mode(GPIOC,pin,3);
  }
  GPIOA->BSRR=(1u<<21)|(1u<<12);mode(GPIOA,5,1);
  GPIOA->OTYPER|=1u<<12;mode(GPIOA,12,1);
  mode(GPIOA,6,0);mode(GPIOA,11,0);
  mode(GPIOB,0,0);mode(GPIOB,6,0);mode(GPIOB,9,0);mode(GPIOC,15,0);
#if EC_PI_BOARD
  pd_init();
#endif
  TIM17->PSC=0;TIM17->ARR=639;TIM17->CCR1=0;
  TIM17->CCMR1=TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1PE;
  TIM17->CCER=TIM_CCER_CC1NE;TIM17->BDTR=TIM_BDTR_MOE;
  TIM17->EGR=TIM_EGR_UG;TIM17->CR1=TIM_CR1_ARPE|TIM_CR1_CEN;fan(100);
  EXTI->EXTICR[2]=(EXTI->EXTICR[2]&~EXTI_EXTICR3_EXTI9)|EXTI_EXTICR3_EXTI9_0;
  EXTI->FTSR1|=EXTI_FTSR1_FT9;EXTI->FPR1=EXTI_FPR1_FPIF9;EXTI->IMR1|=1u<<9;
  NVIC_EnableIRQ(EXTI4_15_IRQn);SysTick_Config(SystemCoreClock/1000);
}
typedef struct { bool value,candidate;uint32_t since; } Debounce;
static bool debounce(Debounce *d,bool raw,uint32_t now) {
  if(raw!=d->candidate){d->candidate=raw;d->since=now;}
  if((uint32_t)(now-d->since)>=20)d->value=raw;
  return d->value;
}
int main(void) {
  hardware_init();bool adc_ok=adc_init(),por=false;
  ec_init(&policy,EC_PI_BOARD,milliseconds);watchdog_start();
  Debounce button={0},sd={0},usb={0};uint32_t last=UINT32_MAX,rpm_at=0,old_edges=0;
  uint16_t rpm=0;
  for(;;) {
    uint32_t now=milliseconds;
    if(now==last)continue;
    last=now;
#if EC_PI_BOARD
    pd_poll(now);now=milliseconds;
#endif
    EcInputs in={.carrier_enable=!!(GPIOB->IDR&1u),
      .button_pressed=debounce(&button,!(GPIOA->IDR&(1u<<6)),now),
      .force_usb=debounce(&usb,!(GPIOA->IDR&(1u<<11)),now),
      .sd_present=debounce(&sd,!(GPIOC->IDR&(1u<<15)),now),
      .host_off=!!(GPIOB->IDR&(1u<<6)),.aon_ok=adc_ok&&supply(&por),.por_high=por};
    // Read ADC before assigning POR: C initializer evaluation order is unspecified.
#if EC_PI_BOARD
    in.carrier_enable=in.carrier_enable&&pd_allowed;
#endif
    in.por_high=por;ec_step(&policy,&in,now);hardware_sync();
    if((uint32_t)(now-rpm_at)>=1000) {
      uint32_t edges=tach_edges,dt=now-rpm_at;
      uint32_t measured=(edges-old_edges)*30000u/dt; // Standard two tach pulses/rev.
      rpm=(uint16_t)(measured>65535?65535:measured);old_edges=edges;rpm_at=now;
    }
    if(runtime_active) {
      if(rx_error){__disable_irq();rx_read=rx_write;rx_error=false;__enable_irq();protocol.used=0;policy.fan_command_valid=false;}
      unsigned budget=64;
      while(rx_read!=rx_write&&budget--&&runtime_active) {
        uint8_t byte=rx[rx_read],response[8];rx_read=(uint8_t)((rx_read+1)&63);
        if(ec_protocol_feed(&protocol,&policy,byte,now,rpm,response)) {
          if(!tx(response,8))policy.fan_command_valid=false;
          hardware_sync();
        }
      }
    }
    IWDG->KR=0xaaaa;
  }
}
void Default_Handler(void) {
  __disable_irq();USART2->CR1=0;
  for(unsigned pin=0;pin<5;pin++)mode(GPIOA,pin,3);
  GPIOA->BSRR=1u<<21;for(;;){} // Hardware watchdog resets with RUN pulled low.
}
extern uint32_t _estack,_sidata,_sdata,_edata,_sbss,_ebss;
void Reset_Handler(void) {
  uint32_t *src=&_sidata;
  for(uint32_t *dst=&_sdata;dst<&_edata;)*dst++=*src++;
  for(uint32_t *dst=&_sbss;dst<&_ebss;)*dst++=0;
  (void)main();Default_Handler();
}
__attribute__((used,section(".isr_vector")))
const uintptr_t vectors[48]={
  [0]=(uintptr_t)&_estack,[1]=(uintptr_t)Reset_Handler,
  [2]=(uintptr_t)Default_Handler,[3]=(uintptr_t)Default_Handler,
  [11]=(uintptr_t)Default_Handler,[14]=(uintptr_t)Default_Handler,[15]=(uintptr_t)SysTick_Handler,
  [16+EXTI4_15_IRQn]=(uintptr_t)EXTI4_15_IRQHandler,[16+USART2_IRQn]=(uintptr_t)USART2_IRQHandler,
};
void *memcpy(void *dst,const void *src,unsigned n) {
  unsigned char *d=dst;const unsigned char *s=src;while(n--)*d++=*s++;return dst;
}
void *memset(void *dst,int c,unsigned n) { unsigned char *d=dst;while(n--)*d++=(unsigned char)c;return dst; }