Net_Ethernet_28j60 dspic library

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Hacky35
Posts: 15
Joined: 13 Aug 2013 21:12

Net_Ethernet_28j60 dspic library

#1 Post by Hacky35 » 04 Mar 2014 21:46

Hi everyone,

The following code does not work. If you have a friend who can run, please help me.

I tried the C code of the library, is compiled without problems.
but not compiled Basic code.

Code: Select all

program HTTP_Demo_28j60

'* Project Name:
'     HTTP_Demo_28j60
' * Copyright:
'     (c) Mikroelektronika, 2011.
' *
' * description  :
' *      this code shows how to use the Net_Ethernet_28j60 library :
' *              the board will reply to ARP & ICMP echo requests
' *              the board will reply to UDP requests on any port :
' *                      returns the request in upper char with a header made of remote host IP & port number
' *              the board will reply to HTTP requests on port 80.
' *
' *
' * Test configuration:
'     MCU:             P33FJ256GP710A
'     Dev.Board:       LV24-33 v6
'                      http://www.mikroe.com/eng/products/view/430/lv-24-33-v6-development-system/
'     Oscillator:      XT-PLL, 80.000MHz
'     ext. modules:    Serial Ethernet Board
'                      http://www.mikroe.com/eng/products/view/14/serial-ethernet-board/
'
'     SW:              mikroBasic PRO for dsPIC
'                      http://www.mikroe.com/eng/products/view/229/mikrobasic-pro-for-dspic/
' * NOTES:
'     - External power supply should be used due to Serial Ethernet Board power consumption.
'     - Because page contains 4 images, We need minimum 5 socket (defined in _Lib_NetEthEnc28j60_Defs.c file), one for HTML page
'       and one for each image.

include "recources"

' mE ehternet NIC pinout
dim
  Net_Ethernet_28j60_Rst  as sbit at LATF0_bit
  Net_Ethernet_28j60_CS  as sbit at LATF1_bit
  Net_Ethernet_28j60_Rst_Direction  as sbit at TRISF0_bit
  Net_Ethernet_28j60_CS_Direction  as sbit at TRISF1_bit
' end ethernet NIC definitions


const httpHeader       as string[69] = "HTTP/1.1 200 OK"+Chr(10)+"Content-Length: 7787"+Chr(10)+"Connection: close"+Chr(10)+"Content-type: "   ' HTTP header
const httpMimeTypeHTML as string[11] = "text/html"+Chr(10)+Chr(10)               ' HTML MIME type
const httpImage        as string[64] = "HTTP/1.1 200 OK"+Chr(10)+"Connection: keep-alive"+Chr(10)+"Content-type: image/jpg"+Chr(10)+Chr(10)            ' TEXT MIME type
const httpMethod       as string[5] = "GET /"
const httpRequ         as string[14] = "GET / HTTP/1.1"

const HTML_CODE_SIZE = 5822
const IMAGE1_SIZE    = 43959
const IMAGE2_SIZE    = 5168
const IMAGE3_SIZE    = 4103
const IMAGE4_SIZE    = 3778


dim myMacAddr   as byte[6]   ' my MAC address
    myIpAddr    as byte[4]   ' my IP address
    gwIpAddr    as byte[4]   ' gateway (router) IP address
    ipMask      as byte[4]   ' network mask (for example : 255.255.255.0)
    dnsIpAddr   as byte[4]   ' DNS server IP address
    IP as byte[4]
    dim sock as ^SOCKET_28j60_Dsc
dim  getRequest  as byte[15]  ' HTTP request buffer
     dyna        as char[30]  ' buffer for dynamic response
     httpCounter as word                ' counter of HTTP requests


''''''''''''''''''''''''''''''''''''''''
' Values of global variables in _Lib_NetEthEnc28j60_Defs.mpas file.
'
'NUM_OF_SOCKET_28j60 = 5      ' Max number of socket We can open.
'TCP_TX_SIZE_28j60 = 512      ' Size of Tx buffer in RAM.
'MY_MSS_28j60 = 30            ' Our maximum segment size.
'SYN_FIN_WAIT_28j60 = 3       ' Wait-time (in second) on remote SYN/FIN segment.
'RETRANSMIT_WAIT_28j60 = 3    ' Wait-time (in second) on ACK which we expect.
'
''''''''''''''''''''''''''''''''''''''''


