Switching from mp_obj_str_get_data to mp_get_buffer_raise #9387
-
I am running into an issue with a serial driver, where I want to switch from only supporting string data to supporting anything with the buffer protocol. The motivation behind this is not having to allocate memory for every string, when all the data is already being populated in a bytearray. I will then look at using a memory view as well. Previously, I would pass the string object to the DMA like so: volatile static char *dma_ptr;
volatile static uint32_t dma_bytes_left_to_send;
dma_ptr = (char *)mp_obj_str_get_data(write_buffer, (size_t *)&dma_bytes_left_to_send);
dma_transfer(&dma_ptr, &dma_bytes_left_to_send) I looked at an example (UJSON Module, BTree Module), and tried to do the same changes: mp_buffer_info_t bufinfo;
mp_get_buffer_raise(write_buffer, &bufinfo, MP_BUFFER_READ);
dma_ptr = (char *)bufinfo.buf;
dma_bytes_left_to_send = (size_t)bufinfo.len; But what I then get out of the serial buffer is sometimes garbage. If I pass this function a bytearray or string, some of the data makes sense, but other data does not. For example, if I send it: >>>send_serial(bytearray(b'HELLO WORLD\r\n'))
or
>>>send_serial("HELLO WORLD\r\n") then this works as expected, and I get that data sent over serial. However, if I then send the same data in lowercase: >>>send_serial(bytearray(b'hello world\r\n'))
or
>>>send_serial("hello world\r\n") then the data I get out is garbage. Am I missing something obvious or do I need to chase the issue further down the line into the DMA? It works if I use To maintain backwards compatibility, I cannot move it to just using a bytearray or anything similar. I need to continue supporting strings or byte-strings. How do I extract the string data (which I assumed was just being stored as bytes anyway)? is this because of a UTF8 encoding this perhaps? EDIT: Non standard board with ARM_CORTEX M4 processor, running Micropython 1.10 (I know, its a bit old, need to update at some point but low on the priority list at the moment) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You did not mentions which board and firmware version you use. |
Beta Was this translation helpful? Give feedback.
-
Apologies for wasting everyone's time, I have now resolved the issue. The issue was unrelated to this method, and turned out to be a silly mistake validating data before the dma transfer. |
Beta Was this translation helpful? Give feedback.
-
No problem. It's a well known mechanism that explaining the problem to a real audience helps finding the solution oneself. |
Beta Was this translation helpful? Give feedback.
Apologies for wasting everyone's time, I have now resolved the issue. The issue was unrelated to this method, and turned out to be a silly mistake validating data before the dma transfer.