CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Timer not working with TCPIP stack

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

Timer not working with TCPIP stack
PostPosted: Tue Dec 15, 2020 3:54 am     Reply with quote

I have been having problems in a project attempting to use a combination of TCPIP stack, ADC and timers.
This project uses a dsPIC33EP512MC806 with the version 5.096 compiler (have tried some slightly older versions with no change).

I have been able to resolve the problems, but I can't understand why the problems occured or why my change fixed it.

The code below compiles and runs but timer2_isr() is never reached. Somehow adc1_isr is reached, and hangs for some time.

This is resolved by moving the TCPIP stack StackInit() call to after enable_interrupts(INTR_GLOBAL) (or by removing the TCPIP stack initialisation entirely).

My mistake for calling StackInit() without interrupts enabled, but can anyone offer an explanation why this mistake shows up as a broken timer (rather than the TCPIP stack failing for example)?

main.c:
Code:

#include <33EP512MC806.h>

/*
TCP/IP Stack enabled.
Many TCP/IP configuration settings (servers enabled, ports used,
etc) are defined in TCPIPConfig.h.
Many hardware configuration settings (SPI port and GPIO pins used)
are defined in HardwareProfile.h.
*/

#define STACK_USE_UDP

#include "tcpip/PCDxxxx.h"

#pragma use delay( crystal=12Mhz, clock=120Mhz, AUX:clock=48Mhz )

#define __PIC24F__

#include <stdint.h>

#define MIN(a,b)  ((a > b) ? b : a)
#define wf_debug_printf(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)

#include "tcpip/TCPIPConfig.h"
#include "tcpip/HardwareProfile.h"
#include "tcpip/StackTsk2.h"

#if TCP_CONFIGURATION > 0
   TCPSocketInitializer_t TCPSocketInitializer[TCP_CONFIGURATION] =
   {
      #if defined(STACK_USE_CCS_HTTP2_SERVER)
         {TCP_PURPOSE_HTTP_SERVER, TCP_ETH_RAM, STACK_CCS_HTTP2_SERVER_TX_SIZE, STACK_CCS_HTTP2_SERVER_RX_SIZE}
      #endif
      #if defined(STACK_USE_SMTP_CLIENT)
         {TCP_PURPOSE_DEFAULT, TCP_ETH_RAM, STACK_CCS_SMTP_TX_SIZE, STACK_CCS_SMTP_RX_SIZE}
      #endif
      #if defined(STACK_USE_MY_TELNET_SERVER)
         {TCP_PURPOSE_TELNET, TCP_ETH_RAM, STACK_MY_TELNET_SERVER_TX_SIZE, STACK_MY_TELNET_SERVER_RX_SIZE}
      #endif
      #if defined(STACK_USE_CCS_HTTP_CLIENT)
         {TCP_PURPOSE_GENERIC_TCP_CLIENT, TCP_ETH_RAM, STACK_MY_HTTPC_TX_SIZE, STACK_MY_HTTPC_RX_SIZE}
      #endif
   };
#else
   #undef TCP_CONFIGURATION
   #define TCP_CONFIGURATION 1
   TCPSocketInitializer_t TCPSocketInitializer[TCP_CONFIGURATION] =
   {
      {TCP_PURPOSE_DEFAULT, TCP_ETH_RAM, 250, 250}
   };
#endif

#include "tcpip/StackTsk2.c"

// Serial Output
#pin_select U1TX=PIN_F2
#pin_select U1RX=PIN_B12
#use rs232(UART1, baud=115200, parity = N, bits = 8, errors, TIMEOUT = 1, stream=DEBUGSTREAM)

#word U1STA = getenv("SFR:U1STA")
#bit U1TRMT = U1STA.8

#define DEBUGPRINTF( x ) fprintf ( DEBUGSTREAM, x )
#define DEBUG_FLUSH() while(!U1TRMT) {}

int main (int argc, char* argv[])
{
   setup_wdt(WDT_4S);

   TickInit();
   StackInit();

   setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 11999);
   enable_interrupts(INT_TIMER2);

   setup_adc_ports(sAN4, VSS_VREF);
   setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_4);
   set_adc_channel(4);
   enable_interrupts(INT_ADC1);

   DEBUGPRINTF("\r\nInitialised");
   DEBUG_FLUSH();

   enable_interrupts(INTR_GLOBAL);
   
   while (TRUE)
   {
      restart_wdt();
   }
   
   return 0;
}

#INT_TIMER2
void timer2_isr(void)
{
    DEBUGPRINTF("\r\nTimer5");
    DEBUG_FLUSH();

    read_adc(ADC_START_ONLY);
}

#INT_ADC1
void adc1_isr(void)
{
    DEBUGPRINTF("\r\nADC Interrupt");
    DEBUG_FLUSH();

    int16 adc;

    adc = read_adc(ADC_READ_ONLY);
}


TCPIPConfig.h:
Code:

#ifndef __TCPIPCONFIG_H
#define __TCPIPCONFIG_H

#include "GenericTypeDefs.h"
#include "Compiler.h"

#define STACK_CCS_SMTP_TX_SIZE    0
#define STACK_CCS_SMTP_RX_SIZE    0

#define STACK_CCS_HTTP2_SERVER_TX_SIZE    0
#define STACK_CCS_HTTP2_SERVER_RX_SIZE    0

#define STACK_MY_HTTPC_RX_SIZE    0
#define STACK_MY_HTTPC_TX_SIZE    0

#define STACK_MY_TELNET_SERVER_TX_SIZE    0
#define STACK_MY_TELNET_SERVER_RX_SIZE    0

#define MY_DEFAULT_HOST_NAME      "CONTROLLER"

