Statistics
| Branch: | Tag: | Revision:

mongoose / examples / CC3200 / wifi.c @ eaef5bd1

History | View | Annotate | Download (5.3 KB)

1
#include "wifi.h"
2

    
3
#include "mongoose.h"
4

    
5
#include <simplelink/cc_pal.h>
6
#include <simplelink/include/wlan.h>
7

    
8
#include <inc/hw_types.h>
9

    
10
#include <driverlib/gpio.h>
11
#include <driverlib/utils.h>
12

    
13
#include <example/common/gpio_if.h>
14
#include "cs_dbg.h"
15

    
16
void SimpleLinkWlanEventHandler(SlWlanEvent_t *e) {
17
  switch (e->Event) {
18
    case SL_WLAN_CONNECT_EVENT:
19
      LOG(LL_INFO, ("WiFi: connected, getting IP"));
20
      break;
21
    case SL_WLAN_STA_CONNECTED_EVENT:
22
      LOG(LL_INFO, ("WiFi: station connected"));
23
      break;
24
    case SL_WLAN_STA_DISCONNECTED_EVENT:
25
      LOG(LL_INFO, ("WiFi: station disconnected"));
26
      break;
27
    default:
28
      LOG(LL_INFO, ("WiFi: event %d", e->Event));
29
  }
30
}
31

    
32
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) {
33
  if (e->Event == SL_NETAPP_IPV4_IPACQUIRED_EVENT) {
34
    SlIpV4AcquiredAsync_t *ed = &e->EventData.ipAcquiredV4;
35
    LOG(LL_INFO, ("IP acquired: %lu.%lu.%lu.%lu", SL_IPV4_BYTE(ed->ip, 3),
36
                  SL_IPV4_BYTE(ed->ip, 2), SL_IPV4_BYTE(ed->ip, 1),
37
                  SL_IPV4_BYTE(ed->ip, 0)));
38
    GPIO_IF_LedToggle(MCU_RED_LED_GPIO);
39
  } else if (e->Event == SL_NETAPP_IP_LEASED_EVENT) {
40
    LOG(LL_INFO, ("IP leased"));
41
  } else {
42
    LOG(LL_INFO, ("NetApp event %d", e->Event));
43
  }
44
}
45

    
46
bool wifi_setup_ap(const char *ssid, const char *pass, int channel) {
47
  uint8_t v;
48
  if (sl_WlanSetMode(ROLE_AP) != 0) {
49
    return false;
50
  }
51
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid),
52
                 (const uint8_t *) ssid) != 0) {
53
    return false;
54
  }
55
  v = strlen(pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN;
56
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v) != 0) {
57
    return false;
58
  }
59
  if (v == SL_SEC_TYPE_WPA &&
60
      sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(pass),
61
                 (const uint8_t *) pass) != 0) {
62
    return false;
63
  }
64
  v = channel;
65
  if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, &v) != 0) {
66
    return false;
67
  }
68
  sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID);
69
  {
70
    SlNetCfgIpV4Args_t ipcfg;
71
    memset(&ipcfg, 0, sizeof(ipcfg));
72
    if (!inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4) ||
73
        !inet_pton(AF_INET, "255.255.255.0", &ipcfg.ipV4Mask) ||
74
        /* This means "disable". 0.0.0.0 won't do. */
75
        !inet_pton(AF_INET, "255.255.255.255", &ipcfg.ipV4DnsServer) ||
76
        /* We'd like to disable gateway too, but DHCP server refuses to start.
77
           */
78
        !inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4Gateway) ||
79
        sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4,
80
                     sizeof(ipcfg), (uint8_t *) &ipcfg) != 0) {
81
      return false;
82
    }
83
  }
84
  {
85
    SlNetAppDhcpServerBasicOpt_t dhcpcfg;
86
    memset(&dhcpcfg, 0, sizeof(dhcpcfg));
87
    dhcpcfg.lease_time = 900;
88
    if (!inet_pton(AF_INET, "192.168.4.20", &dhcpcfg.ipv4_addr_start) ||
89
        !inet_pton(AF_INET, "192.168.4.200", &dhcpcfg.ipv4_addr_last) ||
90
        sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT,
91
                     sizeof(dhcpcfg), (uint8_t *) &dhcpcfg) != 0) {
92
      return false;
93
    }
94
  }
95
  sl_Stop(0);
96
  sl_Start(NULL, NULL, NULL);
97
  if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) {
98
    LOG(LL_ERROR, ("DHCP server failed to start"));
99
    return false;
100
  }
101
  LOG(LL_INFO, ("WiFi: AP %s configured, IP 192.168.4.1", ssid));
102
  return true;
103
}
104

    
105
bool wifi_setup_sta(const char *ssid, const char *pass) {
106
  SlSecParams_t sp;
107
  LOG(LL_INFO, ("WiFi: connecting to %s", ssid));
108
  if (sl_WlanSetMode(ROLE_STA) != 0) return false;
109
  sl_Stop(0);
110
  sl_Start(NULL, NULL, NULL);
111
  sl_WlanDisconnect();
112
  sp.Key = (_i8 *) pass;
113
  sp.KeyLen = strlen(pass);
114
  sp.Type = sp.KeyLen ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN;
115
  if (sl_WlanConnect((const _i8 *) ssid, strlen(ssid), 0, &sp, 0) != 0) {
116
    return false;
117
  }
118
  return true;
119
}
120

    
121
/*
122
 * In SDK 1.2.0 TI decided to stop resetting NWP before sl_Start, which in
123
 * practice means that sl_start will hang on subsequent runs after the first.
124
 *
125
 * See this post for details and suggested solution:
126
 * https://e2e.ti.com/support/wireless_connectivity/simplelink_wifi_cc31xx_cc32xx/f/968/p/499123/1806610#1806610
127
 *
128
 * However, since they don't provide OS_debug variant of simplelink.a and
129
 * adding another project dependency will complicate our demo even more,
130
 * we just take the required bit of code.
131
 *
132
 * This is a copy-paste of NwpPowerOnPreamble from cc_pal.c.
133
 */
134
void stop_nwp(void) {
135
#define MAX_RETRY_COUNT 1000
136
  unsigned int sl_stop_ind, apps_int_sts_raw, nwp_lpds_wake_cfg;
137
  unsigned int retry_count;
138
  /* Perform the sl_stop equivalent to ensure network services
139
     are turned off if active */
140
  HWREG(0x400F70B8) = 1; /* APPs to NWP interrupt */
141
  UtilsDelay(800000 / 5);
142

    
143
  retry_count = 0;
144
  nwp_lpds_wake_cfg = HWREG(0x4402D404);
145
  sl_stop_ind = HWREG(0x4402E16C);
146

    
147
  if ((nwp_lpds_wake_cfg != 0x20) && /* Check for NWP POR condition */
148
      !(sl_stop_ind & 0x2))          /* Check if sl_stop was executed */
149
  {
150
    /* Loop until APPs->NWP interrupt is cleared or timeout */
151
    while (retry_count < MAX_RETRY_COUNT) {
152
      apps_int_sts_raw = HWREG(0x400F70C0);
153
      if (apps_int_sts_raw & 0x1) {
154
        UtilsDelay(800000 / 5);
155
        retry_count++;
156
      } else {
157
        break;
158
      }
159
    }
160
  }
161
  HWREG(0x400F70B0) = 1; /* Clear APPs to NWP interrupt */
162
  UtilsDelay(800000 / 5);
163

    
164
  /* Stop the networking services */
165
  NwpPowerOff();
166
}