From f31b02e3f8a6a5ad28781a2b08312811b177ba63 Mon Sep 17 00:00:00 2001 From: Martinix75 Date: Mon, 20 Jan 2025 16:37:49 +0100 Subject: [PATCH 1/3] Check if the number is> = 0 on the USB buffer --- src/picostdlib/pico/stdio.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/picostdlib/pico/stdio.nim b/src/picostdlib/pico/stdio.nim index 9d0d1a6..e92f6eb 100644 --- a/src/picostdlib/pico/stdio.nim +++ b/src/picostdlib/pico/stdio.nim @@ -175,7 +175,7 @@ proc print*(s: string) = proc stdinReadLine*(echoInput: bool = true): string = while true: let res = getcharTimeoutUs(100_000) - if res != PicoErrorTimeout.int: + if res >= 0 and res != PicoErrorTimeout.int: let character = res.char if character == '\r': echo "" From 4a9e5a0e54cdc0e9e3502a7793361c9ebefe2e37 Mon Sep 17 00:00:00 2001 From: Martinix75 Date: Wed, 22 Jan 2025 12:45:52 +0100 Subject: [PATCH 2/3] Small changes of I2C utility for Nim 1 -Changed by template -> proc "ic2setupnim" an detiturn PRT i2cInst 2 -Address in the procedure is now Uint8 (and not i2c Address) --- src/picostdlib/hardware/i2c.nim | 82 +++++++++++++++++---------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/picostdlib/hardware/i2c.nim b/src/picostdlib/hardware/i2c.nim index 689c129..fd1935f 100644 --- a/src/picostdlib/hardware/i2c.nim +++ b/src/picostdlib/hardware/i2c.nim @@ -216,62 +216,64 @@ proc getDreq*(i2c: ptr I2cInst; isTx: bool): cuint {.importc: "i2c_get_dreq".} {.pop.} -## Nim helpers - -template i2cSetupNim*(blokk: ptr I2cInst, pSda, pScl: Gpio, freq: uint, pull = true) = +## Nim helpers rev 0.2.0 +type + Pull = enum + Up, Disable + +proc i2cSetupNim*(port: ptr I2cInst; + pSda, pScl: uint8; + freq: uint; + pull = Pull.Up): ptr I2cInst = #sugar setup for i2c: - #blokk= block i2c0 / i2c1 (see pinout) - #pSda/pScl = the pins you want use (ex: 2.Gpio, 3.Gpio) I do not recommend the use of 0.Gpio, 1.Gpio - #freq = is the working frequency of the i2c device (see device manual; ex: 100000) - #pull = use or not to use pullup (default = true) - var i2cx = blokk - discard i2cx.init(freq) - pSda.setFunction(GpioFunction.I2c) - pScl.setFunction(GpioFunction.I2c) - if pull: - pSda.pullUp() - pScl.pullUp() - else: - pSda.pullDown() - pScl.pullDown() - -proc i2cWriteBlockingNim*( - i2c: ptr I2cInst, - address: I2cAddress, - data: var openArray[uint8], - noStop: bool = false - ): int = + #port = block i2c0 / i2c1 (see pinout) + #pSda/pScl = the pins you want use (ex: 2.Gpio, 3.Gpio) I do not recommend the use of 0.Gpio, 1.Gpio. + #freq = is the working frequency of the i2c device (see device manual; ex: 100000). + #pull = Disable: disable pullup, UP: active pullUp (default Up). + let sda: Gpio = pSda.Gpio + let scl: Gpio = pScl.Gpio + discard port.init(freq) + sda.setFunction(I2c) + scl.setFunction(I2c) + if pull == Disable: + sda.disablePulls() + scl.disablePulls() + elif pull == Up: + sda.pullUp() + scl.pullUp() + +proc i2cWriteBlockingNim*(i2c: ptr I2cInst; + address: uint8; + data: var openArray[uint8]; + noStop: bool = false): int {.inline.} = ## Write bytes to I2C bus. ## If `noStop` is `true`, master retains control of the bus at the end of ## the transfer. - result = i2c.writeBlocking(address, data[0].addr, data.len.uint, noStop) - -proc i2cReadBlockingNim*( - i2c: ptr I2cInst, - address: I2cAddress, - numBytes: uint, - noStop: bool = false - ): seq[uint8] = + result = i2c.writeBlocking(address.I2cAddress, data[0].addr, data.len.uint, noStop) + +proc i2cReadBlockingNim*(i2c: ptr I2cInst; + address: uint8; + numBytes: uint; + noStop: bool = false): seq[uint8] {.inline.} = ## Read `numBytes` bytes from I2C bus and return a seq containing the bytes ## that were read. In case of error return a 0-length seq. If `noStop` is ## `true`, master retains control of the bus at the end of the transfer. - result.setLen(numBytes) - let n = i2c.readBlocking(address, result[0].addr, numBytes.uint, noStop) - result.setLen(max(0, n)) + let n = i2c.readBlocking(address.I2cAddress, result[0].addr, numBytes.uint, noStop) + result.setLen(max(0,n)) proc i2cReadBlockingNim*[N: Natural]( - i2c: ptr I2cInst, - address: I2cAddress, + i2c: ptr I2cInst; + address: uint8; dest: var array[N, uint8], - noStop: bool = false - ): int = + noStop: bool = false): int {.inline.} = ## Fill the array `dest` with bytes read from I2C bus. Return the number of ## bytes that were read successfully. Negative values are error codes (refer ## to Pico SDK documentation). In case of error return a 0-length seq. If ## `noStop` is `true`, master retains control of the bus at the end of the ## transfer. - result = i2c.readBlocking(address, dest[0].addr, N.uint, noStop) + result = i2c.readBlocking(address.I2cAddress, dest[0].addr, N.uint, noStop) + when defined(runtests): i2cSetupNim(DefaultI2c.i2cGetInstance(), DefaultI2cSdaPin, DefaultI2cSclPin, 1000) From db670ec601e1fd12b170cdc9f1e6d84b15e7d82c Mon Sep 17 00:00:00 2001 From: Martinix75 Date: Wed, 22 Jan 2025 15:53:08 +0100 Subject: [PATCH 3/3] now type Pull is pubblic. --- src/picostdlib/hardware/i2c.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/picostdlib/hardware/i2c.nim b/src/picostdlib/hardware/i2c.nim index fd1935f..6dbaa91 100644 --- a/src/picostdlib/hardware/i2c.nim +++ b/src/picostdlib/hardware/i2c.nim @@ -218,7 +218,7 @@ proc getDreq*(i2c: ptr I2cInst; isTx: bool): cuint {.importc: "i2c_get_dreq".} ## Nim helpers rev 0.2.0 type - Pull = enum + Pull* = enum Up, Disable proc i2cSetupNim*(port: ptr I2cInst; @@ -235,10 +235,10 @@ proc i2cSetupNim*(port: ptr I2cInst; discard port.init(freq) sda.setFunction(I2c) scl.setFunction(I2c) - if pull == Disable: + if pull == Pull.Disable: sda.disablePulls() scl.disablePulls() - elif pull == Up: + elif pull == Pull.Up: sda.pullUp() scl.pullUp()