1

i'm here to ask you some help about an error that happen during the execution of my code. I' m coding on a Adafruit Feather HUZZAH ESP8266 board with the latest NodeMCU master firmware using Lua. I want to read values from a .bin file in an SD card and show it on the Monitor near its decimal address position, the rest of the project it s to write it on a ROM with SPI but before i have to resolve this, so there is some commented code that is unnecessary.

This is the code :

    function WriteAByte()
    spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
    vol = file.mount("/SD0", 2)
    file.open("/SD0/bios.bin","r")
    file.seek("set", seek)
    bytes = file.read(1)
    print(seek.." "..bytes)
    seek = file.seek("cur")
    file.close()
    if(bytes == nil)then
      mytimer:stop()
      print("FINISHED TO WRITE")
    else 
        -- set SPI BIOS
        --spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
        --spi.set_mosi(1,0,8,bytes)
        --spi.transaction(1, 8,0x03, 24, index, 8, 0, 0)
        --index = index + 0x000001
    end
end

mytimer = tmr.create()

index = 0x000000
seek = 0
bytes = 0



mytimer:register(100, tmr.ALARM_AUTO,WriteAByte)
mytimer:start()

The code works but after 64 bytes (always 64) the program stop to work and tell me

        0 �
1 �
2 �
3 �
4 �
5 �
6 �
7 �
8 �
9 �
10 �
11 �
12 �
13 �
14 �
15 �
16 Z
17 �
18 �
19 
20 
21 
22 
23 
24 
25 
26 
27 
28  
29 
30 !
31 
32 �
33 �
34 �
35 �
36 �
37 �
38 �
39 �
40 �
41 �
42 �
43 �
44 �
45 �
46 �
47 �
48 D
49 
50 �
51 d
52 
53 
54 
55 
56 
57 
58 
59 
60 �
61 �
62 �
63 �
64 
E:M 568
PANIC: unprotected error in call to Lua API (application.lua:5: open a file first)

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 26996, room 16 
tail 4
chksum 0x80
load 0x3ffe8000, len 2380, room 4 
tail 8
chksum 0xf5
load 0x3ffe894c, len 136, room 0 
tail 8
chksum 0xd7
csum 0xd7

I thinked about a Serial Monitor Buff Saturation so i tried it without the printing of the seek and bytes but it went in the same error. After i thinked about a problem on the 65 bytes so i set seek to an higher value than 65 but it read only 64 bytes of the rest and after it goes in error with the same code. So i think it is something like a memory saturation error but calling the garbage collecting function to see free memory tell always the same value of free memory in every cycle and if i garbage collect every cycle the error doen t disappear. I read something about a heap memory saturation so i tried to call the function in a separate file with dofile every cycle and keep some variables of the function local but this didn t helped. How can i fix this problem ? I can t upload the binary but it is 64 MB and i read 1 Bytes for cycle and i dont think the problem is the file.

UPDATE

this are the new code files:

init.lua

dofile("afterInit.lua")

afterInit.lua

function f()
    print("Heap all'avvio del timer : "..node.heap()) 
    if(semaphore3 == 0)then
        mytimer2:start()
    end
end

function ff()
    print("Heap all'avvio del timer2 : "..node.heap())
    semaphore3 = 1
    print("Heap dopo assegnazione semaphore3 in timer2 : "..node.heap())
    dofile("application.lua")
end

semaphore1 = 0
semaphore2 = 0
semaphore3 = 0

index = 0x000000
seek = 0
bytes = 0

mytimer = tmr.create()
mytimer2 = tmr.create()
mytimer:register(100, tmr.ALARM_AUTO,f)
mytimer2:register(1, tmr.ALARM_SEMI,ff)
mytimer:start()

application.lua

print("Heap all'avvio di application.lua : "..node.heap())
if(semaphore1 == 0)then
    print("Heap all'avvio 1 di a.lua : "..node.heap())
    semaphore1 = 1
    dofile("a.lua")
end
if(semaphore1 == 0)then
    print("Heap alla chiusura 1 di a.lua : "..node.heap())

    collectgarbage ()
    tmr.delay(50)
    print("Heap dopo garbace collector : "..node.heap())
    print((seek-1).." "..bytes)
    print("Heap dopo controllo semaphore1 in application : "..node.heap())
    if(semaphore2 == 0)then
        print("Heap all'avvio 1 di b.lua : "..node.heap())
        semaphore2 = 1
        dofile("b.lua")
    end
    if(semaphore2 == 0)then
        print("Heap alla chiusura 1 di b.lua : "..node.heap())
        print("Heap alla chiusura di application.lua : "..node.heap())
        semaphore3 = 0
    else
        print("Heap al riavvio di application: "..node.heap())
        dofile("application.lua")
    end
