; PARAMETERS:
;     R6 = Status flags
;
; RETURN VALUES:
;     R6 = Updated status flags
;
; CLOBBERS:
;     R5
I2cWaitSclStretch:
                        ; Load the timeout (up to 20 ms) loop counter = configured number of us
                        ld          R5, #(-I2C_STRETCH_TIMEOUT_US)
/waitLoop:
                            ; If SCL is high, we're done
                            iobtst      #(AUXIO_I2C_SCL & 0x7), [#(IOP_AIODIO0_GPIODIN + (AUXIO_I2C_SCL >> 3))]
                            biob1       /done
                            ; Unroll the loop to 12 instructions = 1 per loop for faster response
                            iobtst      #(AUXIO_I2C_SCL & 0x7), [#(IOP_AIODIO0_GPIODIN + (AUXIO_I2C_SCL >> 3))]
                            biob1       /done
                            iobtst      #(AUXIO_I2C_SCL & 0x7), [#(IOP_AIODIO0_GPIODIN + (AUXIO_I2C_SCL >> 3))]
                            biob1       /done
                            iobtst      #(AUXIO_I2C_SCL & 0x7), [#(IOP_AIODIO0_GPIODIN + (AUXIO_I2C_SCL >> 3))]
                            biob1       /done
                            iobtst      #(AUXIO_I2C_SCL & 0x7), [#(IOP_AIODIO0_GPIODIN + (AUXIO_I2C_SCL >> 3))]
                            biob1       /done

                        add         R5, #1;
                        bnz         /waitLoop

/timeout:               ; Timeout has occurred, so OR it into the flags and bail out
                        or          R6, #0x0002

/done:                  ; Done
                        rts
