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

📄 client.c

📁 基于lm3s6916的LEDClient示例代码
💻 C
字号:
/*----------------------------------------------------------------------------
 *      R T L   T C P N E T   E x a m p l e
 *----------------------------------------------------------------------------
 *      Name:    CLIENT.C
 *      Purpose: Example LED Control Client application
 *      Rev.:    V3.12
 *----------------------------------------------------------------------------
 *      This code is part of the RealView Run-Time Library.
 *      Copyright (c) 2004-2007 KEIL - An ARM Company. All rights reserved.
 *---------------------------------------------------------------------------*/

#include <RTL.h>
#include <stdio.h>
#include <LM3Sxxxx.H>
#include <string.h>
#include "..\..\..\osram128x64x4.h"

//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------

//   <h>Remote IP Address
//   ====================
//
//     <o>IP1: Address byte 1 <0-255>
//     <i> Default: 192
#define IP1            192

//     <o>IP2: Address byte 2 <0-255>
//     <i> Default: 168
#define IP2            168

//     <o>IP3: Address byte 3 <0-255>
//     <i> Default: 0
#define IP3            0

//     <o>IP4: Address byte 4 <0-255>
//     <i> Default: 100
#define IP4            100

//   </h>

//   <o>Remote Port <1-65535>
//   <i> Do not set number of port too small,
//   <i> maybe it is already used.
//   <i> Default: 1001
#define PORT_NUM       1001

//   <o>Communication Protocol <0=> TCP <1=> UDP
//   <i> Selecet a protocol for sending data.
#define PROTOCOL       1

//   <o>LED Blinking speed <1-100>
//   <i> Blinking speed = SPEED * 100ms
//   <i> Default: 2
#define SPEED          2


//------------- <<< end of configuration section >>> -----------------------


#define BLINKLED 0x01  /* Command for blink the leds on board */
#define SENDLEN  2     /* Number of bytes to send */
#define TCP      0
#define UDP      1

BOOL tick;
U8 socket_tcp;
U8 socket_udp;
U8 Rem_IP[4] = {IP1,IP2,IP3,IP4};


/*--------------------------- init ------------------------------------------*/

static void init () {
   /* Add System initialisation code here */

   /* Set the clocking to run from the PLL at 50 MHz */
   SysCtlClockSet (SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
                   SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ);

   /* Configure the GPIO for the LED. */
   SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF);
   GPIOPadConfigSet (GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA,
                     GPIO_PIN_TYPE_STD);
   GPIODirModeSet (GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_DIR_MODE_OUT);

   /* Configure UART0 for 115200 baud. */
   SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA);
   SysCtlPeripheralEnable (SYSCTL_PERIPH_UART0);
   GPIOPinTypeUART (GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
   UARTConfigSet(UART0_BASE, 115200, (UART_CONFIG_WLEN_8 |
                                      UART_CONFIG_STOP_ONE |
                                      UART_CONFIG_PAR_NONE));

   /* Setup and enable the SysTick timer for 100ms. */
   SysTickPeriodSet(SysCtlClockGet() / 10);
   SysTickEnable();
}


/*--------------------------- init_display ----------------------------------*/

static void init_display () {
   /* OLED Module init */

   OSRAM128x64x4Init(1000000);
   OSRAM128x64x4Clear ();
   OSRAM128x64x4StringDraw ("    RL-ARM      ", 20, 20, 11);
   OSRAM128x64x4StringDraw (" Client example ", 20, 35, 11);
}


/*--------------------------- timer_poll ------------------------------------*/

static void timer_poll () {
   /* System tick timer running in poll mode */

   if ((HWREG (NVIC_ST_CTRL) >> 16) & 1) {
      /* Timer tick every 100 ms */
      timer_tick ();
      tick = __TRUE;
   }
}


/*--------------------------- fputc -----------------------------------------*/

int fputc (int ch, FILE *f)  {
   /* Debug output to serial port. */

   if (ch == '\n')  {
      UARTCharPut (UART0_BASE, '\r');        /* output CR                    */
   }
   UARTCharPut (UART0_BASE, ch);
   return (ch);
}


/*--------------------------- UDP socket ------------------------------------*/

U16 udp_callback (U8 soc, U8 *rip, U16 rport, U8 *buf, U16 len) {
   /* This function is called by the UDP module when UDP packet is received. */

   /* Make a reference to suppress compiler warnings. */
   buf  = buf;
   rip  = rip;
   rport= rport;
   len  = len;
   soc  = soc;
   return (0);
}


/*--------------------------- TCP socket ------------------------------------*/

U16 tcp_callback (U8 soc, U8 evt, U8 *ptr, U16 par) {
   /* This function is called by the TCP module on TCP event */
   /* Check the Net_Config.h for possible events.            */

   /* Make a reference to suppress compiler warnings. */
   soc = soc;
   par = par;
   evt = evt;
   ptr = ptr;
   return (0);
}


/*--------------------------- TCP send --------------------------------------*/

void send_data (U8 p2val) {
   U8 *sendbuf;
   U8 p2;

   /* UDP */
   if (socket_udp != 0) {
      /* Start Connection */
      sendbuf = udp_get_buf (SENDLEN);
      sendbuf[0] = BLINKLED;
      sendbuf[1] = p2val;
      udp_send (socket_udp, Rem_IP, 1001, sendbuf, SENDLEN);
   }

   /* TCP */
   if (socket_tcp != 0) {
      /* Start Connection */
      p2 = p2val;
      switch (tcp_get_state(socket_tcp)) {
         case TCP_STATE_FREE:
         case TCP_STATE_CLOSED:
            tcp_connect (socket_tcp, Rem_IP, PORT_NUM, 0);
            break;
         case TCP_STATE_CONNECT:
            if (tcp_check_send (socket_tcp) == __TRUE) {
               sendbuf = tcp_get_buf(SENDLEN);
               sendbuf[0] = BLINKLED;
               sendbuf[1] = p2;
               tcp_send (socket_tcp, sendbuf, SENDLEN);
            }
            break;
      }
   }
}


/*--------------------------- main ------------------------------------------*/

int main (void) {
   /* Main Thread of the TcpNet */
   U8 p2val, cnt, lshf;
   U8 protocol;

   init ();
   init_display ();
   init_TcpNet ();

   protocol = PROTOCOL;
   switch (protocol) {
      case TCP:
         socket_tcp = tcp_get_socket (TCP_TYPE_CLIENT, 0, 10, tcp_callback);
         break;
      case UDP:
         socket_udp = udp_get_socket (0, UDP_OPT_SEND_CS | UDP_OPT_CHK_CS, udp_callback);
         if (socket_udp != 0) {
            udp_open (socket_udp, PORT_NUM);
         }
         break;
   }

   p2val = 1;
   cnt   = 0;
   lshf  = 1;  /* left shift */
   while (1) {
      timer_poll ();
      main_TcpNet ();
      if (tick == __TRUE) {
         if (++cnt == SPEED) {
            GPIOPinWrite (GPIO_PORTF_BASE, GPIO_PIN_0, p2val & 1);
            send_data(p2val);
            p2val = lshf ? (p2val << 1) : (p2val >> 1);
            if (p2val == 0x80) lshf = 0;
            if (p2val == 0x01) lshf = 1;
            cnt = 0;
        }
        tick = __FALSE;
     }
   }
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/


⌨️ 快捷键说明

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