lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

frist, you push data into lua'stack, then you set global'table[key] = data, when DMA call onver, set global'table[key] = nil
in lua515 code like below
for hold in lua 
    lua_pushnumber(L, uuid-num-key)
    lua_pushlstring(L, str, len)

for delete from lua
    lua_pushnumber(L, uuid-num-key )

Flyer31 Test <> 于2021年6月11日周五 下午7:48写道:
Hi Shankusu,
thank you for answer, this sounds interesting - can you give me some short example how to put the data best to global data for a Lua programming newbie like me?

... somehow the "upgrade" for my current 
const char* pcData= lua_tostring( L, -1);


On Fri, Jun 11, 2021 at 11:48 AM shankusu2017 <> wrote:
Lua's gc module does not change the reference attribute of the data, so you still need to change the reference attribute of the data. You can set the data to the global table before sending, and delete it from the global table in the handle of the callback when the DMA transmission is completed. Hope Can help you

Flyer31 Test <> 于2021年6月11日周五 下午4:47写道:
In my Lua microcontroller application (quite restricted RAM space), I would implement some function written by C, which sends data out through a serial interface.

So this function gets e. g. a longer string (e. g. 200-1000 bytes), and sends out through serial interface.

For doing this in an optimum way, I would use DMA (Direct Memory access), tell the DMA the start address of this string data, and then continue with my Lua program (or if the user prefers to wait end of send, I would call yieldk/resume and thus do some other CPU work until the DMA send has finished).

Problem now is, that as soon that I leave my c function with yieldk, principally the Lua garbage collector could free this string, as it will not be needed any more by Lua.

Is there some simple and elegant way to tell to avoid this?

(of course I could also allocate some memory by c, and then copy the string to this allocated memory... but this sounds a bit weird for me as I am used to program such microcontroller application always in a very time- and RAM-optimized way - further I want to restrict the dynamic memory allocation exclusively to Lua - for "usual microcontroller" programming I would not use any dynamic memory allocation at all ... therefore it would be great if for the typical send time of some msec (could also be 200msec...) Lua garbage collector could somehow be "informed" that this string still is "in use")

(or should I better disable the Lua garbage collector during this time ... but this would sound also a bit weird to me, as it could me, that during some "file transfer" this sending will occur for longer time, and I would not like to block the lua garbage collector for "longer time").

... but for DMA it is important, that the Lua Garbage collector for sure NEITHER will free this string, NOR will re-allocate this string to some other place, until my C code signifies to Lua that the send is finished / the data is not used any more.