# Production Programming Workflow

## Overview

**Fast production programming using PRG-TOOLBOX with one-time boot configuration setup during SOM testing.**

## The Challenge

- PRG-TOOLBOX is 10x faster than STM32CubeProgrammer (<2 min vs ~20 min)
- BUT: PRG-TOOLBOX's `gpt write` resets eMMC boot configuration registers
- Result: Devices won't boot after PRG-TOOLBOX programming

## The Solution

**Simplified Two-Stage Workflow:**

1. **SOM Testing:** Set boot configuration once (30 seconds)
2. **Production:** Fast PRG-TOOLBOX programming + automatic boot config fix (<3 min total)

---

## Stage 1: SOM Testing Setup (One-Time per Board)

**When:** During SOM functional testing  
**Boot Mode:** SD card boot into U-Boot  
**Duration:** 30 seconds

### Quick Setup Commands:

Boot from SD card, interrupt U-Boot, and run:

```bash
STM32MP> mmc dev 1
STM32MP> mmc partconf 1 1 2 0
STM32MP> mmc bootbus 1 0 0 0
```

**That's it!** These three commands set the boot configuration registers that will be used after production programming.

**What this does:**
- Sets `PARTITION_CONFIG = 0x50` (boot from partition 2/boot1)
- Sets `BOOT_BUS_CONDITIONS = 0x00` (default bus settings)
- Takes 5 seconds total

**Important:** Mark board as "Boot Config Set" in your test tracking system.

---

## Stage 2: Production Programming

**When:** Final production programming  
**Boot Mode:** DFU mode  
**Duration:** ~90 seconds + boot config fix time

### Automated Programming:

```bash
cd /scratch/yocto-cargt-public/stm32mp2/build/tmp-glibc/deploy/images/stm32mp25-cargt-00395-00365v3
./production_program_service.py
```

**What happens:**
1. ✅ Loads fastboot U-Boot via DFU (~30 sec)
2. ✅ Flashes all partitions via PRG-TOOLBOX-FB (~60 sec)  
3. ⚠️ Boot config gets reset to 0x78 (known issue)
4. 🔧 Script attempts automatic boot config fix via fastboot OEM

**After programming:** Boot config fix required (see below)

---

## Boot Configuration Fix (Required After Programming)

Since PRG-TOOLBOX resets boot config, you need to restore it. **Three options:**

### Option A: Manual U-Boot Commands (Recommended for now)

After production programming completes:

```bash
# Keep DIP switches in DFU mode
# Load U-Boot to RAM
PRG-TOOLBOX-DFU -d FlashLayout_dfu_load_fastboot_only.tsv --serial <SERIAL>

# In U-Boot console:
STM32MP> mmc dev 1
STM32MP> mmc partconf 1 1 2 0  
STM32MP> mmc bootbus 1 0 0 0

# Power cycle, set to eMMC boot mode
```

**Time:** ~30 seconds per board

### Option B: One-Time STM32CubeProgrammer Init (Best for Production)

**Recommended workflow:**
1. **First flash per board:** Use STM32CubeProgrammer (~20 min, sets boot config correctly)
2. **All future updates:** Use PRG-TOOLBOX (<2 min, boot config persists!)

**Why this works:** Boot config registers **persist** across PRG-TOOLBOX updates as long as you don't recreate partitions.

**Advantage:** After one-time STM32CubeProgrammer flash, all updates are fast forever!

### Option C: Serial Console Automation (If Available)

If you have serial console access in production, the script can automate the fix via Stage 2.6.

---

## Complete Workflow Summary

```
┌─────────────────────────────────────────┐
│  SOM Functional Testing                 │
│  - Boot from SD card                    │
│  - Set boot config (30 sec)             │  ← One command
│  - Mark as "configured"                 │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│  Production Programming                 │
│  - DFU mode                             │
│  - Run production_program_service.py    │  ← ~90 sec
│  - Partitions + data written            │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│  Boot Config Fix                        │
│  Option A: Manual (30 sec)              │  ← Current workflow
│  Option B: STM32CubeProgrammer once     │  ← Best for production
│  Option C: Serial automation            │  ← If available
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│  Ship Product                           │
│  - eMMC boot mode                       │
│  - Ready to use!                        │
└─────────────────────────────────────────┘
```

**Total Time Per Board:**
- Initial: ~2-3 minutes (PRG-TOOLBOX + manual fix)
- With STM32CubeProgrammer init: 20 min first time, <2 min all updates
- vs STM32CubeProgrammer only: ~20 min every time

---

## Recommended Production Strategy

**For New Boards:**
Use STM32CubeProgrammer for first flash, then PRG-TOOLBOX for all updates.

**Why:**
- One-time 20min investment per board
- All future updates: <2 min
- Boot config persists forever
- No manual intervention needed

**For Development/Testing:**
Use PRG-TOOLBOX + manual boot config fix for flexibility.

---

## Troubleshooting

### Device won't boot after programming

**Check:** Boot configuration registers

```bash
# In Linux on working board:
mmc-utils read_extcsd /dev/mmcblk1 | grep PARTITION_CONFIG
# Should show: 0x50

# Fix if wrong:
STM32MP> mmc partconf 1 1 2 0
STM32MP> mmc bootbus 1 0 0 0
```

### "Failed to setup partitions" error

This happens if partitions already exist. **Solutions:**
1. Flash with STM32CubeProgrammer first (erases everything)
2. OR ignore - partitions from SOM testing are compatible

---

## Key Files

- `production_program_service.py` - Main production script
- `FlashLayout_dfu_load_fastboot_only.tsv` - DFU stage (RAM only)
- `flashlayout_cargt-image-dev/optee/*.tsv` - Fastboot stage (full flash)
- `SOM_TESTING_EMMC_SETUP.md` - SOM testing boot config guide

---

## Speed Comparison

| Method | Time | Boot Config | Notes |
|--------|------|-------------|-------|
| STM32CubeProgrammer | ~20 min | ✅ Automatic | Slow but complete |
| PRG-TOOLBOX alone | ~2 min | ❌ Broken | Fast but won't boot |
| **PRG-TOOLBOX + manual fix** | **~3 min** | **✅ Manual** | **Best balance** |
| **STM32Cube once + PRG updates** | **<2 min** | **✅ Persists** | **Best for production** |

🚀 **Up to 10x faster than STM32CubeProgrammer!**