#define MY_DEFAULT_MAC_BYTE1      (0x02)
#define MY_DEFAULT_MAC_BYTE2      (0x00)
#define MY_DEFAULT_MAC_BYTE3      (0x43)
#define MY_DEFAULT_MAC_BYTE4      (0x19)
#define MY_DEFAULT_MAC_BYTE5      (0x05)
#define MY_DEFAULT_MAC_BYTE6      (0x01)

#define MY_DEFAULT_IP_ADDR_BYTE1  (172ul)
#define MY_DEFAULT_IP_ADDR_BYTE2  (16ul)
#define MY_DEFAULT_IP_ADDR_BYTE3  (1ul)
#define MY_DEFAULT_IP_ADDR_BYTE4  (1ul)

#define MY_DEFAULT_GATE_BYTE1     (172ul)
#define MY_DEFAULT_GATE_BYTE2     (16ul)
#define MY_DEFAULT_GATE_BYTE3     (1ul)
#define MY_DEFAULT_GATE_BYTE4     (1ul)

#define MY_DEFAULT_MASK_BYTE1     (255ul)
#define MY_DEFAULT_MASK_BYTE2     (255ul)
#define MY_DEFAULT_MASK_BYTE3     (0ul)
#define MY_DEFAULT_MASK_BYTE4     (0ul)

#define TCP_CONFIGURATION      0

#define TCP_ETH_RAM_SIZE (STACK_CCS_SMTP_TX_SIZE + \
                          STACK_CCS_SMTP_RX_SIZE + \
                          STACK_CCS_HTTP2_SERVER_TX_SIZE + \
                          STACK_CCS_HTTP2_SERVER_RX_SIZE + \
                          STACK_MY_TELNET_SERVER_TX_SIZE + \
                          STACK_MY_TELNET_SERVER_RX_SIZE + \
                          STACK_MY_HTTPC_TX_SIZE + \
                          STACK_MY_HTTPC_RX_SIZE + \
                          100*TCP_CONFIGURATION)

// Define names of socket types
#define TCP_PURPOSE_GENERIC_TCP_CLIENT 0
#define TCP_PURPOSE_GENERIC_TCP_SERVER 1
#define TCP_PURPOSE_TELNET             2
#define TCP_PURPOSE_FTP_COMMAND        3
#define TCP_PURPOSE_FTP_DATA           4
#define TCP_PURPOSE_TCP_PERFORMANCE_TX 5
#define TCP_PURPOSE_TCP_PERFORMANCE_RX 6
#define TCP_PURPOSE_UART_2_TCP_BRIDGE  7
#define TCP_PURPOSE_HTTP_SERVER        8
#define TCP_PURPOSE_DEFAULT            9
#define TCP_PURPOSE_BERKELEY_SERVER    10
#define TCP_PURPOSE_BERKELEY_CLIENT    11
#define TCP_PURPOSE_CCS_SMTP           0x40

typedef struct
{
   BYTE vSocketPurpose;
   BYTE vMemoryMedium;
   WORD wTXBufferSize;
   WORD wRXBufferSize;
} TCPSocketInitializer_t;

#ifndef MAX_HTTP_CONNECTIONS
   #define  MAX_HTTP_CONNECTIONS 1
#endif
#ifndef MAX_UDP_SOCKETS
   #define MAX_UDP_SOCKETS 7
#endif

#endif


Can post the other TCPIP setup files, not sure if they're relevant here.
Ttelmah



Joined: 11 Mar 2010
Posts: 19439

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 4:18 am     Reply with quote

The TCP stack is dependant on tick for it's internal timings. That is why
the example calls TickInit, then enables the global interrupt before anything
else. If the global interrupt is not enabled, the tick dependant timings all
won't work....
You can #define TICK_ISR_POLLING and it'll then poll the tick functions
instead of using the interrupt.
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 4:25 am     Reply with quote

I can certainly understand the tick dependant timings of the stack itself not working, but why does my timer2 also not work (not used by the TCP stack)?
Ttelmah



Joined: 11 Mar 2010
Posts: 19439

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 4:39 am     Reply with quote

In the TCP_STUB structure, there is a bit field bTimer2Enabled. If this is set
to '1', it uses timer2 as well. Problem is the functions doing this are
Microchip functions not CCS functions, so a search for the CCS functions
won't find that it is used....
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 5:01 am     Reply with quote

Searching for the CCS functions is exactly what I had done, so that makes sense.
Does the bTimer2Enabled flag refer to timer2 as in setup_timer2() (and so should I avoid using timer2 myself?), or is it simply a secondary timer?
Ttelmah



Joined: 11 Mar 2010
Posts: 19439

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 7:00 am     Reply with quote

One would have to sit down and dismantle what the MicroChip code actually
does. There may be remarks in the master sources from MicroChip, but the
current ones are for a much later stack.
There is a setting in the MHC configuration 'TimerInstanceToUse', which
probably controls what it actually does. People do seem to refer to the TCP/IP
stack using Timer2. Where, I do not know!...

Looking at it, the other possibility is it 'fiddles' with the interrupt enables,
hence the affect on INT_TIMER2. Possibly a direct register access, so
a complex search to find it!... Sad
SamMeredith



Joined: 13 Jul 2018
Posts: 23

View user's profile Send private message

PostPosted: Tue Dec 15, 2020 7:45 am     Reply with quote

My original problem is not specific to timer2, I see the same result if I use timer3/4/5.
In my actual project I use multiple timers and only noticed this behaviour on one of them (timer5 iirc).
That all suggests to me that the MicroChip code is not referring to a specific timer (in the setup_timer() sense) but simply some secondary timer (in the general sense).

Regardless, it is easy to avoid the issue now I know what I was doing wrong. Dissecting the MicroChip code is beyond me for now, so I'll have to leave it at that.

Thanks for your help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group