⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s7600.h

📁 TI公司的CCS一些常用的函数库
💻 H
📖 第 1 页 / 共 3 页
字号:
   if (modem_response(MODEM_RESPONSE_TIMEOUT) == OK) {
      printf(s7600_Serial_Write,"ATDT%s\r\n",phone_number);
      return(modem_response(MODEM_CONNECT_TIMEOUT));
   }
   else {return(NO_MODEM_RESPONSE);}
}

/********************************************************************
//
// function: modem_response
//
// description: listens to and figures out the modem's AT response to AT commands we sent.
//
// input: to (time to wait, in ms, for a response from the modem)
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_Serial_kbhit(), s7600_Serial_Read(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec modem_response(int16 to)
{
   int1 done=0;
   int1 hit;
   int1 first=1;
   int1 crlf=0;
   int8 char1=0,char2=0;
   s7600_ec ret=BAD;

   while ((to != 0) && (!done)) {
      to--;
      hit=s7600_Serial_kbhit();
      if (hit && !crlf) {  //with local echo on wait until we hear the echo, which as after a crlf
         char1=s7600_Serial_Read();
         if ((char1 == 0x0D) || (char1 == 0x0A)) {crlf=1;}
      }
      else if (hit && first) {
         char1=s7600_Serial_Read();
         if ((char1 < 0x80) && (char1 > 0x1F)) {first=0;}
      }
      else if (hit && !first) {
         char2=s7600_Serial_Read();
         done=1;
      }
      else {delay_ms(1);}
   }

   if      ((done) && (char1 == 'O') && (char2 == 'K')) {ret=OK;}
   else if ((done) && (char1 == 'C') && (char2 == 'O')) {ret=MODEM_CONNECTED;}
   else if ((done) && (char1 == 'B') && (char2 == 'U')) {ret=MODEM_BUSY_SIGNAL;}
   else if ((done) && (char1 == 'N') && (char2 == 'O')) {ret=MODEM_NO_DIALTONE;}

   s7600_Serial_Flush();
   return(ret);
}


/********************************************************************
//
// function: modem_disconnect
//
// description: hangs up the modem
//
// input: none
// output: s7600_ec (error code type which gives a reason for error)
// calls: s7600_DMA_Write()
//
//*******************************************************************/
s7600_ec modem_disconnect(void)
{
#if USE_HWFC
   s7600_DMA_Write(IR_PORT_STAT_REG,0x20);//regain control and config communication port.
#else
   s7600_DMA_Write(IR_PORT_STAT_REG,0x00);//regain control and config communication port.
#endif
   delay_ms(100);
   printf(s7600_Serial_Write,"+++");
   delay_ms(5000);
   printf(s7600_Serial_Write,"ATH\r\n");
   //    return(modem_response(MODEM_RESPONSE_TIMEOUT+MODEM_RESPONSE_TIMEOUT));//wait double time
   return(OK);
}

