1 | #include "asf.h"
|
2 | #include "main.h"
|
3 | #include "driver/include/m2m_wifi.h"
|
4 | #include "driver/source/nmasic.h"
|
5 |
|
6 | #define STRING_EOL "\r\n"
|
7 | #define STRING_HEADER "-- WINC1500 AP mode example --"STRING_EOL \
|
8 | "-- "BOARD_NAME " --"STRING_EOL \
|
9 | "-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
|
10 |
|
11 |
|
12 | struct i2c_master_module i2c_master_instance;
|
13 |
|
14 | /** UART module for debug. */
|
15 | static struct usart_module cdc_uart_module;
|
16 |
|
17 |
|
18 | /**
|
19 | * \brief Configure UART console.
|
20 | */
|
21 | static void configure_console(void)
|
22 | {
|
23 | struct usart_config usart_conf;
|
24 |
|
25 | usart_get_config_defaults(&usart_conf);
|
26 | usart_conf.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
|
27 | usart_conf.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
|
28 | usart_conf.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
|
29 | usart_conf.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
|
30 | usart_conf.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
|
31 | usart_conf.baudrate = 115200;
|
32 |
|
33 | stdio_serial_init(&cdc_uart_module, EDBG_CDC_MODULE, &usart_conf);
|
34 | usart_enable(&cdc_uart_module);
|
35 | }
|
36 |
|
37 | /**
|
38 | * \brief Callback to get the Wi-Fi status update.
|
39 | *
|
40 | * \param[in] u8MsgType type of Wi-Fi notification. Possible types are:
|
41 | * - [M2M_WIFI_RESP_CON_STATE_CHANGED](@ref M2M_WIFI_RESP_CON_STATE_CHANGED)
|
42 | * - [M2M_WIFI_REQ_DHCP_CONF](@ref M2M_WIFI_REQ_DHCP_CONF)
|
43 | * \param[in] pvMsg A pointer to a buffer containing the notification parameters
|
44 | * (if any). It should be casted to the correct data type corresponding to the
|
45 | * notification type.
|
46 | */
|
47 | static void wifi_cb(uint8_t u8MsgType, void *pvMsg)
|
48 | {
|
49 | switch (u8MsgType) {
|
50 | case M2M_WIFI_RESP_CON_STATE_CHANGED:
|
51 | {
|
52 | tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg;
|
53 | if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) {
|
54 | } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) {
|
55 | printf("Station disconnected\r\n");
|
56 | }
|
57 |
|
58 | break;
|
59 | }
|
60 |
|
61 | case M2M_WIFI_REQ_DHCP_CONF:
|
62 | {
|
63 | uint8_t *pu8IPAddress = (uint8_t *)pvMsg;
|
64 | printf("Station connected\r\n");
|
65 | printf("Station IP is %u.%u.%u.%u\r\n",
|
66 | pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]);
|
67 | break;
|
68 | }
|
69 |
|
70 | default:
|
71 | {
|
72 | break;
|
73 | }
|
74 | }
|
75 | }
|
76 |
|
77 | /**
|
78 | * \brief Main application function.
|
79 | */
|
80 | int main(void)
|
81 | {
|
82 | tstrWifiInitParam param;
|
83 | tstrM2MAPConfig strM2MAPConfig;
|
84 | int8_t ret;
|
85 |
|
86 | /* Initialize the board. */
|
87 | system_init();
|
88 |
|
89 | /* Initialize the UART console. */
|
90 | configure_console();
|
91 | printf(STRING_HEADER);
|
92 |
|
93 | /* Initialize the BSP. */
|
94 | nm_bsp_init();
|
95 |
|
96 | /* Initialize Wi-Fi parameters structure. */
|
97 | memset((uint8_t *)¶m, 0, sizeof(tstrWifiInitParam));
|
98 |
|
99 | /* Initialize Wi-Fi driver with data and status callbacks. */
|
100 | param.pfAppWifiCb = wifi_cb;
|
101 | ret = m2m_wifi_init(¶m);
|
102 | if (M2M_SUCCESS != ret) {
|
103 | printf("main: m2m_wifi_init call error!(%d)\r\n", ret);
|
104 | while (1) {
|
105 | }
|
106 | }
|
107 |
|
108 | /* Initialize AP mode parameters structure with SSID, channel and OPEN security type. */
|
109 | memset(&strM2MAPConfig, 0x00, sizeof(tstrM2MAPConfig));
|
110 | strcpy((char *)&strM2MAPConfig.au8SSID, MAIN_WLAN_SSID);
|
111 | strM2MAPConfig.u8ListenChannel = MAIN_WLAN_CHANNEL;
|
112 | strM2MAPConfig.u8SecType = MAIN_WLAN_AUTH;
|
113 |
|
114 | strM2MAPConfig.au8DHCPServerIP[0] = 192;
|
115 | strM2MAPConfig.au8DHCPServerIP[1] = 168;
|
116 | strM2MAPConfig.au8DHCPServerIP[2] = 1;
|
117 | strM2MAPConfig.au8DHCPServerIP[3] = 1;
|
118 |
|
119 | #if USE_WEP
|
120 | strcpy((char *)&strM2MAPConfig.au8WepKey, MAIN_WLAN_WEP_KEY);
|
121 | strM2MAPConfig.u8KeySz = strlen(MAIN_WLAN_WEP_KEY);
|
122 | strM2MAPConfig.u8KeyIndx = MAIN_WLAN_WEP_KEY_INDEX;
|
123 | #endif
|
124 |
|
125 | /* Bring up AP mode with parameters structure. */
|
126 | ret = m2m_wifi_enable_ap(&strM2MAPConfig);
|
127 | if (M2M_SUCCESS != ret) {
|
128 | printf("main: m2m_wifi_enable_ap call error!\r\n");
|
129 | while (1) {
|
130 | }
|
131 | }
|
132 |
|
133 | printf("AP mode started. You can connect to %s.\r\n", (char *)MAIN_WLAN_SSID);
|
134 |
|
135 | while (1) {
|
136 | /* Handle pending events from network controller. */
|
137 | while (m2m_wifi_handle_events(NULL) != M2M_SUCCESS) {
|
138 | }
|
139 | bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
|
140 | port_pin_set_output_level(PIN_PA23, pin_state);
|
141 |
|
142 |
|
143 | wake_mpu9250();
|
144 | }
|
145 |
|
146 | return 0;
|
147 | }
|
148 |
|
149 | void wake_mpu9250(void){
|
150 |
|
151 | #define ADDRESS 0x68 // I2C address of the sensor
|
152 |
|
153 | /* Initialize config structure and software module. */
|
154 | struct i2c_master_config config_i2c_master;
|
155 | i2c_master_get_config_defaults(&config_i2c_master);
|
156 | /* Change buffer timeout to something longer. */
|
157 | config_i2c_master.buffer_timeout = 10000;
|
158 |
|
159 | /* Initialize and enable device with config. */
|
160 | i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master);
|
161 | i2c_master_enable(&i2c_master_instance);
|
162 |
|
163 | uint8_t maxRetries = 64;
|
164 |
|
165 | enum status_code powerOnStatus;
|
166 | // POWER ON THE SENSOR!
|
167 | {
|
168 | uint8_t retryCount = 0;
|
169 |
|
170 | static uint8_t buffer[2] = { 0x6b, 0x0 }; // power on packet
|
171 | struct i2c_master_packet packet = {
|
172 | .address = ADDRESS,
|
173 | .data = buffer,
|
174 | .data_length = 2,
|
175 | };
|
176 |
|
177 | while ((powerOnStatus = i2c_master_write_packet_wait(&i2c_master_instance, &packet)) != STATUS_OK)
|
178 | {
|
179 |
|
180 | if (++retryCount >= maxRetries) break;
|
181 | }
|
182 | }
|
183 |
|
184 | uint8_t whoAmI;
|
185 | {
|
186 | uint8_t retryCount = 0;
|
187 |
|
188 | static uint8_t buffer[1] = { 0x75 }; // who am I
|
189 | struct i2c_master_packet packet = {
|
190 | .address = ADDRESS,
|
191 | .data = buffer,
|
192 | .data_length = 1,
|
193 | };
|
194 |
|
195 | // here use the no_stop variant
|
196 | while (i2c_master_write_packet_wait_no_stop(&i2c_master_instance, &packet) != STATUS_OK)
|
197 | {
|
198 | if (++retryCount >= maxRetries) break;
|
199 | }
|
200 |
|
201 | // and here it sends stop automatically
|
202 | UNUSED(i2c_master_read_packet_wait(&i2c_master_instance, &packet));
|
203 | whoAmI = buffer[0];
|
204 |
|
205 | printf("BUFFER: %s\r\n", whoAmI);
|
206 | }
|
207 |
|
208 | }
|
209 | // check that whoAmI == 0x68
|