Skip to content

Commit d9713e4

Browse files
committed
add board-defined custom handler support to libjabi and pyjabi
1 parent 7bd84b0 commit d9713e4

File tree

8 files changed

+59
-1
lines changed

8 files changed

+59
-1
lines changed

clients/libjabi/device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class Device {
130130
std::string echo(std::string str);
131131
size_t req_max_size();
132132
size_t resp_max_size();
133+
std::vector<uint8_t> custom(std::vector<uint8_t> data);
133134

134135
/* CAN */
135136
void can_set_filter(int id, int id_mask, bool rtr, bool rtr_mask, int idx=0);

clients/libjabi/peripherals/metadata.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,23 @@ size_t Device::resp_max_size() {
102102
return letoh<uint16_t>(ret->size);
103103
}
104104

105+
std::vector<uint8_t> Device::custom(std::vector<uint8_t> data) {
106+
if (data.size() > interface->get_req_max_size()) {
107+
throw std::runtime_error("data too long");
108+
}
109+
iface_dynamic_req_t req = {
110+
.msg = {
111+
.periph_id = PERIPH_METADATA_ID,
112+
.periph_idx = 0,
113+
.periph_fn = METADATA_CUSTOM_ID,
114+
.payload_len = static_cast<uint16_t>(data.size()),
115+
.payload = {0},
116+
},
117+
.payload = data,
118+
};
119+
120+
iface_dynamic_resp_t resp = interface->send_request(req);
121+
return resp.payload;
122+
}
123+
105124
};

clients/pyjabi/jabi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ PYBIND11_MODULE(jabi, m) {
136136
.def("echo", &Device::echo)
137137
.def("req_max_size", &Device::req_max_size)
138138
.def("resp_max_size", &Device::resp_max_size)
139+
.def("custom", &Device::custom)
139140

140141
/* CAN */
141142
.def("can_set_filter", &Device::can_set_filter,

examples/pyjabi/usb_pdmon.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import jabi
2+
import time
3+
4+
# see zephyrboards/boards/riscv/usb_pdmon/board.c
5+
6+
def set(d, mV, mA):
7+
d.custom(list(b'\x00' + mV.to_bytes(2, 'big') + mA.to_bytes(2, 'big')))
8+
9+
def get(d):
10+
raw = d.custom([1])
11+
mV = int.from_bytes(raw[0:2], 'big')
12+
mA = int.from_bytes(raw[2:4], 'big')
13+
return { "mV": mV, "mA": mA }
14+
15+
if __name__ == "__main__":
16+
d = jabi.USBInterface.list_devices()[0]
17+
set(d, 20000, 5000)
18+
time.sleep(1.0) # wait for request to happen
19+
print("actual: ", get(d))
20+
while True:
21+
mV = d.adc_read(1)
22+
mA = (d.adc_read(0) - 1500) * 1000 / 200 # ACS70331EESATR-005B3
23+
print(mV, "\bmV", mA, "\bmA")
24+
time.sleep(0.5)

firmware/boards/usb_pdmon.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CONFIG_LOG=y
2+
CONFIG_LOG_DEFAULT_LEVEL=2
23
CONFIG_LOG_MODE_IMMEDIATE=n
34
CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD=0
45
CONFIG_LOG_PROCESS_THREAD_SLEEP_MS=100

firmware/src/peripherals/metadata.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,20 @@ PERIPH_FUNC_DEF(resp_max_size) {
8585
return JABI_NO_ERR;
8686
}
8787

88+
__attribute__((weak)) // allow boards to override
89+
int16_t jabi_metadata_custom(uint16_t idx, uint8_t *req, uint16_t req_len,
90+
uint8_t *resp, uint16_t *resp_len) {
91+
LOG_ERR("board doesn't support custom commands");
92+
return JABI_NOT_SUPPORTED_ERR;
93+
}
94+
8895
static const periph_func_t metadata_periph_fns[] = {
8996
serial,
9097
num_inst,
9198
echo,
9299
req_max_size,
93100
resp_max_size,
101+
jabi_metadata_custom,
94102
};
95103

96104
const struct periph_api_t metadata_periph_api = {

include/jabi/peripherals/metadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ PACKED(metadata_resp_max_size_resp_t,
2424
uint16_t size;
2525
);
2626

27+
typedef uint8_t metadata_custom_req_t;
28+
typedef uint8_t metadata_custom_resp_t;
29+
2730
/* Function indices */
2831
#define METADATA_SERIAL_ID 0
2932
#define METADATA_NUM_INST_ID 1
3033
#define METADATA_ECHO_ID 2
3134
#define METADATA_REQ_MAX_SIZE_ID 3
3235
#define METADATA_RESP_MAX_SIZE_ID 4
36+
#define METADATA_CUSTOM_ID 5
3337

3438
#endif // JABI_PERIPHERALS_METADATA_H

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ manifest:
66
projects:
77
- name: zephyrboards
88
remote: dragonlock2
9-
revision: 912db0139ca1bfc6db0c629d7264d5befb2acd4e
9+
revision: d901566c60aa1d83032c6348e8ee6ee8094604ee
1010
import: true
1111

1212
self:

0 commit comments

Comments
 (0)