[En-Nut-Discussion] PPP/memory problem

p3stk4 nut at krazniki.pl
Tue Oct 16 16:58:22 CEST 2007


Welcome,

I'm writting a program for device which has to connect via GPRS modem
send (MC35i) and some information to the server. Everything was good
until program become bigger. Than I've noticed a problem with PPP
driver. Without any reason it cannot establish connection (modem says
ERROR/OK) or device hangup or restart.
It seams to be a memory - like problem, because sometimes the
configuration structure in the memory is overwritten with some data. 

How can I solve this problem?

The device is:
ATmega128 + 64 KB sram memory (3 wait states, one block)
One RS232
and nothing more.

Configuration file used to build Nut/OS:

PLATFORM = "ETHERNUT1"
AVR_GCC = ""
MCU_ATMEGA128 = ""
NUTMEM_START = "0x100"
NUTMEM_RESERVED = "64"
NUTXMEM_SIZE = "0xEE00"
NUTXMEM_START = "0x1100"
NUT_CPU_FREQ = "16000000"

Software:
avr-gcc 4.2.1-1
avr-libc 1.4.5-2
binutils-avr 2.17
Nut/OS 4.4.0.0
Ubuntu Gutsy Gibbon RC




The main below.
Regards,
Daniel Lewandowski



/*
 * open modem as uart device
 */
int modem_open (void)
{
  u_long lctl;
  char buffer[64];

  strcpy_P(buffer, PSTR("ppp:" PPPCOM "/"));
  strncat(buffer, config->pppuser, PPPUSER_MAX);
  strcat_P(buffer, PSTR("/"));
  strncat(buffer, config->ppppass, PPPPASS_MAX);

  uart = _open(buffer, _O_RDWR | _O_BINARY);
  if (uart == -1) {
    return -1;
  }

  lctl = UARTSPEED;
  _ioctl(uart, UART_SETSPEED, &lctl);

  lctl = 1;
  _ioctl(uart, UART_SETRAWMODE, &lctl);

  modem_rxto(UARTFSTRXTO);

  _ioctl(uart, HDLC_SETIFNET, 0);
  NutSleep(2000);

  return 0;
}


/*
 * close raw uart 
 */
int modem_close(void)
{
  return _close(uart);
}


/*
 * make a gprs connection active
 */
int modem_gprs_open(void)
{
  int rc;
  u_long lctl;
  char buffer[256];

  lctl = 0;
  _ioctl(uart, UART_SETRAWMODE, &lctl);

  strcpy_P(buffer, PSTR("TIMEOUT 30"
			" ABORT ERROR"
			" ABORT 'NO CARRIER'"
			" ABORT 'NO DIALTONE'"
			" '' ATH OK AT&D1 OK"
			" AT+CGDCONT=1,\"IP\",\""));
  strncat(buffer, config->pppapn, PPPAPN_MAX);
  strcat_P(buffer, PSTR("\" OK ATD*99***1# CONNECT"));

  rc = NutChat(uart, buffer);
  return rc;
}


int modem_gprs_close(void)
{
  int rc = 0;
  u_long lctl;

  _ioctl(uart, HDLC_SETIFNET, 0);
  NutSleep(2000);

  MODEM_PORT &= ~_BV(MODEM_DTR); // DTR low

  lctl = 1;
  _ioctl(uart, UART_SETRAWMODE, &lctl);

  NutSleep(3000);


  MODEM_PORT |= _BV(MODEM_DTR); // DTR high
  NutSleep(500);


  return rc;
}


/*
 * create request frame
 */
static void modem_frame_request(char *buffer);
static void modem_frame_alarm(char *buffer);
static void modem_frame_logged(char *buffer);
static void modem_frame_keepalive(char *buffer);

static int send_data(u_long ip, u_short port, void *data, u_short bytes)
{
  int rc;
  u_char *msg = (u_char*)data;
  u_long sock_to = 5000;
  TCPSOCKET *const sock = NutTcpCreateSocket();

  if (sock == NULL) {
    return -1;
  }

  NutTcpSetSockOpt(sock, SO_RCVTIMEO, &sock_to, sizeof(sock_to));
  NutTcpSetSockOpt(sock, SO_SNDTIMEO, &sock_to, sizeof(sock_to));

  rc = NutTcpConnect(sock, ip, port);
  if (rc == 0) {
    while(bytes > 0) {
      rc = NutTcpSend(sock, msg, bytes);
      if (rc > 0) {
	bytes -= rc;
	msg += rc;
      }
      else if (rc == 0) {
	break;
      }
      else {
	break;
      }
    }
  }

  rc = NutTcpCloseSocket(sock);
  if (rc) {
    return -1;
  }

  return 0;
}


/*
 * send string message to the server
 */
static int send_msg(u_long ip, u_short port, char *msg)
{
  return send_data(ip, port, msg, strlen(msg));
}



char buffer[64];

/*
 * main loop
 */
int main (void)
{
  DEVICE_STATE state;
  int rc;

  u_char alarm_phone_index = 0;
  u_long interval = 0;
  u_long next_slot = 0;
  PPPDCB *dcb;

  state = INITIALIZE;

  for(;;) {
    NutSleep(0);
    watchdog_clear();
    switch (state) {
    case INITIALIZE:
      config_init();
      adc_init();
      leds_init();
      amplifier_init();
      buttons_init();
      modem_init();

      modem_open();
      modem_ignite();
      state = IDLE;


    case IDLE:
      state = READ_SMS;
      break;


    case OPEN_GPRS:
      NutNetIfConfig("ppp", 0, 0, 0); 
      dcb = devPpp.dev_dcb;
      NutDnsConfig2(0, 0, dcb->dcb_ip_dns1, dcb->dcb_ip_dns2);
      NutIpRouteAdd(0, 0, dcb->dcb_remote_ip, &devPpp);

      state = WORKING;
      break;


    case CLOSE_GPRS:
      modem_gprs_close();
      state = IDLE;
      break;


    case WORKING:
	...
      break;
    }
  }

  return 0;
}





More information about the En-Nut-Discussion mailing list