Shop OBEX P1 Docs P2 Docs Learn Events
multi-thread use of W5100_Indirect_Driver.spin — Parallax Forums

multi-thread use of W5100_Indirect_Driver.spin

JavalinJavalin Posts: 892
edited 2012-03-01 17:49 in Accessories
All,

I'd like to raise/highlight an issue in the W5100_Indirect_Driver.spin driver relating to use of the W5100 chip from multiple Propeller cog's. As Microsoft would say - its not "thread safe"....

The ReadIND and WriteIND functions don't wait for the cog to complete its current action before setting the command variable and waiting output. For example if you're using (as I am) 3 sockets for a web server in one cog, and one for a dedicated client socket in another, it all goes badly wrong when both access the W5100 at the same time.

"fixed" code below:
PUB readIND(_register, _dataPtr, _Numbytes)

'' High level access to Indirect/parallel routine for reading from the W5100.
'' Note for faster execution of functions code them in assembly routine like the examples of setting the MAC/IP addresses.
''
''  params:  _register is the 2 byte register address.  See the constant block with register definitions
''           _dataPtr is the place to return the byte(s) of data read from the W5100 (use the @ in front of the byte variable)
''           _Numbytes is the number of bytes to read
''  return:  none

  'If the ASM cog is running, execute the command
  if (W5100flags & _Flag_ASMstarted)

    ' wait for the current command to complete
    repeat while command

    'Send the command
    command := _readIND + @_register

    'wait for the command to complete
    repeat while command

  return 'end of readIND

PUB writeIND(_block, _register, _dataPtr, _Numbytes)

'' High level access to Indirect/parallel routine for writing to the W5100.
'' Note for faster execution of functions code them in assembly routine like the examples of setting the MAC/IP addresses.
''
''  params:  _block if true will wait for ASM routine to send before continuing
''           _register is the 2 byte register address. See the constant block with register definitions
''           _dataPtr is a pointer to the byte(s) of data to be written (use the @ in front of the byte variable)
''           _Numbytes is the number of bytes to write
''  return:  none

  'If the ASM cog is running, execute the command
  if (W5100flags & _Flag_ASMstarted)

    ' wait for the current command to complete
    repeat while command

    'Send the command
    command := _writeIND + @_register

    'wait for the command to complete (forced - declaration left as is for compatibility)
    'if _block
    repeat while command

  return 'end of writeIND

The writeIND function - i've commented out the if _block statement as the function always needs to wait.

James

Comments

  • JavalinJavalin Posts: 892
    edited 2012-03-01 04:10
    continuing this - I've found some more functions that need to be updated with a:
              'wait for PASM to be idle
          repeat while command
    

    Before the command variable is set - i.e.
    PUB Initialize(_socket, _mode, _srcPort, _destPort, _destIP)
    
    '' Open the specified socket in the specified mode on the W5100.
    '' The mode can be either TCP or UDP.
    ''
    ''  params:  _socket is a value of 0 to 3 - only four sockets on the W5100
    ''           _mode is one of the constants specifing closed, TCP, UDP, IPRaw etc
    ''           _srcPort, _destPort are the ports to use in the connection pass by value
    ''           _destIP is a pointer to the destination IP byte array (use the @on the variable)
    ''  return:  none
    
      'If the ASM cog is running, execute the command
      if (W5100flags & _Flag_ASMstarted)
        'If the socket has memory allocated, execute the command
        if (rx_mask_S[_socket] <> 0)
    
          'wait for PASM to be idle
          repeat while command
        
          'Send the command
          command := _Sopen + @_socket
           
          'wait for the command to complete
          repeat while command
    
      return 'end of SocketOpen
    

    This needs to be done in the following functions : Initialize, Disconnect and Close

    James
  • Mike GMike G Posts: 2,702
    edited 2012-03-01 17:49
    Nice work Javalin
Sign In or Register to comment.