/********************************************************************
//
// function: ppp_disconnect
//
// description: closes the PPP connection and then hangs up the modem. terminates TCPIP connection to internet
//
// input: none
// output: s7600_ec (error code type which gives a reason for error),
//         MyIPAddr, a global variable that has our IP address, cleared.
// calls: modem_disconnect(), s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
s7600_ec ppp_disconnect(void)
{
   int8 tmp;
   int16 to=0;

   tmp = s7600_DMA_Read(IR_PPP_STATUS_REG);
   tmp &= ~0x03;   // Write only PPP_EN and RST bit low
   s7600_DMA_Write(IR_PPP_STATUS_REG, tmp);

   while( (s7600_DMA_Read(IR_PPP_STATE) != 0x02) && (to++ < PPP_DISCONNECT_TIMEOUT) ) {delay_ms(1);}

   if (s7600_DMA_Read(IR_PPP_STATE) == 0x02) {}
   else {return(NO_PPP_DISCONNECT);}

   s7600_DMA_Write(IR_PPP_STATUS_REG, 0); //disable PPP and reset

   modem_disconnect();

   s7600_DMA_Write(IR_GENERAL, 0x01); //resets the s7600

   MyIPAddr=0;

   return(OK);
}

/********************************************************************
//
// function: ppp_keepalive
//
// description: kicks the PPP connection to keep it alive
//
// input: none
// output: none
// calls: ppp_check(), s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
void ppp_keepalive(void)
{
   int1 alive;
   int8 status;

   alive=ppp_check();

   if (!alive) {
      status=s7600_DMA_Read(IR_PPP_STATUS_REG);
      s7600_DMA_Write(IR_PPP_STATUS_REG, status | 0x04);
   }
}

/********************************************************************
//
// function: s7600_init
//
// description: initializes the s7600 to be used as a tcp/ip stack
//
// input: none
// output: none
// calls: s7600_init_clock(), s7600_serial_flush(), tcp_flush_socket(), s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
void s7600_init() {
   int16 to;
   int i;

   output_high(s7600_resetx);
   output_low(s7600_resetx);
   delay_ms(100);
   output_high(s7600_resetx);
   delay_ms(100);

   delay_ms(100);
   output_low(modem_reset);
   delay_ms(100);
   output_high(modem_reset);

   s7600_init_clock();

   for (i=0;i<MAX_SOCKET;i++) {
      to=TCP_TIMEOUT;
      s7600_DMA_Write(SOCKET_SELECT_REG,i);
      while ((s7600_DMA_Read(SOCKET_STATUS_HIGH) & 0x01) && (to)) {delay_ms(1);to--;}  //if not snd_busy
      if (to)  {
         s7600_DMA_Write(SOCKET_STATUS_REG,0x10);  //reset
         tcp_flush_socket(i);
      }
   }
   s7600_Serial_Flush();

   s7600_free_sockets();

   debug("\r\ns7600 Version: %X",s7600_DMA_Read(IR_VERSION));
}

/********************************************************************
//
// function: s7600_init_clock
//
// description: intializes the PWM to generate the proper clock for the s7600, and
//              sets the s7600 clock and baud rate generator SFRs.
//
// input: none
// output: none
// calls: s7600_DMA_Write(), s7600_DMA_Read()
//
//*******************************************************************/
void s7600_init_clock(void) {
   int16 divide_count,brDivVal;
   int16 duty;

   // The cycle time will be (1/clock)*4*t2div*(period+1)
   // where setup_timer_2(T2div, period, 1);
   // In this program clock=CLOCK_FREQ
   // When CLOCK_FREQ = 19.66MHz, PWM should generate 702KHz.
   setup_ccp1(CCP_PWM);               // Configure CCP1 as a PWM
   setup_timer_2(T2_DIV_BY_1, 6, 1);

   // if you change your clock speeds then you need to use this to set your duty frequency.
   // of course, s7600_CLOCK_FREQ is determined by the above PWM calculation
   //duty=(int16)((float)CLOCK_FREQ * ((float)1/s7600_CLOCK_FREQ)/2 );  //find duty setting for 50%

   // if using the picnet board by CCS then this setting is fine
   duty=14;

   set_pwm1_duty(duty);               // 50% duty cycle

   // set IR_CLOCK_DIV register to tell it that what clock speed we're at
   // if you change your clock speeds then you need to use this to set your frequency divider.
   // of course, s7600_CLOCK_FREQ is determined by the above PWM calculation
   //divide_count=(int16)((float)(s7600_CLOCK_FREQ/1000)-1);

   // if using the picnet board by CCS then this setting is fine
   divide_count=701;

   s7600_DMA_Write(IR_CLOCK_DIV + 0, make8(divide_count,0));
   s7600_DMA_Write(IR_CLOCK_DIV + 1, make8(divide_count,1));

   //regain control of serial port
   s7600_DMA_Write(IR_PORT_STAT_REG,0x00);//regain control and config communication port. (0x00 for no hw control, 0x20 for yes)

   //if not using CCS board then the following calculation will work
   // brDivVal = baud_rate;
   // brDivVal = (int16)( ( (s7600_CLOCK_FREQ ) / brDivVal ) - 1 );

   //if using CCS board you must use this setting because solsis/pctel model locked at 115.2k
   brDivVal = 5;

   s7600_DMA_Write(IR_BAUD_DIVIDER, make8(brDivVal,0));
   s7600_DMA_Write(IR_BAUD_DIVIDER+1, make8(brDivVal,1));
}


/********************************************************************
//
// function: s7600_free_sockets
//
// description: makes sure all the sockets are disabled, so that they are free for later
//
// input: none
// output: none
// calls: s7600_DMA_Write()
//
//*******************************************************************/
void s7600_free_sockets(void) {
         s7600_DMA_Write(SOCKET_ACTIVATE_REG, 0);
}

