Shop OBEX P1 Docs P2 Docs Learn Events
Trouble with multiple instances of FatEngine — Parallax Forums

Trouble with multiple instances of FatEngine

average joeaverage joe Posts: 795
edited 2015-01-15 08:32 in Propeller 1
Hi guys, I'm sure this has been answered before but my google foo just isn't working.

I'm trying to log up to 4 serial inputs to independent files. So I have fullduplexSerial4port + 4 instances of FatEngine. Launch a cog to read each serial stream and write to a file. I think everything is right, up to closing the file... Now when I do a capture, I end up with a corrupted SD card?

CON Stuff

  _clkmode = xtal1 + pll16x
  _clkfreq = 80_000_000

  ' pins and stuff
  _RXn1         = 0
  _TXn1         = 1
  _BAUDn1       = 115_200
  _TRn1         = 15
  _n1           = 0
  
  _RXn2         = 2
  _TXn2         = 3
  _BAUDn2       = 115_200
  _TRn2         = 14
  _n2           = 1

  _RXbase       = 4
  _TXbase       = 5
  _BAUDbase     = 115_200
  _base         = 2

  _button       = 13

  _numOfPorts   = 3
  _stackSize    = 250
  
obj 

  fat[3] : "SD-MMC_FATEngine.spin"
  pins : "pins.spin"                
  ser   : "FullDuplexSerial4portPlus_0v3"

VAR
  long  stack[_numOfPorts * _stackSize], runFlag[_numOfPorts]
  byte  cog[_numOfPorts]
   
DAT
        fileNptrs       long @filename1, @filename2, @filename3, @filename4 

        filename1        byte    "Log000s1.csv", 0
        filename2        byte    "Log000s2.csv", 0
        filename3        byte    "Log000s3.csv", 0
        filename4        byte    "Log000s4.csv", 0
        
