Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions lib/i2c.toit
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ CONTROLLER-RESULT-OK_ ::= 0
CONTROLLER-RESULT-NACK_ ::= 1
CONTROLLER-RESULT-TIMEOUT_ ::= 2

validate-target-address_ address/int address-bit-size/int broadcast/bool -> none:
if address-bit-size != 7 and address-bit-size != 10: throw "INVALID_ARGUMENT"
limit := (1 << address-bit-size) - 1
if not 0 <= address <= limit: throw "INVALID_ARGUMENT"
if broadcast and address-bit-size == 10: throw "INVALID_ARGUMENT"

/**
An addressable I2C target.

Expand Down Expand Up @@ -117,10 +123,7 @@ class Target:
--default-response/ByteArray?=null
--pull-up/bool=false
--broadcast/bool=false:
if address-bit-size != 7 and address-bit-size != 10: throw "INVALID_ARGUMENT"
limit := (1 << address-bit-size) - 1
if not 0 <= address <= limit: throw "INVALID_ARGUMENT"
if broadcast and address-bit-size == 10: throw "INVALID_ARGUMENT"
validate-target-address_ address address-bit-size broadcast
if send-buffer-size <= 0 or receive-buffer-size <= 0: throw "INVALID_ARGUMENT"
response := default-response or ByteArray MAX-DEFAULT-RESPONSE-SIZE --initial=0xff
if response.size == 0 or response.size > MAX-DEFAULT-RESPONSE-SIZE:
Expand Down Expand Up @@ -348,10 +351,7 @@ class RegisterTarget:
--receive-buffer-size/int=DEFAULT-TARGET-BUFFER-SIZE
--pull-up/bool=false
--broadcast/bool=false:
if address-bit-size != 7 and address-bit-size != 10: throw "INVALID_ARGUMENT"
address-limit := (1 << address-bit-size) - 1
if not 0 <= address <= address-limit: throw "INVALID_ARGUMENT"
if broadcast and address-bit-size == 10: throw "INVALID_ARGUMENT"
validate-target-address_ address address-bit-size broadcast
if register-address-byte-size != 1 and register-address-byte-size != 2: throw "INVALID_ARGUMENT"
register-limit := 1 << (register-address-byte-size * 8)
if not 0 < register-count <= register-limit: throw "INVALID_ARGUMENT"
Expand Down Expand Up @@ -563,9 +563,18 @@ class Bus:
test address --timeout-ms/int=100 -> bool:
if not 0 <= address <= 0x7f: throw "INVALID_ARGUMENT"
if timeout-ms <= 0: throw "INVALID_ARGUMENT"
return perform-controller-operation_
(: i2c-bus-probe_ resource_ address timeout-ms)
(: i2c-bus-probe-finish_ resource_)
result := false
error := catch --unwind=(: it != DEADLINE-EXCEEDED-ERROR):
result = with-timeout --ms=timeout-ms:
perform-controller-operation_
(: i2c-bus-probe_ resource_ address timeout-ms)
(: i2c-bus-probe-finish_ resource_)
if error:
// An earlier caller deadline is automatically rethrown by 'catch'. A
// deadline caught here was installed for this probe and is equivalent
// to the driver's hardware timeout.
return false
return result

/**
Closes this I2C bus.
Expand Down
8 changes: 7 additions & 1 deletion lib/spi.toit
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,11 @@ class Bus:
cycles before the first clock edge. ESP-IDF only supports this option for
half-duplex transactions, except for a limited one-cycle case on the
classic ESP32. $cs-hold-cycles keeps CS active after the last clock edge.
Both values must be between 0 and 16.
Setup must be between 0 and 16. Hold must be between 0 and 16, except on the
classic ESP32 where the maximum is 15.

On ESP32, a bus configured with only one of `mosi` and `miso` registers its
devices as half-duplex. A bus with both data pins remains full-duplex.

The bus retains the returned device until either the device or the bus is
explicitly closed. Dropping the last application reference does not free a
Expand All @@ -600,6 +604,8 @@ class Bus:
if mode < 0 or mode > 3: throw "Argument Error"
if not 0 <= cs-setup-cycles <= 16: throw "OUT_OF_RANGE"
if not 0 <= cs-hold-cycles <= 16: throw "OUT_OF_RANGE"
if system.architecture == system.ARCHITECTURE-ESP32 and cs-hold-cycles == 16:
throw "OUT_OF_RANGE"
cs-num := gpio.to-pin-num_ cs
dc-num := gpio.to-pin-num_ dc
// For a deprecated gpio.Pin the dc pin is configured here; for the new
Expand Down
Loading
Loading