/********************************************************************
//
// function: s7600_Serial_Write
//
// description: sends a byte of data out to the modem.  Same as putc(), but the destination is the modem
//
// input: data (data to send)
// output: none
// calls: s7600_DMA_Write()
//
//*******************************************************************/
void s7600_Serial_Write(char data)
{
   s7600_DMA_Write(IR_PORT_DATA_REG,data);  //send data to Serial_Port_Data address
   delay_ms(5);
}

/********************************************************************
//
// function: s7600_serial_kbhit
//
// description: checks to see if there is data in the modem -> s7600 data buffer
//
// input: none
// output: boolean; true if there is data available from modem, false if data is not available
// calls: s7600_DMA_Read()
//
//*******************************************************************/
int1 s7600_Serial_kbhit(void)
{
   int8 status;

   status=s7600_DMA_Read(IR_PORT_STAT_REG);

   if (status & 0x80) {return(1);}
   else {return(0);}
}

/********************************************************************
//
// function: s7600_serial_read
//
// description: gets data from the modem that was sent to the s7600, just like getc().
//              NOTE: this makes no checks as to whether the data is valid or not.  use s7600_serial_kbhit() first.
//
// input: none
// output: data from the modem
// calls: s7600_DMA_Read()
//
//*******************************************************************/
char s7600_Serial_Read(void)
{
   int8 data;

   data=s7600_DMA_Read(IR_PORT_DATA_REG);

   return(data);
}

/********************************************************************
//
// function: s7600_serial_flush
//
// description: clears the modem -> s7600 receive buffer
//
// input: none
// output: none
// calls: s7600_DMA_Read()
//
//*******************************************************************/
void s7600_Serial_Flush(void)
{
   while (s7600_Serial_kbhit()) {
      s7600_Serial_Read();
   }
}

/********************************************************************
//
// function: s7600_DMA_Write
//
// description: writes data to the s7600 using the bus between the PIC and the s7600
//
// input: address (s7600 SFR to write to), data (data to write)
// output: none
// calls: none
//
//*******************************************************************/
void s7600_DMA_Write(char address, char data)
{
   while(!input(s7600_busyx)) {}   // Check if S-7600A is busy

   disable_interrupts(GLOBAL);

   output_high(s7600_writex);
   output_high(s7600_readx);
   output_low(s7600_rs);
   output_high(s7600_cs);   // 1st cycle sets register address
   s7600_buscontrol();
   s7600_busout(address);
   output_low(s7600_writex);
   delay_cycles(1);
   output_high(s7600_writex);

   s7600_busfloat();

   output_low(s7600_cs);
   output_low(s7600_readx);
   output_high(s7600_rs);

   output_high(s7600_readx);
   output_high(s7600_cs);
   s7600_buscontrol();
   s7600_busout(data);
   output_low(s7600_writex);
   delay_cycles(1);
   output_high(s7600_writex);
   s7600_busfloat();
   output_low(s7600_cs);
   output_low(s7600_readx);

   while(!input(s7600_busyx)) {}   // Check if S-7600A is busy

   enable_interrupts(GLOBAL);
}

/********************************************************************
//
// function: s7600_DMA_Read
//
// description: reads data from the s7600 using the bus between the PIC and the s7600
//
// input: address (s7600 SFR to read)
// output: none
// calls: none
//
//*******************************************************************/
char s7600_DMA_Read(char address)
{
   char data;

   while(!input(s7600_busyx)) {}   // Check if S-7600A is busy

   disable_interrupts(GLOBAL);

   output_high(s7600_writex);
   output_high(s7600_readx);
   output_low(s7600_rs);
   output_high(s7600_cs);

   s7600_buscontrol();
   s7600_busout(address);  // Write address
   output_low(s7600_writex);
   delay_cycles(1);
   output_high(s7600_writex);

   s7600_busfloat();

   output_low(s7600_cs);
   output_high(s7600_rs);
   output_low(s7600_rs);
   output_high(s7600_cs);

   output_low(s7600_readx);
   data=s7600_busin();  //read back the address (odd interface, but whatever)
   output_high(s7600_readx);

   output_low(s7600_cs);
   output_high(s7600_rs);

   while(!input(s7600_busyx)) {}   // Check if S-7600A is busy

   output_high(s7600_cs);
   output_low(s7600_readx);
   data=s7600_busin();
   output_high(s7600_readx);

   output_low(s7600_cs);

   enable_interrupts(GLOBAL);

   return (data);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -