lua-users home
lua-l archive

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


Hi Pierre + all others,
please excuse, meanwhile I really have a slightly bad conscience concerning this question ... it is really somehow not thought deep enough - you really are somehow right with you basic contras against the general approach here.

I really recognized now that using the user software data directly for DMA is quite unsuitable for serial comm, alone due to the typical "serial timeout possibilities" which need to be handled very special (and which should not delay Lua of course), and further also because Lua user script is not the only possibility to trigger dtata transfer some data over such a serial interface, e. g. I also need to think about what with error messages, programming which possibly does NOT go through Lua etc etc... .... .  

So quite clear now that I need to "spend" some RAM for such DMA buffers, wether static or dynamic not yet clear, but this is currently my own task to think a bit deeper here... .

On Fri, Jun 11, 2021 at 11:01 AM Pierre Chapuis <catwell@archlinux.us> wrote:
The way I'd do this to avoid copies is I'd implement a buffer type with its memory managed by the engine (in C) and expose it to the Lua program as userdata. With the right metatables you can almost make it look like a string on the Lua side if you want.

Trying to use Lua-managed data in unexpected ways is always trickier in my opinion.

-- 
Pierre Chapuis


On Fri, Jun 11, 2021, at 10:47, Flyer31 Test wrote:
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.