Statistics
| Branch: | Tag: | Revision:

mongoose / examples / TM4C129 / ccs / Mongoose_TM4C129_BM / main.c @ eaef5bd1

History | View | Annotate | Download (5.51 KB)

1
/*
2
 * Copyright (c) 2014-2016 Cesanta Software Limited
3
 * All rights reserved
4
 *
5
 *
6
 * This example sets up a simple HTTP server using the Mongoose multi-protocol
7
 * networking library.
8
 *
9
 * It assumes TivaWare is installed under
10
 * ${TI_PRODUCTS_DIR}/TivaWare_C_Series-2.1.3.156. ${TI_PRODUCTS_DIR} is usually
11
 * C:\ti on Windows, ~/ti on Linux.
12
 *
13
 * If you have a different version installed, please edit the SDK_ROOT path
14
 * variable under Project -> Properties -> Resource -> Linked Resources.
15
 */
16

    
17
#include <stdbool.h>
18
#include <stdint.h>
19

    
20
#include "mongoose.h"
21

    
22
#include "inc/hw_ints.h"
23
#include "inc/hw_memmap.h"
24
#include "driverlib/flash.h"
25
#include "driverlib/interrupt.h"
26
#include "driverlib/gpio.h"
27
#include "driverlib/rom_map.h"
28
#include "driverlib/sysctl.h"
29
#include "driverlib/systick.h"
30
#include "utils/lwiplib.h"
31
#include "utils/ustdlib.h"
32
#include "utils/uartstdio.h"
33
#include "drivers/pinout.h"
34

    
35
#define SYSTICKHZ 100
36
#define SYSTICKMS (1000 / SYSTICKHZ)
37

    
38
struct mg_mgr g_mgr;
39

    
40
// The main Mongoose event handler.
41
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
42
  static int led_value = 0;
43
  if (ev == MG_EV_POLL) return;
44
  // UARTprintf("%p: ev %d\r\n", nc, ev);
45
  switch (ev) {
46
    case MG_EV_ACCEPT: {
47
      char addr[32];
48
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
49
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
50
      UARTprintf("%p: Connection from %s\r\n", nc, addr);
51
      break;
52
    }
53
    case MG_EV_HTTP_REQUEST: {
54
      struct http_message *hm = (struct http_message *) ev_data;
55
      char addr[32];
56
      mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
57
                          MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
58
      UARTprintf("%p: HTTP request\r\n", nc);
59
      mg_send_response_line(nc, 200,
60
                            "Content-Type: text/html\r\n"
61
                            "Connection: close");
62
      mg_printf(nc,
63
                "\r\n<h1>Hello, %s!</h1>\r\n"
64
                "You asked for %.*s\r\n",
65
                addr, (int) hm->uri.len, hm->uri.p);
66
      nc->flags |= MG_F_SEND_AND_CLOSE;
67
      led_value ^= CLP_D1;
68
      LEDWrite(CLP_D1, led_value);
69
      break;
70
    }
71
    case MG_EV_CLOSE: {
72
      UARTprintf("%p: Connection closed\r\n", nc);
73
      break;
74
    }
75
  }