dim
    send_mark1, send_mark2, send_mark3, send_mark4, sendHTML_mark as byte
    socketImage1, socketImage2, socketImage3, socketImage4, socketHTML as ^SOCKET_28j60_Dsc
    pos as word[NUM_OF_SOCKET_28j60]

dim tmp_p1, tmp_p2, tmp_p3, tmp_p4, tmp_pH as ^const far byte

sub procedure Net_Ethernet_28j60_UserTCP(dim socket as ^SOCKET_28j60_Dsc)
dim
   ch, i  as char

  ' I listen only to web request on port 80
  if(socket^.destPort <> 80) then
    exit
  end if


  ' get 10 first bytes only of the request, the rest does not matter here

  for i = 0 to 9
    getRequest[i] = Net_Ethernet_28j60_getByte()
  next i
  getRequest[i+1] = 0

  ' only GET method is supported here
  if(memcmp(@getRequest, @httpMethod, 5)and(socket^.state <> 3)) then
    exit
  end if


  if(memcmp(@getRequest, @httpRequ, 9)=0)then
    sendHTML_mark = 1
    socketHTML    = socket
  end if

  ' Check if getRequest contains image request (example: GET /i/p1.jpg):
  if(getRequest[7] = "p") then

    if(getRequest[8] = "1") then
      send_mark1 = 1
      socketImage1 = socket
    else
      if(getRequest[8] = "2") then
        send_mark2 = 1
        socketImage2 = socket
      else
        if(getRequest[8] = "3") then
          send_mark3 = 1
          socketImage3 = socket
        else
          if(getRequest[8] = "4") then
            send_mark4 = 1
            socketImage4 = socket
          end if
        end if
      end if
    end if

  end if

  '............................................................................
  ' Send picture p1.jpg.
  if( (send_mark1 = 1)and(socketImage1 = socket) ) then
    ' First put HTTP heder.
    if (pos[socket^.ID]=0) then
      tmp_p1 = @p1
      Net_Ethernet_28j60_putConstStringTCP(@httpImage, socket)
    end if
    ' Put picture data.
    while(pos[socket^.ID] < IMAGE1_SIZE)
      ' Put byte in TCP Tx buffer. If overflow occure, do break, and wait for next call of UserTCP, for this socket.
      ch = Net_Ethernet_28j60_putByteTCP(tmp_p1^, socket)
      if(ch = 0) then
        break
      end if
      __AddToFarPointer(tmp_p1,1)
      pos[socket^.ID] = pos[socket^.ID] + 1
    wend

    ' Close connection if all data sent.
    if( Net_Ethernet_28j60_bufferEmptyTCP(socket) and (pos[socket^.ID] >= IMAGE1_SIZE) ) then
      Net_Ethernet_28j60_disconnectTCP(socket)
      send_mark1 = 0
      pos[socket^.ID] = 0
    end if
  end if


  '............................................................................
  ' Send picture p2.jpg.
  if( (send_mark2 = 1)and(socketImage2 = socket) ) then
    ' First put HTTP heder.
    if (pos[socket^.ID]=0) then
      tmp_p2 = @p2
      Net_Ethernet_28j60_putConstStringTCP(@httpImage, socket)
    end if
    ' Put picture data.
    while(pos[socket^.ID] < IMAGE2_SIZE)
      ' Put byte in TCP Tx buffer. If overflow occure, do break, and wait for next call of UserTCP, for this socket.
      ch = Net_Ethernet_28j60_putByteTCP(tmp_p2^, socket)
      if(ch = 0) then
        break
      end if
      __AddToFarPointer(tmp_p2,1)
      pos[socket^.ID] = pos[socket^.ID] + 1
    wend

    ' Close connection if all data sent.
    if( Net_Ethernet_28j60_bufferEmptyTCP(socket) and (pos[socket^.ID] >= IMAGE2_SIZE) ) then
      Net_Ethernet_28j60_disconnectTCP(socket)
      send_mark2 = 0
      pos[socket^.ID] = 0
    end if
  end if

  '............................................................................
  ' Send picture p3.jpg.
  if( (send_mark3 = 1)and(socketImage3 = socket) ) then
    ' First put HTTP heder.
    if (pos[socket^.ID]=0) then
      tmp_p3 = @p3
      Net_Ethernet_28j60_putConstStringTCP(@httpImage, socket)
    end if
    ' Put picture data.
    while(pos[socket^.ID] < IMAGE3_SIZE)
      ' Put byte in TCP Tx buffer. If overflow occure, do break, and wait for next call of UserTCP, for this socket.
      ch = Net_Ethernet_28j60_putByteTCP(tmp_p3^, socket)
      if(ch = 0) then
        break
      end if
      __AddToFarPointer(tmp_p3,1)
      pos[socket^.ID] = pos[socket^.ID] + 1
    wend

    ' Close connection if all data sent.
    if( Net_Ethernet_28j60_bufferEmptyTCP(socket) and (pos[socket^.ID] >= IMAGE3_SIZE) ) then
      Net_Ethernet_28j60_disconnectTCP(socket)
      send_mark3 = 0
      pos[socket^.ID] = 0
    end if
  end if

  '............................................................................
  ' Send picture p4.jpg.
  if( (send_mark4 = 1)and(socketImage4 = socket) ) then
    ' First put HTTP heder.
    if (pos[socket^.ID]=0) then
      tmp_p4 = @p4
      Net_Ethernet_28j60_putConstStringTCP(@httpImage, socket)
    end if
    ' Put picture data.
    while(pos[socket^.ID] < IMAGE4_SIZE)
      ' Put byte in TCP Tx buffer. If overflow occure, do break, and wait for next call of UserTCP, for this socket.
      ch = Net_Ethernet_28j60_putByteTCP(tmp_p4^, socket)
      if(ch = 0) then
        break
      end if
      __AddToFarPointer(tmp_p4,1)
      pos[socket^.ID] = pos[socket^.ID] + 1
    wend

    ' Close connection if all data sent.
    if( Net_Ethernet_28j60_bufferEmptyTCP(socket) and (pos[socket^.ID] >= IMAGE4_SIZE) ) then
      Net_Ethernet_28j60_disconnectTCP(socket)
      send_mark4 = 0
      pos[socket^.ID] = 0
    end if
  end if

  '............................................................................
  ' Send picture html page.
  if((sendHTML_mark = 1)and(socketHTML = socket)) then
    if(pos[socket^.ID]=0) then
      ' Send HTTP header.
      Net_Ethernet_28j60_putConstStringTCP(@httpHeader, socket)
      Net_Ethernet_28j60_putConstStringTCP(@httpMimeTypeHTML, socket)
      tmp_pH = @html_code
    end if
    while(pos[socket^.ID] < HTML_CODE_SIZE)
      ch = Net_Ethernet_28j60_putByteTCP(tmp_pH^, socket)
      if(ch = 0) then
        break
      end if
      __AddToFarPointer(tmp_pH,1)
      pos[socket^.ID] = pos[socket^.ID] + 1
    wend
    ' Close connection if all data sent.
    if( Net_Ethernet_28j60_bufferEmptyTCP(socket) and (pos[socket^.ID] >= HTML_CODE_SIZE) ) then
      Net_Ethernet_28j60_disconnectTCP(socket)
      sendHTML_mark = 0
      pos[socket^.ID] = 0
    end if
  end if
