feat(lua): add lua_driver_twai -- TWAI (CAN bus) Lua driver#96
Open
ayyaris wants to merge 1 commit into
Open
Conversation
Wraps the ESP-IDF v6 esp_driver_twai node API as a Lua module:
API (require("twai")):
twai.new(tx_gpio, rx_gpio, bitrate [, opts]) -> node
node:enable()
node:disable()
node:send(id, data [, is_extended [, is_rtr]])
node:receive([timeout_ms]) -> {id, data, extended, rtr} | nil
node:status() -> {state, tx_errors, rx_errors, ...}
node:recover()
node:close()
Implementation:
- ISR-driven RX queue (depth 32) via on_rx_done callback
- Registered via Kconfig APP_CLAW_LUA_DRIVER_TWAI
(default y when SOC_TWAI_SUPPORTED)
- idf_component.yml: conditional dependency on lua_driver_twai
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3 tasks
|
Alessio Ayyari seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Pull request overview
This PR introduces a new twai Lua module (lua_driver_twai) to expose the ESP32 TWAI (CAN bus) peripheral via ESP-IDF v6’s esp_driver_twai node API, wiring it into app_claw’s Kconfig/dependency system and Lua module registration.
Changes:
- Adds a new
lua_driver_twaicomponent implementingtwai.new()/enable()/send()/receive()/status()/recover()/close()with an ISR-driven RX queue. - Adds
APP_CLAW_LUA_DRIVER_TWAIKconfig andidf_component.ymlconditional dependency, plus registration inapp_lua_modules.c. - Removes LVGL Lua module configuration/dependency/registration from
app_claw(appears unrelated and breaking).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 12 comments.
Show a summary per file
| File | Description |
|---|---|
| components/lua_modules/lua_driver_twai/src/lua_driver_twai.c | Implements the TWAI Lua driver, including ISR RX callback + Lua API surface. |
| components/lua_modules/lua_driver_twai/src/lua_driver_twai.h | Public header for module open/registration. |
| components/lua_modules/lua_driver_twai/README.md | Documents usage and examples for the twai Lua module. |
| components/lua_modules/lua_driver_twai/CMakeLists.txt | Registers the new component and its dependencies. |
| components/common/app_claw/Kconfig | Adds APP_CLAW_LUA_DRIVER_TWAI option; also removes LVGL option. |
| components/common/app_claw/idf_component.yml | Adds conditional dependency for lua_driver_twai; also removes LVGL dependency block. |
| components/common/app_claw/app_lua_modules.c | Registers the twai module; also removes LVGL include/registration entries. |
Comments suppressed due to low confidence (1)
components/common/app_claw/idf_component.yml:272
- The conditional dependency block for lua_module_lvgl was removed from app_claw’s idf_component.yml, which will prevent the existing lua_module_lvgl component from being pulled in when enabled. If LVGL support is still intended to be available, please re-add the lua_module_lvgl dependency rules/path entry (and keep it aligned with the corresponding Kconfig option).
lua_module_led_strip:
rules:
- if: $CONFIG{APP_CLAW_CAP_LUA} == True
- if: $CONFIG{APP_CLAW_LUA_MODULE_LED_STRIP} == True
path: ../../lua_modules/lua_module_led_strip
lua_module_magnetometer:
rules:
- if: $CONFIG{APP_CLAW_CAP_LUA} == True
- if: $CONFIG{APP_CLAW_LUA_MODULE_MAGNETOMETER} == True
path: ../../lua_modules/lua_module_magnetometer
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
351
to
360
| config APP_CLAW_LUA_MODULE_LED_STRIP | ||
| bool "Enable LED strip Lua module" | ||
| default y | ||
| help | ||
| Enable the LED strip Lua module and keep app_claw's direct dependency | ||
| on lua_module_led_strip when Lua capability is enabled. | ||
|
|
||
| config APP_CLAW_LUA_MODULE_LVGL | ||
| bool "Enable LVGL Lua module" | ||
| default n | ||
| help | ||
| Enable the LVGL Lua module and keep app_claw's direct dependency | ||
| on lua_module_lvgl when Lua capability is enabled. | ||
|
|
||
| config APP_CLAW_LUA_MODULE_MAGNETOMETER | ||
| bool "Enable magnetometer Lua module" | ||
| default n |
Comment on lines
88
to
96
| #if CONFIG_APP_CLAW_LUA_MODULE_LCD_TOUCH && defined(CONFIG_ESP_BOARD_DEV_LCD_TOUCH_SUPPORT) && defined(CONFIG_ESP_BOARD_DEV_LCD_TOUCH_SUB_I2C_SUPPORT) | ||
| #include "lua_module_lcd_touch.h" | ||
| #endif | ||
| #if CONFIG_APP_CLAW_LUA_MODULE_LED_STRIP | ||
| #include "lua_module_led_strip.h" | ||
| #endif | ||
| #if CONFIG_APP_CLAW_LUA_MODULE_LVGL | ||
| #include "lua_module_lvgl.h" | ||
| #endif | ||
| #if CONFIG_APP_CLAW_LUA_MODULE_MAGNETOMETER | ||
| #include "lua_module_magnetometer.h" | ||
| #endif |
Comment on lines
+86
to
+99
| int tx_gpio = (int)luaL_checkinteger(L, 1); | ||
| int rx_gpio = (int)luaL_checkinteger(L, 2); | ||
| uint32_t bps = (uint32_t)luaL_checkinteger(L, 3); | ||
|
|
||
| bool listen_only = false; | ||
| bool loopback = false; | ||
| if (lua_istable(L, 4)) { | ||
| lua_getfield(L, 4, "listen_only"); | ||
| listen_only = lua_toboolean(L, -1); | ||
| lua_pop(L, 1); | ||
| lua_getfield(L, 4, "loopback"); | ||
| loopback = lua_toboolean(L, -1); | ||
| lua_pop(L, 1); | ||
| } |
Comment on lines
+63
to
+67
| memcpy(item.data, buf, item.len); | ||
|
|
||
| BaseType_t woken = pdFALSE; | ||
| xQueueSendFromISR(ud->rx_queue, &item, &woken); | ||
| return woken == pdTRUE; |
Comment on lines
+56
to
+64
| twai_rx_item_t item = { | ||
| .id = frame.header.id, | ||
| .is_extended = frame.header.ide, | ||
| .is_rtr = frame.header.rtr, | ||
| .len = (uint8_t)(frame.header.dlc > LUA_DRIVER_TWAI_DATA_MAX | ||
| ? LUA_DRIVER_TWAI_DATA_MAX : frame.header.dlc), | ||
| }; | ||
| memcpy(item.data, buf, item.len); | ||
|
|
|
|
||
| esp_err_t err = twai_node_transmit(ud->node, &frame, pdMS_TO_TICKS(100)); | ||
| if (err != ESP_OK) { | ||
| return luaL_error(L, "twai: transmit failed (%d)", err); |
| twai_node_record_t record = {0}; | ||
| esp_err_t err = twai_node_get_info(ud->node, &status, &record); | ||
| if (err != ESP_OK) { | ||
| return luaL_error(L, "twai: get_info failed (%d)", err); |
| lua_driver_twai_ud_t *ud = twai_check_ud(L, 1); | ||
| esp_err_t err = twai_node_recover(ud->node); | ||
| if (err != ESP_OK) { | ||
| return luaL_error(L, "twai: recover failed (%d)", err); |
Comment on lines
+194
to
+200
| }, | ||
| .buffer = buf, | ||
| .buffer_len = data_len, | ||
| }; | ||
|
|
||
| esp_err_t err = twai_node_transmit(ud->node, &frame, pdMS_TO_TICKS(100)); | ||
| if (err != ESP_OK) { |
Comment on lines
+84
to
+90
| static int twai_new(lua_State *L) | ||
| { | ||
| int tx_gpio = (int)luaL_checkinteger(L, 1); | ||
| int rx_gpio = (int)luaL_checkinteger(L, 2); | ||
| uint32_t bps = (uint32_t)luaL_checkinteger(L, 3); | ||
|
|
||
| bool listen_only = false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
lua_driver_twai, a new Lua module that exposes the ESP32 TWAI (Two-Wire Automotive Interface / CAN bus) peripheral to Lua scripts via the ESP-IDF v6esp_driver_twainode API.API
Implementation
on_rx_donecallback — zero-copy path from ISR to FreeRTOS queueAPP_CLAW_LUA_DRIVER_TWAI(defaultywhenSOC_TWAI_SUPPORTED)components/common/app_claw/idf_component.ymlconditional dependency +app_lua_modules.cregistrationesp_driver_twai(IDF v6+)Files
components/lua_modules/lua_driver_twai/src/lua_driver_twai.ccomponents/lua_modules/lua_driver_twai/src/lua_driver_twai.hcomponents/lua_modules/lua_driver_twai/CMakeLists.txtcomponents/lua_modules/lua_driver_twai/README.mdcomponents/common/app_claw/KconfigAPP_CLAW_LUA_DRIVER_TWAIoptioncomponents/common/app_claw/app_lua_modules.ccomponents/common/app_claw/idf_component.ymlTest plan
require("twai")succeeds in Lua runtimetwai.new()creates node,enable()/send()/receive()work on bus🤖 Generated with Claude Code