76
}
77

    
78
int gettimeofday(struct timeval *tv, void *tzvp) {
79
  tv->tv_sec = time(NULL);
80
  tv->tv_usec = 0;
81
  return 0;
82
}
83

    
84
void mg_lwip_mgr_schedule_poll(struct mg_mgr *mgr) {
85
}
86

    
87
// Special IP address values that lwiplib uses.
88
#define IP_LINK_DOWN (0xffffffffU)
89
#define IP_LINK_UP (0)
90

    
91
// Invoked by lwiplib every HOST_TMR_INTERVAL ms (defined in lwipopts.h).
92
void lwIPHostTimerHandler(void) {
93
  static uint32_t old_ip = IP_LINK_DOWN;
94
  uint32_t ip = lwIPLocalIPAddrGet();
95
  if (ip != old_ip) {
96
    switch (ip) {
97
      case IP_LINK_DOWN: {
98
        UARTprintf("Link down.\r\n");
99
        LEDWrite(CLP_D2, 0);
100
        break;
101
      }
102
      case IP_LINK_UP: {
103
        UARTprintf("Link up.\r\n");
104
        LEDWrite(CLP_D2, 0);
105
        break;
106
      }
107
      default: {
108
        UARTprintf("IP: http://%s/\r\n", ipaddr_ntoa((const ip_addr_t *) &ip));
109
        LEDWrite(CLP_D2, CLP_D2);
110
        break;
111
      }
112
    }
113
    old_ip = ip;
114
  }
115

    
116
  mg_mgr_poll(&g_mgr, 0);
117
}
118

    
119
void SysTickIntHandler(void) {
120
  // Call the lwIP timer handler.
121
  lwIPTimer(SYSTICKMS);
122
}
123

    
124
int main(void) {
125
  //
126
  // Make sure the main oscillator is enabled because this is required by
127
  // the PHY.  The system must have a 25MHz crystal attached to the OSC
128
  // pins. The SYSCTL_MOSC_HIGHFREQ parameter is used when the crystal
129
  // frequency is 10MHz or higher.
130
  //
131
  SysCtlMOSCConfigSet(SYSCTL_MOSC_HIGHFREQ);
132

    
133
  // Run from the PLL at 120 MHz.
134
  uint32_t sys_clk =
135
      MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
136
                              SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480),
137
                             120000000);
138

    
139
  // Configure the device pins.
140
  PinoutSet(true, false);
141

    
142
  // Configure UART.
143
  UARTStdioConfig(0, 115200, sys_clk);
144
  UARTprintf("Mongoose example\n\n");
145

    
146
  // Configure board's LEDs: D2 is on when IP has been acquired,
147
  // D1 is toggled on every HTTP request.
148
  MAP_GPIOPinTypeGPIOOutput(CLP_D1_PORT, CLP_D1_PIN);
149
  MAP_GPIOPinTypeGPIOOutput(CLP_D2_PORT, CLP_D2_PIN);
150
  LEDWrite(CLP_D1, 0);
151
  LEDWrite(CLP_D2, 0);
152

    
153
  //
154
  // Configure the hardware MAC address for Ethernet Controller filtering of
155
  // incoming packets.  The MAC address will be stored in the non-volatile
156
  // USER0 and USER1 registers.
157
  //
158
  uint32_t u0, u1;
159
  MAP_FlashUserGet(&u0, &u1);
160
  uint8_t mac[6];
161
  mac[0] = ((u0 >> 0) & 0xff);
162
  mac[1] = ((u0 >> 8) & 0xff);
163
  mac[2] = ((u0 >> 16) & 0xff);
164
  mac[3] = ((u1 >> 0) & 0xff);
165
  mac[4] = ((u1 >> 8) & 0xff);
166
  mac[5] = ((u1 >> 16) & 0xff);
167

    
168
  // Initialize the lwIP library, using DHCP.
169
  lwIPInit(sys_clk, mac, 0, 0, 0, IPADDR_USE_DHCP);
170

    
171
  // Configure Mongoose, create an HTTP listener.
172
  mg_mgr_init(&g_mgr, NULL);
173
  const char *err;
174
  struct mg_bind_opts opts = {};
175
  opts.error_string = &err;
176
  struct mg_connection *nc = mg_bind_opt(&g_mgr, "80", ev_handler, opts);
177
  if (nc == NULL) {
178
    UARTprintf("Failed to create listener: %s\r\n", err);
179
    return 1;
180
  }
181
  mg_set_protocol_http_websocket(nc);
182

    
183
  // Configure SysTick for a periodic interrupt.
184
  MAP_SysTickPeriodSet(sys_clk / SYSTICKHZ);
185
  MAP_SysTickEnable();
186
  MAP_SysTickIntEnable();
187

    
188
  // Loop forever.  All the work is done in interrupt handlers.
189
  while (true) {
190
  }
191
}