@startuml

!include puml-theme-xmos.puml

participant Host as H
participant Device as D
H -> D : DFU_DETACH
D -> D : Reboot in DFU mode
H -> D : DFU_DNLOAD (BlockNum=0, Size>0)
D -> D : Save block 0 data.\nStart flash erase
loop while state returned is dfuDNBUSY
    H -> D : DFU_GETSTATUS
    D -> D : Continue flash erase for a fixed period
    alt Flash erase not complete
        D --> H : State=dfuDNBUSY
    else Flash erase complete
        D -> D : Write block 0 to flash
        D --> H : State=dfuDNLOAD-IDLE
    end
end

loop until image is complete: n from 1 to N-1
    H -> D : DFU_DNLOAD (BlockNum=n, Size>0)
    D -> D : Write block n to flash
    H -> D : DFU_GETSTATUS
    D --> H : State=dfuDNLOAD-IDLE
end
H -> D : DFU_DNLOAD (BlockNum=N, Size=0)

H -> D : DFU_GETSTATUS
D --> H : State=dfuIDLE
H -> D : DFU_DETACH
D -> D : Boot in runtime mode\nfrom the upgrade image
@enduml
