Skip to content

Commit 9d7ebf1

Browse files
committed
Repair link module for revert
1 parent 9ba1712 commit 9d7ebf1

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/link/engine_link_module.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(engine_link_module_set_disconnected_cb_obj, engine_lin
140140
/* --- doc ---
141141
NAME: send
142142
ID: engine_link_send
143-
DESC: Provided a `bytearray`, send up to `count`, `len(send_buffer)`, or `len(send_buffer)-offset` (whichever is smallest and passed) bytes starting at `0` by default or from `offset` if passed
143+
DESC: Provided a `bytearray`, send up to `count`, `len(send_buffer)`, or `len(send_buffer)-offset` (whichever is smallest and passed). Sends bytes starting at `0` by default or from `offset` if passed. The actual number of bytes sent is returned. If the send buffer is larger than 256 bytes, the extra data is not sent.
144144
PARAM: [type=bytearray] [name=send_buffer] [value=bytearray]
145145
PARAM: [type=int] [name=count] [value=int (optional)]
146146
PARAM: [type=int] [name=offset] [value=int (optional)]
@@ -190,20 +190,22 @@ static mp_obj_t engine_link_module_send(size_t n_args, const mp_obj_t *args){
190190

191191
// Figure out which of the three is smallest and send
192192
count = min3(count, send_buffer->len, send_buffer->len-offset);
193-
engine_link_send((uint8_t*)send_buffer->items, count, offset);
193+
194+
// Get the actual number of bytes sent/placed into the TX buffer
195+
uint32_t sent_count = engine_link_send((uint8_t*)send_buffer->items, count, offset);
194196

195197
// Run the task first (for host) to get data flowing
196198
engine_link_module_task();
197199

198-
return mp_obj_new_int(count);
200+
return mp_obj_new_int(sent_count);
199201
}
200202
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(engine_link_module_send_obj, 1, 3, engine_link_module_send);
201203

202204

203205
/* --- doc ---
204206
NAME: read
205207
ID: engine_link_read
206-
DESC: Read up to `count` or {ref_link:engine_link_available}, whichever is smallest. Allocates and returns a new `bytearray` if 1 or more bytes to read.
208+
DESC: Read up to `count` or `engine_link.available()`, whichever is smallest. Allocates and returns a new `bytearray` if 1 or more bytes to read. If data is not read fast enough and `engine_link.available()` reaches 511 and new data is received, the oldest data is overwritten.
207209
PARAM: [type=int] [name=count] [value=int (optional)]
208210
RETURN: None if no bytes to read otherwise `bytearray`
209211
*/
@@ -242,7 +244,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(engine_link_module_read_obj, engine_link_module_read);
242244
/* --- doc ---
243245
NAME: read_into
244246
ID: engine_link_read_into
245-
DESC: Read up to `count`, {ref_link:engine_link_available} or `len(buffer)`, whichever is smallest into `buffer` starting at `0` or from `offset` if passed.
247+
DESC: Read up to `count`, `engine_link.available()` or `len(buffer)`, whichever is smallest into `buffer` starting at `0` or from `offset` if passed. If data is not read fast enough and `engine_link.available()` reaches 511 and new data is received, the oldest data is overwritten.
246248
PARAM: [type=bytearray] [name=read_buffer] [value=bytearray]
247249
PARAM: [type=int] [name=count] [value=int (optional)]
248250
PARAM: [type=int] [name=offset] [value=int (optional)]
@@ -305,7 +307,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(engine_link_module_read_into_obj, 1, 3, engi
305307
/* --- doc ---
306308
NAME: available
307309
ID: engine_link_available
308-
DESC: Returns the number of bytes available to read from the internal 512 byte buffer. If after 512 bytes more data is sent to the full internal buffer, those extra bytes overwrite previous bytes (ringbuffer).
310+
DESC: Returns the number of bytes available to read from the internal 511 byte buffer. If after 511 bytes more data is sent to the full internal buffer, those extra bytes overwrite the oldest data (ringbuffer).
309311
RETURN: Number of bytes available to read (int)
310312
*/
311313
static mp_obj_t engine_link_module_available(){
@@ -358,7 +360,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(engine_link_module_is_started_obj, engine_link_module_
358360
/* --- doc ---
359361
NAME: is_host
360362
ID: engine_link_is_host
361-
DESC: When not connected, always returns `False`. If connected, returns `True` the unit is acting as the USB host, otherwise returns `False` if acting as a USB device.
363+
DESC: When not connected, always returns `False`. If connected, returns `True` if the unit is acting as the USB host, otherwise returns `False` if acting as a USB device.
362364
RETURN: True or False
363365
*/
364366
static mp_obj_t engine_link_module_is_host(){

src/link/engine_link_rp3.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,24 @@ void engine_link_on_just_disconnected(){
163163
}
164164

165165

166-
void engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset){
166+
uint32_t engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset){
167167
// Don't try to send if not connected to anything
168168
if(!engine_link_connected()){
169-
return;
169+
return 0;
170170
}
171171

172+
uint32_t sent_count = 0;
173+
172174
// Send
173175
if(is_host){
174-
tuh_cdc_write(mounted_device_cdc_daddr, send_buffer+offset, count);
176+
sent_count = tuh_cdc_write(mounted_device_cdc_daddr, send_buffer+offset, count);
175177
tuh_cdc_write_flush(mounted_device_cdc_daddr);
176178
}else{
177-
tud_cdc_write(send_buffer+offset, count);
179+
sent_count = tud_cdc_write(send_buffer+offset, count);
178180
tud_cdc_write_flush();
179181
}
182+
183+
return sent_count;
180184
}
181185

182186

src/link/engine_link_rp3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void engine_link_stop();
1212
void engine_link_on_just_connected();
1313
void engine_link_on_just_disconnected();
1414

15-
void engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset);
15+
uint32_t engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset);
1616
void engine_link_read_into(uint8_t *buffer, uint32_t count, uint32_t offset);
1717
uint32_t engine_link_available();
1818
void engine_link_clear_send();

src/link/engine_link_unix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ void engine_link_on_just_disconnected(){
3131
}
3232

3333

34-
void engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset){
35-
34+
uint32_t engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset){
35+
return 0;
3636
}
3737

3838

src/link/engine_link_unix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void engine_link_stop();
1212
void engine_link_on_just_connected();
1313
void engine_link_on_just_disconnected();
1414

15-
void engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset);
15+
uint32_t engine_link_send(const uint8_t *send_buffer, uint32_t count, uint32_t offset);
1616
void engine_link_read_into(uint8_t *buffer, uint32_t count, uint32_t offset);
1717
uint32_t engine_link_available();
1818
void engine_link_clear_send();

0 commit comments

Comments
 (0)