else
    print("Heap al riavvio di application: "..node.heap())
    dofile("application.lua")
end

a.lua

print("A a.lua : "..node.heap())
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
print("B a.lua : "..node.heap())
local vol = file.mount("/SD0", 2)
print("C a.lua : "..node.heap())
local bios = file.open("/SD0/bios.bin","r")
print("D a.lua : "..node.heap())
print(bios)
print("E a.lua : "..node.heap())
print(seek)
print("F a.lua : "..node.heap())
bios:seek("set", seek)
print("G a.lua : "..node.heap())
bytes = bios.read(1)
print("H a.lua : "..node.heap())
seek = bios:seek("cur")
print("I a.lua : "..node.heap())
bios:close()
print("J a.lua : "..node.heap())
semaphore1 = 0
print("K a.lua : "..node.heap())
print("Heap alla chiusura 2 di a.lua : "..node.heap())

b.lua

print("Heap all'avvio 2 di b.lua : "..node.heap())
--if(bytes == nil)then
if(seek > 8)then
    mytimer:stop()
    print("FINISHED TO WRITE")
else 
    -- set SPI BIOS
    --spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
    --spi.set_mosi(1,0,8,bytes)
    --spi.transaction(1, 8,0x03, 24, index, 8, 0, 0)
    --index = index + 0x000001
end
semaphore2 = 0
print("Heap alla chiusura 2 di b.lua : "..node.heap())

And that are the Output Lines:

add if1
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100

_flash_used_end:402bb570
fs.start:c0000,max:33b000
_flash_used_end:402bb570
fs.start:c0000,max:33a000
_flash_used_end:402bb570
fs.start:d0000,max:32b000
_flash_used_end:402bb570
fs.start:d0000,max:32a000
mount res: 0, 0
Task task_lua started.
nul mode, fpm auto sleep set:enalbe

NodeMCU custom build by frightanic.com
    branch: master
    commit: 67027c0d05f7e8d1b97104e05a3715f6ebc8d07f
    SSL: true
    modules: adc,crypto,encoder,file,gpio,http,i2c,mdns,net,node,pwm,rtcfifo,rtcmem,rtctime,sjson,spi,tmr,uart,websocket,wifi,wifi_monitor,wps,tls
 build created on 2018-05-13 04:40
 powered by Lua 5.1.4 on SDK 2.2.1(cfd48f3)
