Skip to content

Commit 58d58ce

Browse files
committed
i2c updates
1 parent c62f25f commit 58d58ce

File tree

4 files changed

+81
-625
lines changed

4 files changed

+81
-625
lines changed

example_i2cs.nim

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## CONDITIONS OF ANY KIND, either express or implied.
88
##
99

10+
1011
import
1112
argtable3/argtable3, driver/i2c, esp_console, esp_log
1213

src/nesper/esp/esp_intr_alloc.nim

+42-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ const
4141
ESP_INTR_FLAG_LEVELMASK* = (ESP_INTR_FLAG_LEVEL1 or ESP_INTR_FLAG_LEVEL2 or ESP_INTR_FLAG_LEVEL3 or ESP_INTR_FLAG_LEVEL4 or ESP_INTR_FLAG_LEVEL5 or ESP_INTR_FLAG_LEVEL6 or ESP_INTR_FLAG_NMI) ## /< Mask for all level flags
4242
## *@}
4343

44+
type
45+
InterruptFlags* = enum
46+
intr_flag_level1 = ESP_INTR_FLAG_LEVEL1,
47+
intr_flag_level2 = ESP_INTR_FLAG_LEVEL2,
48+
intr_flag_level3 = ESP_INTR_FLAG_LEVEL3,
49+
intr_flag_lowmed = ESP_INTR_FLAG_LOWMED,
50+
intr_flag_level4 = ESP_INTR_FLAG_LEVEL4,
51+
intr_flag_level5 = ESP_INTR_FLAG_LEVEL5,
52+
intr_flag_level6 = ESP_INTR_FLAG_LEVEL6,
53+
intr_flag_nmi = ESP_INTR_FLAG_NMI,
54+
intr_flag_high = ESP_INTR_FLAG_HIGH,
55+
intr_flag_levelmask = ESP_INTR_FLAG_LEVELMASK
56+
intr_flag_shared = ESP_INTR_FLAG_SHARED,
57+
intr_flag_edge = ESP_INTR_FLAG_EDGE,
58+
intr_flag_iram = ESP_INTR_FLAG_IRAM,
59+
intr_flag_intrdisabled = ESP_INTR_FLAG_INTRDISABLED,
60+
4461
## * @addtogroup Intr_Alloc_Pseudo_Src
4562
## @{
4663
##
@@ -93,6 +110,8 @@ type
93110

94111
proc esp_intr_mark_shared*(intno: cint; cpu: cint; is_in_iram: bool): esp_err_t {.
95112
importc: "esp_intr_mark_shared", header: "esp_intr_alloc.h".}
113+
114+
96115
## *
97116
## @brief Reserve an interrupt to be used outside of this framework
98117
##
@@ -108,6 +127,8 @@ proc esp_intr_mark_shared*(intno: cint; cpu: cint; is_in_iram: bool): esp_err_t
108127