end sub

''''''''''''''''''''''''''''''''''''''''

sub function Net_Ethernet_28j60_UserUDP(dim udpDsc as ^UDP_28j60_Dsc) as word
dim
   l as word                                    ' my reply length
   tmp as string[5]

   result = 0
    ' reply is made of the remote host IP address in human readable format
    byteToStr(udpDsc^.remoteIP[0], dyna)            ' first IP address byte
    dyna[3] = "."
    byteToStr(udpDsc^.remoteIP[1], tmp)             ' second
    dyna[4] = tmp[0]
    dyna[5] = tmp[1]
    dyna[6] = tmp[2]
    dyna[7] = "."
    byteToStr(udpDsc^.remoteIP[2], tmp)             ' second
    dyna[8] = tmp[0]
    dyna[9] = tmp[1]
    dyna[10] = tmp[2]
    dyna[11] = "."
    byteToStr(udpDsc^.remoteIP[3], tmp)             ' second
    dyna[12] = tmp[0]
    dyna[13] = tmp[1]
    dyna[14] = tmp[2]

    dyna[15] = ":"                           ' add separator

    ' then remote host port number
    WordToStr(udpDsc^.remotePort, tmp)
    dyna[16] = tmp[0]
    dyna[17] = tmp[1]
    dyna[18] = tmp[2]
    dyna[19] = tmp[3]
    dyna[20] = tmp[4]
    dyna[21] = "["
    WordToStr(udpDsc^.destPort, tmp)
    dyna[22] = tmp[0]
    dyna[23] = tmp[1]
    dyna[24] = tmp[2]
    dyna[25] = tmp[3]
    dyna[26] = tmp[4]
    dyna[27] = "]"
    dyna[28] = 0




  ' the total length of the request is the length of the dynamic string plus the text of the request
  l = 28 + udpDsc^.dataLength

  ' puts the dynamic string into the transmit buffer
  Net_Ethernet_28j60_putBytes(@dyna, 28)

  ' then puts the request string converted into upper char into the transmit buffer

  while(l <> 0)
        Net_Ethernet_28j60_putByte(toupper(Net_Ethernet_28j60_getByte()))
        l = l - 1
  wend

  result = 28 + udpDsc^.dataLength
end sub

dim cnt as word
' Initialization of Timer1
sub procedure MCUInit()

  PLLFBD = 0x1E   ' with 10 MHz input
                   ' PLLFBD must be set to 0x1E  to make 40 MHz output
                   ' see family reference for details
  AD1PCFGL = 0xFFFF          ' initialize AN pins as digital
  AD1PCFGH = 0xFFFF          ' initialize AN pins as digital
  AD2PCFGL = 0xFFFF          ' initialize AN pins as digital

  TRISB  = 0                 ' initialize PORTB as output
  LATB   = 0xAAAA            ' initialize PORTB value
  IPC0   = IPC0 or 0x1000    ' interrupt priority level = 1
  T1IF_bit = 0               ' clear T1IF
  T1IE_bit = 1               ' enable Timer1 interrupts

  T1CON = 0x8010             ' Timer1 ON, internal clock FCY, prescaler 1:256

end sub

' Interrupt routine Timer1
dim timer_tmp as byte
sub procedure Timer1Int() iv IVT_ADDR_T1INTERRUPT
  T1IF_bit = 0             ' clear T1IF
  timer_tmp = timer_tmp + 1
  if(timer_tmp > 38) then
    Net_Ethernet_28j60_UserTimerSec = Net_Ethernet_28j60_UserTimerSec + 1
    LATD = not PORTD           ' invert PORTB
    timer_tmp = 0
  end if
end sub

dim i as char

main:
        send_mark1 = 0
        send_mark2 = 0
        send_mark3 = 0
        send_mark4 = 0
        sendHTML_mark = 0


        PORTD = 0
        TRISD = 0                 ' set PORTD as output

        ' set mac address
        myMacAddr[0] = 0x00
        myMacAddr[1] = 0x14
        myMacAddr[2] = 0xA5
        myMacAddr[3] = 0x76
        myMacAddr[4] = 0x19
        myMacAddr[5] = 0x3F

        IP[0] = 192
        IP[1] = 168
        IP[2] = 20
        IP[3] = 200


        ' set IP address
        myIpAddr[0] = 192
        myIpAddr[1] = 168
        myIpAddr[2] = 20
        myIpAddr[3] = 61

        ' set gateway address
        gwIpAddr[0]  = 192
        gwIpAddr[1]  = 168
        gwIpAddr[2]  = 20
        gwIpAddr[3]  = 6

        ' set dns address
        dnsIpAddr[0] = 192
        dnsIpAddr[1] = 168
        dnsIpAddr[2] = 20
        dnsIpAddr[3] = 1

        ' set subnet mask
        ipMask[0]    = 255
        ipMask[1]    = 255
        ipMask[2]    = 255
        ipMask[3]    = 0

        MCUInit()

        for i = 0 to (NUM_OF_SOCKET_28j60 - 1)
          pos[i] = 0
        next i

        Net_Ethernet_28j60_stackInitTCP()

        SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_4,
                            _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_IDLE_2_ACTIVE)

        Net_Ethernet_28j60_Init(@myMacAddr, @myIpAddr, Net_Ethernet_28j60_FULLDUPLEX)  ' init ethernet board
        Net_Ethernet_28j60_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

        Delay_ms(1000)
        while(1)

         if PortB = 0 then
          
          Net_Ethernet_28j60_connectTCP(IP, 2000, 10000, @sock)
          end if
        wend
end.
Thank you

Hacky

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: Net_Ethernet_28j60 dspic library

#2 Post by dejan.odabasic » 19 Mar 2014 19:30

Hello,

In order for library to process the incoming packets, Net_Ethernet_28j60_doPacket() need to be called in your main endless loop.

Best regards.

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”