> Heap size::38928.
Heap all'avvio del timer : 38928
Heap all'avvio del timer2 : 40216
Heap dopo assegnazione semaphore3 in timer2 : 40216
Heap all'avvio di application.lua : 37224
Heap all'avvio 1 di a.lua : 38248
A a.lua : 33744
B a.lua : 35904
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 35288
D a.lua : 34712
userdata: 0x3fff21b8
E a.lua : 34560
0
F a.lua : 34680
G a.lua : 34640
H a.lua : 34680
I a.lua : 34608
J a.lua : 35176
K a.lua : 35168
Heap alla chiusura 2 di a.lua : 35176
Heap alla chiusura 1 di a.lua : 35144
Heap dopo garbace collector : 37704
0 �
Heap dopo controllo semaphore1 in application : 37616
Heap all'avvio 1 di b.lua : 37568
Heap all'avvio 2 di b.lua : 36368
Heap alla chiusura 2 di b.lua : 36864
Heap alla chiusura 1 di b.lua : 36864
Heap alla chiusura di application.lua : 37584
Heap all'avvio del timer : 37576
Heap all'avvio del timer2 : 39552
Heap dopo assegnazione semaphore3 in timer2 : 39552
Heap all'avvio di application.lua : 36544
Heap all'avvio 1 di a.lua : 37584
A a.lua : 33088
B a.lua : 35248
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 34624
D a.lua : 34048
userdata: 0x3fff2598
E a.lua : 33896
1
F a.lua : 34016
G a.lua : 33976
H a.lua : 33976
I a.lua : 33976
J a.lua : 34544
K a.lua : 34544
Heap alla chiusura 2 di a.lua : 34544
Heap alla chiusura 1 di a.lua : 34512
Heap dopo garbace collector : 37064
1 �
Heap dopo controllo semaphore1 in application : 37000
Heap all'avvio 1 di b.lua : 36952
Heap all'avvio 2 di b.lua : 35760
Heap alla chiusura 2 di b.lua : 36256
Heap alla chiusura 1 di b.lua : 36256
Heap alla chiusura di application.lua : 36960
Heap all'avvio del timer : 36960
Heap all'avvio del timer2 : 38928
Heap dopo assegnazione semaphore3 in timer2 : 38928
Heap all'avvio di application.lua : 35896
Heap all'avvio 1 di a.lua : 36936
A a.lua : 32432
B a.lua : 34616
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 33992
D a.lua : 33408
userdata: 0x3fff23e8
E a.lua : 33240
2
F a.lua : 33376
G a.lua : 33328
H a.lua : 33336
I a.lua : 33328
J a.lua : 33904
K a.lua : 33896
Heap alla chiusura 2 di a.lua : 33904
Heap alla chiusura 1 di a.lua : 33872
Heap dopo garbace collector : 36416
2 �
Heap dopo controllo semaphore1 in application : 36336
Heap all'avvio 1 di b.lua : 36296
Heap all'avvio 2 di b.lua : 35080
Heap alla chiusura 2 di b.lua : 35576
Heap alla chiusura 1 di b.lua : 35576
Heap alla chiusura di application.lua : 36304
Heap all'avvio del timer : 36304
Heap all'avvio del timer2 : 38304
Heap dopo assegnazione semaphore3 in timer2 : 38304
Heap all'avvio di application.lua : 35296
Heap all'avvio 1 di a.lua : 36328
A a.lua : 31832
B a.lua : 34000
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 33376
D a.lua : 32800
userdata: 0x3fff2608
E a.lua : 32640
3
F a.lua : 32768
G a.lua : 32720
H a.lua : 32720
I a.lua : 32728
J a.lua : 33288
K a.lua : 33296
Heap alla chiusura 2 di a.lua : 33288
Heap alla chiusura 1 di a.lua : 33264
Heap dopo garbace collector : 35808
3 �
Heap dopo controllo semaphore1 in application : 35736
Heap all'avvio 1 di b.lua : 35696
Heap all'avvio 2 di b.lua : 34472
Heap alla chiusura 2 di b.lua : 34968
Heap alla chiusura 1 di b.lua : 34968
Heap alla chiusura di application.lua : 35704
Heap all'avvio del timer : 35704
Heap all'avvio del timer2 : 37680
Heap dopo assegnazione semaphore3 in timer2 : 37680
Heap all'avvio di application.lua : 34696
Heap all'avvio 1 di a.lua : 35736
A a.lua : 31232
B a.lua : 33392
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 32768
D a.lua : 32192
userdata: 0x3fff2128
E a.lua : 32040
4
F a.lua : 32160
G a.lua : 32120
H a.lua : 32120
I a.lua : 32112
J a.lua : 32688
K a.lua : 32680
Heap alla chiusura 2 di a.lua : 32688
Heap alla chiusura 1 di a.lua : 32664
Heap dopo garbace collector : 35216
4 �
Heap dopo controllo semaphore1 in application : 35144
Heap all'avvio 1 di b.lua : 35096
Heap all'avvio 2 di b.lua : 33896
Heap alla chiusura 2 di b.lua : 34392
Heap alla chiusura 1 di b.lua : 34392
Heap alla chiusura di application.lua : 35112
Heap all'avvio del timer : 35104
Heap all'avvio del timer2 : 37056
Heap dopo assegnazione semaphore3 in timer2 : 37056
Heap all'avvio di application.lua : 34088
Heap all'avvio 1 di a.lua : 35112
A a.lua : 30640
B a.lua : 32800
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 32176
D a.lua : 31600
userdata: 0x3fff2148
E a.lua : 31448
5
F a.lua : 31568
G a.lua : 31528
H a.lua : 31528
I a.lua : 31528
J a.lua : 32096
K a.lua : 32096
Heap alla chiusura 2 di a.lua : 32096
Heap alla chiusura 1 di a.lua : 32072
Heap dopo garbace collector : 34592
5 �
Heap dopo controllo semaphore1 in application : 34528
Heap all'avvio 1 di b.lua : 34480
Heap all'avvio 2 di b.lua : 33288
Heap alla chiusura 2 di b.lua : 33784
Heap alla chiusura 1 di b.lua : 33784
Heap alla chiusura di application.lua : 34488
Heap all'avvio del timer : 34488
Heap all'avvio del timer2 : 36432
Heap dopo assegnazione semaphore3 in timer2 : 36432
Heap all'avvio di application.lua : 33456
Heap all'avvio 1 di a.lua : 34488
A a.lua : 29984
B a.lua : 32144
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 31520
D a.lua : 30944
userdata: 0x3fff2128
E a.lua : 30792
6
F a.lua : 30912
G a.lua : 30872
H a.lua : 30872
I a.lua : 30864
J a.lua : 31440
K a.lua : 31432
Heap alla chiusura 2 di a.lua : 31440
Heap alla chiusura 1 di a.lua : 31416
Heap dopo garbace collector : 33968
6 �
Heap dopo controllo semaphore1 in application : 33896
Heap all'avvio 1 di b.lua : 33856
Heap all'avvio 2 di b.lua : 32648
Heap alla chiusura 2 di b.lua : 33136
Heap alla chiusura 1 di b.lua : 33136
Heap alla chiusura di application.lua : 33856
Heap all'avvio del timer : 33856
Heap all'avvio del timer2 : 35808
Heap dopo assegnazione semaphore3 in timer2 : 35808
Heap all'avvio di application.lua : 32816
Heap all'avvio 1 di a.lua : 33840
A a.lua : 29336
B a.lua : 31496
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 30872
D a.lua : 30296
userdata: 0x3fff2198
E a.lua : 30144
7
F a.lua : 30264
G a.lua : 30224
H a.lua : 30224
I a.lua : 30216
J a.lua : 30792
K a.lua : 30784
Heap alla chiusura 2 di a.lua : 30792
Heap alla chiusura 1 di a.lua : 30768
Heap dopo garbace collector : 33328
7 �
Heap dopo controllo semaphore1 in application : 33256
Heap all'avvio 1 di b.lua : 33208
Heap all'avvio 2 di b.lua : 32008
Heap alla chiusura 2 di b.lua : 32496
Heap alla chiusura 1 di b.lua : 32496
Heap alla chiusura di application.lua : 33208
Heap all'avvio del timer : 33208
Heap all'avvio del timer2 : 35184
Heap dopo assegnazione semaphore3 in timer2 : 35184
Heap all'avvio di application.lua : 32224
Heap all'avvio 1 di a.lua : 33240
A a.lua : 28704
B a.lua : 30864
Function platform_gpio_mode() is called. pin_mux:1610614844, func:0
C a.lua : 30240
D a.lua : 29664
userdata: 0x3fff2198
E a.lua : 29512
8
F a.lua : 29632
G a.lua : 29592
H a.lua : 29592
I a.lua : 29592
J a.lua : 30160
K a.lua : 30160
Heap alla chiusura 2 di a.lua : 30160
Heap alla chiusura 1 di a.lua : 30136
Heap dopo garbace collector : 32728
8 �
Heap dopo controllo semaphore1 in application : 32648
Heap all'avvio 1 di b.lua : 32600
Heap all'avvio 2 di b.lua : 31416
FINISHED TO WRITE
Heap alla chiusura 2 di b.lua : 31976
Heap alla chiusura 1 di b.lua : 31896
Heap alla chiusura di application.lua : 32616

I think the problems are is the allocation of vol and bios but i tried garbagecollecting, putting them nil and garbagecollecting, putting them local, putting them local and garbagecollecting. Lua VM doesn t free their space that takes circa 600 in the heap every cicle between B and C and C and D printing, please i need help on this

2
  • Can you trim your sample code even further (to circle in on the problematic lines) and maybe enable debug output when you build the firmware? Commented May 12, 2018 at 21:12
  • updated, please help me i need this to work Commented May 16, 2018 at 3:20

1 Answer 1

1

I discovered the problem. It s because usedata structure like vol and bios are made by c function calls. They can be collected by the garbage collector if there is no pointer to them but only if you use a method implemented by them to free their internal references to their private data allocated by their library in their creator function. So bios:close() is let bios to be relased, but vol, that hasn t no more a vol:umount() function implemented cant be released. So every cycle it allocate a new vol without disallocate the previous one

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.