109128
proc esp_intr_reserve*(intno: cint; cpu: cint): esp_err_t {.
110129
importc: "esp_intr_reserve", header: "esp_intr_alloc.h".}
130+
131+
111132
## *
112133
## @brief Allocate an interrupt with the given parameters.
113134
##
@@ -145,6 +166,8 @@ proc esp_intr_reserve*(intno: cint; cpu: cint): esp_err_t {.
145166
proc esp_intr_alloc*(source: cint; flags: cint; handler: intr_handler_t; arg: pointer;
146167
ret_handle: ptr intr_handle_t): esp_err_t {.
147168
importc: "esp_intr_alloc", header: "esp_intr_alloc.h".}
169+
170+
148171
## *
149172
## @brief Allocate an interrupt with the given parameters.
150173
##
@@ -181,10 +204,11 @@ proc esp_intr_alloc*(source: cint; flags: cint; handler: intr_handler_t; arg: po
181204
## ESP_OK otherwise
182205
##
183206

184-
proc esp_intr_alloc_intrstatus*(source: cint; flags: cint; intrstatusreg: uint32_t;
185-
intrstatusmask: uint32_t; handler: intr_handler_t;
207+
proc esp_intr_alloc_intrstatus*(source: cint; flags: cint; intrstatusreg: uint32;
208+
intrstatusmask: uint32; handler: intr_handler_t;
186209
arg: pointer; ret_handle: ptr intr_handle_t): esp_err_t {.
187210
importc: "esp_intr_alloc_intrstatus", header: "esp_intr_alloc.h".}
211+
188212
## *
189213
## @brief Disable and free an interrupt.
190214
##
@@ -206,6 +230,8 @@ proc esp_intr_alloc_intrstatus*(source: cint; flags: cint; intrstatusreg: uint32
206230

207231
proc esp_intr_free*(handle: intr_handle_t): esp_err_t {.importc: "esp_intr_free",
208232
header: "esp_intr_alloc.h".}
233+
234+
209235
## *
210236
## @brief Get CPU number an interrupt is tied to
211237
##
@@ -216,6 +242,8 @@ proc esp_intr_free*(handle: intr_handle_t): esp_err_t {.importc: "esp_intr_free"
216242

217243
proc esp_intr_get_cpu*(handle: intr_handle_t): cint {.importc: "esp_intr_get_cpu",
218244
header: "esp_intr_alloc.h".}
245+
246+
219247
## *
220248
## @brief Get the allocated interrupt for a certain handle
221249
##
@@ -226,6 +254,8 @@ proc esp_intr_get_cpu*(handle: intr_handle_t): cint {.importc: "esp_intr_get_cpu
226254

227255
proc esp_intr_get_intno*(handle: intr_handle_t): cint {.
228256
importc: "esp_intr_get_intno", header: "esp_intr_alloc.h".}
257+
258+
229259
## *
230260
## @brief Disable the interrupt associated with the handle
231261
##
@@ -245,6 +275,8 @@ proc esp_intr_get_intno*(handle: intr_handle_t): cint {.
245275

246276
proc esp_intr_disable*(handle: intr_handle_t): esp_err_t {.
247277
importc: "esp_intr_disable", header: "esp_intr_alloc.h".}
278+
279+
248280
## *
249281
## @brief Enable the interrupt associated with the handle
250282
##
@@ -259,6 +291,8 @@ proc esp_intr_disable*(handle: intr_handle_t): esp_err_t {.
259291

260292
proc esp_intr_enable*(handle: intr_handle_t): esp_err_t {.
261293
importc: "esp_intr_enable", header: "esp_intr_alloc.h".}
294+
295+
262296
## *
263297
## @brief Set the "in IRAM" status of the handler.
264298
##
@@ -274,16 +308,22 @@ proc esp_intr_enable*(handle: intr_handle_t): esp_err_t {.
274308

275309
proc esp_intr_set_in_iram*(handle: intr_handle_t; is_in_iram: bool): esp_err_t {.
276310
importc: "esp_intr_set_in_iram", header: "esp_intr_alloc.h".}
311+
312+
277313
## *
278314
## @brief Disable interrupts that aren't specifically marked as running from IRAM
279315
##
280316

281317
proc esp_intr_noniram_disable*() {.importc: "esp_intr_noniram_disable",
282318
header: "esp_intr_alloc.h".}
319+
320+
283321
## *
284322
## @brief Re-enable interrupts disabled by esp_intr_noniram_disable
285323
##
286324

287325
proc esp_intr_noniram_enable*() {.importc: "esp_intr_noniram_enable",
288326
header: "esp_intr_alloc.h".}
327+
328+
289329
## *@}

src/nesper/i2cs.nim

+38-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import consts
33
import general
44
import esp/gpio
5+
import esp/esp_intr_alloc
56
import esp/driver/i2c
67

78
# export spi_host_device_t, spi_device_t, spi_bus_config_t, spi_transaction_t, spi_device_handle_t
@@ -30,28 +31,23 @@ type
3031
# i2c_obj_t = distinct pointer
3132

3233
# var p_i2c_obj {.importc: "p_i2c_obj".}: UncheckedArray[i2c_obj_t]
33-
proc newI2CMaster*(
34+
proc newI2CDriver(
3435
port: i2c_port_t,
3536
mode: i2c_mode_t, ## !< I2C mode
3637
sda_io_num: gpio_num_t, ## !< GPIO number for I2C sda signal
3738
scl_io_num: gpio_num_t, ## !< GPIO number for I2C scl signal
3839
clk_speed: Hertz,
40+
slv_rx_buf_len: csize_t,
41+
slv_tx_buf_len: csize_t;
3942
sda_pullup_en: bool, ## !< Internal GPIO pull mode for I2C sda signal
4043
scl_pullup_en: bool, ## !< Internal GPIO pull mode for I2C scl signal
41-
): I2CPort =
42-
43-
proc i2c_driver_install*(i2c_num: i2c_port_t;
44-
mode: i2c_mode_t;
45-
slv_rx_buf_len: csize_t;
46-
slv_tx_buf_len: csize_t;
47-
intr_alloc_flags: esp_intr_flags
48-
): esp_err_t {. .}
49-
50-
let iret = i2c_driver_install(port,
51-
I2C_MODE_MASTER,
52-
I2C_MASTER_RX_BUF_DISABLE,
53-
I2C_MASTER_TX_BUF_DISABLE,
54-
0)
44+
intr_alloc_flags: set[InterruptFlags]): I2CPort =
45+
46+
var iflags = esp_intr_flags(0)
47+
for fl in intr_alloc_flags:
48+
iflags = iflags or fl.esp_intr_flags
49+
50+
let iret = i2c_driver_install(port, I2C_MODE_MASTER, slv_rx_buf_len, slv_tx_buf_len, iflags)
5551
if iret != ESP_OK:
5652
raise newEspError[I2CError]("Error initializing i2c port (" & $esp_err_to_name(ret) & ")", ret)
5753

@@ -67,6 +63,33 @@ proc newI2CMaster*(
6763
raise newEspError[I2CError]("Error initializing i2c port (" & $esp_err_to_name(ret) & ")", ret)
6864

6965

66+
proc newI2CMaster*(
67+
port: i2c_port_t,
68+
mode: i2c_mode_t, ## !< I2C mode
69+
sda_io_num: gpio_num_t, ## !< GPIO number for I2C sda signal
70+
scl_io_num: gpio_num_t, ## !< GPIO number for I2C scl signal
71+
clk_speed: Hertz;
72+
sda_pullup_en: bool = false, ## !< Internal GPIO pull mode for I2C sda signal
73+
scl_pullup_en: bool = false, ## !< Internal GPIO pull mode for I2C scl signal
74+
intr_alloc_flags: set[InterruptFlags]): I2CPort =
75+
76+
return newI2CDriver( port, mode, sda_io_num, scl_io_num, clk_speed, slv_rx_buf_len = 0, slv_tx_buf_len = 0, sda_pullup_en, scl_pullup_en, intr_alloc_flags)
77+
78+
proc newI2CSlave*(
79+
port: i2c_port_t,
80+
mode: i2c_mode_t, ## !< I2C mode
81+
sda_io_num: gpio_num_t, ## !< GPIO number for I2C sda signal
82+
scl_io_num: gpio_num_t, ## !< GPIO number for I2C scl signal
83+
clk_speed: Hertz;
84+
slv_rx_buf_len: csize_t,
85+
slv_tx_buf_len: csize_t,
86+
sda_pullup_en: bool = false, ## !< Internal GPIO pull mode for I2C sda signal
87+
scl_pullup_en: bool = false, ## !< Internal GPIO pull mode for I2C scl signal
88+
intr_alloc_flags: set[InterruptFlags]): I2CPort =
89+
90+
return newI2CDriver(port, mode, sda_io_num, scl_io_num, clk_speed, slv_rx_buf_len = slv_rx_buf_len , slv_tx_buf_len = slv_tx_buf_len, sda_pullup_en, scl_pullup_en, intr_alloc_flags)
91+
92+
7093
proc newI2CCmd*(): I2CCmd =
7194
# Creates a new I2C Command object
7295
result.handle = i2c_cmd_link_create()

0 commit comments

Comments
 (0)