PUB Run
     pins.updateTicks
     dira[27..26] := %11    
     outa[27..26] := %10      
    ser.init
    ser.AddPort(_n1,_RXn1,_TXn1,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDn1) 
    ser.AddPort(_n2,_RXn2,_TXn2,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDn2) 
    ser.AddPort(_base,_RXbase,_TXbase,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDbase)
    ' one more port if we want
    ser.start    
     outa[27..26] := %01      
     repeat while ina[_button]
     outa[27..26] := %11

     fat.FATEngineStart(pins#_DOPin, pins#_CLKPin, pins#_DIPin, pins#_CSPin, -1, -1, -1, -1, -1)
     
     outa[26] := 1
     dira[27] := 0
     Start       
     repeat until ina[_button]
     pins.pause1ms(1000) 
     repeat while ina[_button]

     Stop
     outa[26] := 0
             
     repeat
       waitcnt(cnt)  
  
PUB Start | n
     repeat n from 0 to (_numOfPorts - 1)
       runFlag[n] := true
       cog[n] := cognew(Ser2SD(n),@stack[n * _stackSize]) + 1
          
PUB Stop | n
  repeat n from 0 to (_numOfPorts -1)
    if cog[n]                                                                   ' if driver is running
      runflag[n] := false     
  pins.pause1ms(5000)

  fat.unmountPartition
  pins.pause1ms(1000)
  fat.FATEngineStop
                     
PUB Ser2SD(port) | b[128], n, c
     fat[port].mountPartition(0)           
     fat[port].newFile(fileNptrs[port])
     fat[port].openFile(fileNptrs[port], "W")

     n := 0
     repeat while runFlag[port]
       if (c := ser.rxcheck(port)) <> -1
         b.byte[n ++] := c
         if c == 13 or n == 512
           fat[port].writeData(@b, n)
           n := 0
           dira[27] := 1
           outa[27] := 1
           pins.pause1ms(100)
           dira[27] := 0 
               
     fat[port].writeData(@b, n ) 
     fat[port].closeFile
     cogstop(cogid)
     

*edit*
It actually looks like I'm having problems with the name array now!

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2015-01-15 08:12
    From a quick glance, when you access fileNptrs[port] you should in fact use @@fileNptrs[port], otherwise your filenames are not what you expect.
  • average joeaverage joe Posts: 795
    edited 2015-01-15 08:32
    kuroneko wrote: »
    From a quick glance, when you access fileNptrs[port] you should in fact use @@fileNptrs[port], otherwise your filenames are not what you expect.

    Sure enough! That fixed it.
    obj 
    
      fat[_numOfPorts] : "SD-MMC_FATEngine.spin"
      pins : "pins.spin"                
      ser   : "FullDuplexSerial4portPlus_0v3"
    
    VAR
      long  stack[_numOfPorts * _stackSize], runFlag[_numOfPorts]
      byte  cog[_numOfPorts]
       
    DAT
            fileNptrs        long @filename1, @filename2, @filename3, @filename4 
    
            filename1        byte    "Log000s1.csv", 0
            filename2        byte    "Log000s2.csv", 0
            filename3        byte    "Log000s3.csv", 0
            filename4        byte    "Log000s4.csv", 0
            
    PUB Run
         pins.updateTicks
         dira[27..26] := %11    
         outa[27..26] := %10      
        ser.init
        case _numOfPorts
          3 :
            ser.AddPort(_base,_RXbase,_TXbase,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDbase)
            ser.AddPort(_n1,_RXn1,_TXn1,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDn1) 
            ser.AddPort(_n2,_RXn2,_TXn2,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDn2) 
        ' one more port if we want
          2 :
            ser.AddPort(_base,_RXbase,_TXbase,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDbase)
            ser.AddPort(_n1,_RXn1,_TXn1,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDn1) 
          1:
            ser.AddPort(_base,_RXbase,_TXbase,ser#PINNOTUSED,ser#PINNOTUSED,ser#DEFAULTTHRESHOLD,ser#NOMODE,_BAUDbase)
      ser.start    
    
      outa[27..26] := %01      
      repeat while ina[_button]
      outa[27..26] := %11
    
      Start       
      repeat until ina[_button]
      pins.pause1ms(1000) 
      repeat while ina[_button]
      Stop
                 
         repeat
           waitcnt(cnt)  
      
    PUB Start | n
         fat.FATEngineStart(pins#_DOPin, pins#_CLKPin, pins#_DIPin, pins#_CSPin, -1, -1, -1, -1, -1)     
         outa[26] := 1
         dira[27] := 0
         repeat n from 0 to (_numOfPorts - 1)
           runFlag[n] := true
           cog[n] := cognew(Ser2SD(n),@stack[n * _stackSize]) + 1
              
    PUB Stop | n
      repeat n from 0 to (_numOfPorts -1)
        if cog[n]                                                                   ' if driver is running
          runflag[n] := false     
      pins.pause1ms(5000)
    
      'pins.pause1ms(1000)
      fat.FATEngineStop
      outa[26] := 0
                       
    PUB Ser2SD(port) | b[128], n, c
         fat[port].mountPartition(0)           
         fat[port].newFile(@@fileNptrs[port])
         fat[port].openFile(@@fileNptrs[port], "W")
    
         n := 0
         repeat while runFlag[port]
           if (c := ser.rxcheck(port)) <> -1
             b.byte[n ++] := c
             if c == 13 or n == 512
               fat[port].writeData(@b, n)
               n := 0
               dira[27] := 1
               outa[27] := 1
               pins.pause1ms(100)
               dira[27] := 0 
                   
         fat[port].writeData(@b, n ) 
         fat[port].closeFile
         fat[port].unmountPartition
      
         cogstop(cogid)
    

    This works, but I don't like the idea of using static names. What I'd like to do is have a name prototype, say "log"xxx"p"x".csv" and have the filename increment. I think the key is listEntry(entryPathName) but haven't quite wrapped my head around it.
Sign In or Register to comment.