5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-onsemi 0:098463de4c5d 1 /* mbed Microcontroller Library
group-onsemi 0:098463de4c5d 2 * Copyright (c) 2016 ARM Limited
group-onsemi 0:098463de4c5d 3 *
group-onsemi 0:098463de4c5d 4 * Licensed under the Apache License, Version 2.0 (the "License");
group-onsemi 0:098463de4c5d 5 * you may not use this file except in compliance with the License.
group-onsemi 0:098463de4c5d 6 * You may obtain a copy of the License at
group-onsemi 0:098463de4c5d 7 *
group-onsemi 0:098463de4c5d 8 * http://www.apache.org/licenses/LICENSE-2.0
group-onsemi 0:098463de4c5d 9 *
group-onsemi 0:098463de4c5d 10 * Unless required by applicable law or agreed to in writing, software
group-onsemi 0:098463de4c5d 11 * distributed under the License is distributed on an "AS IS" BASIS,
group-onsemi 0:098463de4c5d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-onsemi 0:098463de4c5d 13 * See the License for the specific language governing permissions and
group-onsemi 0:098463de4c5d 14 * limitations under the License.
group-onsemi 0:098463de4c5d 15 */
group-onsemi 0:098463de4c5d 16
group-onsemi 0:098463de4c5d 17 #include "utest/utest.h"
group-onsemi 0:098463de4c5d 18 #include "unity/unity.h"
group-onsemi 0:098463de4c5d 19 #include "greentea-client/test_env.h"
group-onsemi 0:098463de4c5d 20
group-onsemi 0:098463de4c5d 21 #include "mbed.h"
group-onsemi 0:098463de4c5d 22
group-onsemi 0:098463de4c5d 23 #if TARGET_UBLOX_EVK_ODIN_W2
group-onsemi 0:098463de4c5d 24 #include "OdinWiFiInterface.h"
group-onsemi 0:098463de4c5d 25 #else
group-onsemi 0:098463de4c5d 26 #error [NOT_SUPPORTED] Only built in WiFi modules are supported at this time.
group-onsemi 0:098463de4c5d 27 #endif
group-onsemi 0:098463de4c5d 28
group-onsemi 0:098463de4c5d 29 using namespace utest::v1;
group-onsemi 0:098463de4c5d 30
group-onsemi 0:098463de4c5d 31 /**
group-onsemi 0:098463de4c5d 32 * WiFi tests require following macros to be defined:
group-onsemi 0:098463de4c5d 33 * - MBED_CONF_APP_WIFI_SSID - SSID of a network the test will try connecting to
group-onsemi 0:098463de4c5d 34 * - MBED_CONF_APP_WIFI_PASSWORD - Passphrase that will be used to connecting to the network
group-onsemi 0:098463de4c5d 35 * - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3"
group-onsemi 0:098463de4c5d 36 */
group-onsemi 0:098463de4c5d 37 #if !defined(MBED_CONF_APP_WIFI_SSID) || !defined(MBED_CONF_APP_WIFI_PASSWORD) || !defined(MBED_CONF_APP_WIFI_NETWORKS)
group-onsemi 0:098463de4c5d 38 #error [NOT_SUPPORTED] MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD and MBED_CONF_APP_WIFI_NETWORKS have to be defined for this test.
group-onsemi 0:098463de4c5d 39 #endif
group-onsemi 0:098463de4c5d 40
group-onsemi 0:098463de4c5d 41 const char *networks[] = {MBED_CONF_APP_WIFI_NETWORKS, NULL};
group-onsemi 0:098463de4c5d 42
group-onsemi 0:098463de4c5d 43 WiFiInterface *wifi;
group-onsemi 0:098463de4c5d 44
group-onsemi 0:098463de4c5d 45 /* In normal circumstances the WiFi object could be global, but the delay introduced by WiFi initialization is an issue
group-onsemi 0:098463de4c5d 46 for the tests. It causes Greentea to timeout on syncing with the board. To solve it we defer the actual object
group-onsemi 0:098463de4c5d 47 creation till we actually need it.
group-onsemi 0:098463de4c5d 48 */
group-onsemi 0:098463de4c5d 49 WiFiInterface *get_wifi()
group-onsemi 0:098463de4c5d 50 {
group-onsemi 0:098463de4c5d 51 if (wifi == NULL) {
group-onsemi 0:098463de4c5d 52 /* We don't really care about freeing this, as its lifetime is through the full test suit run. */
group-onsemi 0:098463de4c5d 53 #if TARGET_UBLOX_EVK_ODIN_W2
group-onsemi 0:098463de4c5d 54 wifi = new OdinWiFiInterface;
group-onsemi 0:098463de4c5d 55 #endif
group-onsemi 0:098463de4c5d 56 }
group-onsemi 0:098463de4c5d 57
group-onsemi 0:098463de4c5d 58 return wifi;
group-onsemi 0:098463de4c5d 59 }
group-onsemi 0:098463de4c5d 60
group-onsemi 0:098463de4c5d 61 void check_wifi(const char *ssid, bool *net_stat)
group-onsemi 0:098463de4c5d 62 {
group-onsemi 0:098463de4c5d 63 int i = 0;
group-onsemi 0:098463de4c5d 64 while(networks[i]) {
group-onsemi 0:098463de4c5d 65 if (strcmp(networks[i], ssid) == 0) {
group-onsemi 0:098463de4c5d 66 net_stat[i] = true;
group-onsemi 0:098463de4c5d 67 break;
group-onsemi 0:098463de4c5d 68 }
group-onsemi 0:098463de4c5d 69 i++;
group-onsemi 0:098463de4c5d 70 }
group-onsemi 0:098463de4c5d 71 }
group-onsemi 0:098463de4c5d 72
group-onsemi 0:098463de4c5d 73 void wifi_scan()
group-onsemi 0:098463de4c5d 74 {
group-onsemi 0:098463de4c5d 75 int count;
group-onsemi 0:098463de4c5d 76 WiFiAccessPoint *aps;
group-onsemi 0:098463de4c5d 77 const int net_len = sizeof(networks)/sizeof(networks[0]);
group-onsemi 0:098463de4c5d 78 bool net_stat[net_len - 1];
group-onsemi 0:098463de4c5d 79
group-onsemi 0:098463de4c5d 80 memset(net_stat, 0, sizeof(net_stat));
group-onsemi 0:098463de4c5d 81
group-onsemi 0:098463de4c5d 82 count = get_wifi()->scan(NULL, 0);
group-onsemi 0:098463de4c5d 83 TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
group-onsemi 0:098463de4c5d 84 TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
group-onsemi 0:098463de4c5d 85
group-onsemi 0:098463de4c5d 86 aps = new WiFiAccessPoint[count];
group-onsemi 0:098463de4c5d 87 count = get_wifi()->scan(aps, count);
group-onsemi 0:098463de4c5d 88 for(int i = 0; i < count; i++) {
group-onsemi 0:098463de4c5d 89 check_wifi(aps[i].get_ssid(), net_stat);
group-onsemi 0:098463de4c5d 90 }
group-onsemi 0:098463de4c5d 91
group-onsemi 0:098463de4c5d 92 delete[] aps;
group-onsemi 0:098463de4c5d 93
group-onsemi 0:098463de4c5d 94 for (unsigned i = 0; i < sizeof(net_stat); i++) {
group-onsemi 0:098463de4c5d 95 TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
group-onsemi 0:098463de4c5d 96 }
group-onsemi 0:098463de4c5d 97 }
group-onsemi 0:098463de4c5d 98
group-onsemi 0:098463de4c5d 99 void wifi_connect()
group-onsemi 0:098463de4c5d 100 {
group-onsemi 0:098463de4c5d 101 int ret;
group-onsemi 0:098463de4c5d 102
group-onsemi 0:098463de4c5d 103 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-onsemi 0:098463de4c5d 104 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
group-onsemi 0:098463de4c5d 105
group-onsemi 0:098463de4c5d 106 ret = get_wifi()->disconnect();
group-onsemi 0:098463de4c5d 107 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
group-onsemi 0:098463de4c5d 108 }
group-onsemi 0:098463de4c5d 109
group-onsemi 0:098463de4c5d 110 void wifi_connect_scan()
group-onsemi 0:098463de4c5d 111 {
group-onsemi 0:098463de4c5d 112 int ret;
group-onsemi 0:098463de4c5d 113 int count;
group-onsemi 0:098463de4c5d 114 WiFiAccessPoint *aps;
group-onsemi 0:098463de4c5d 115 const int net_len = sizeof(networks)/sizeof(networks[0]);
group-onsemi 0:098463de4c5d 116 bool net_stat[net_len - 1];
group-onsemi 0:098463de4c5d 117
group-onsemi 0:098463de4c5d 118 memset(net_stat, 0, sizeof(net_stat));
group-onsemi 0:098463de4c5d 119
group-onsemi 0:098463de4c5d 120 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-onsemi 0:098463de4c5d 121 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
group-onsemi 0:098463de4c5d 122
group-onsemi 0:098463de4c5d 123 count = get_wifi()->scan(NULL, 0);
group-onsemi 0:098463de4c5d 124 TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
group-onsemi 0:098463de4c5d 125 TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
group-onsemi 0:098463de4c5d 126
group-onsemi 0:098463de4c5d 127 aps = new WiFiAccessPoint[count];
group-onsemi 0:098463de4c5d 128 count = get_wifi()->scan(aps, count);
group-onsemi 0:098463de4c5d 129 for(int i = 0; i < count; i++) {
group-onsemi 0:098463de4c5d 130 check_wifi(aps[i].get_ssid(), net_stat);
group-onsemi 0:098463de4c5d 131 }
group-onsemi 0:098463de4c5d 132
group-onsemi 0:098463de4c5d 133 delete[] aps;
group-onsemi 0:098463de4c5d 134
group-onsemi 0:098463de4c5d 135 ret = get_wifi()->disconnect();
group-onsemi 0:098463de4c5d 136 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
group-onsemi 0:098463de4c5d 137
group-onsemi 0:098463de4c5d 138 for (unsigned i = 0; i < sizeof(net_stat); i++) {
group-onsemi 0:098463de4c5d 139 TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
group-onsemi 0:098463de4c5d 140 }
group-onsemi 0:098463de4c5d 141 }
group-onsemi 0:098463de4c5d 142
group-onsemi 0:098463de4c5d 143 void wifi_http()
group-onsemi 0:098463de4c5d 144 {
group-onsemi 0:098463de4c5d 145 TCPSocket socket;
group-onsemi 0:098463de4c5d 146 int ret;
group-onsemi 0:098463de4c5d 147
group-onsemi 0:098463de4c5d 148 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
group-onsemi 0:098463de4c5d 149 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
group-onsemi 0:098463de4c5d 150
group-onsemi 0:098463de4c5d 151 // Open a socket on the network interface, and create a TCP connection to www.arm.com
group-onsemi 0:098463de4c5d 152 ret = socket.open(get_wifi());
group-onsemi 0:098463de4c5d 153 TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed");
group-onsemi 0:098463de4c5d 154 ret = socket.connect("www.arm.com", 80);
group-onsemi 0:098463de4c5d 155 TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed");
group-onsemi 0:098463de4c5d 156
group-onsemi 0:098463de4c5d 157 // Send a simple http request
group-onsemi 0:098463de4c5d 158 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
group-onsemi 0:098463de4c5d 159 int scount = socket.send(sbuffer, sizeof sbuffer);
group-onsemi 0:098463de4c5d 160 TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed");
group-onsemi 0:098463de4c5d 161
group-onsemi 0:098463de4c5d 162 // Recieve a simple http response and check if it's not empty
group-onsemi 0:098463de4c5d 163 char rbuffer[64];
group-onsemi 0:098463de4c5d 164 int rcount = socket.recv(rbuffer, sizeof rbuffer);
group-onsemi 0:098463de4c5d 165 TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error");
group-onsemi 0:098463de4c5d 166 TEST_ASSERT_MESSAGE(rcount > 0, "No data received");
group-onsemi 0:098463de4c5d 167
group-onsemi 0:098463de4c5d 168 ret = socket.close();
group-onsemi 0:098463de4c5d 169 TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed");
group-onsemi 0:098463de4c5d 170
group-onsemi 0:098463de4c5d 171 ret = get_wifi()->disconnect();
group-onsemi 0:098463de4c5d 172 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
group-onsemi 0:098463de4c5d 173 }
group-onsemi 0:098463de4c5d 174
group-onsemi 0:098463de4c5d 175 status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
group-onsemi 0:098463de4c5d 176 greentea_case_failure_abort_handler(source, reason);
group-onsemi 0:098463de4c5d 177 return STATUS_CONTINUE;
group-onsemi 0:098463de4c5d 178 }
group-onsemi 0:098463de4c5d 179
group-onsemi 0:098463de4c5d 180 Case cases[] = {
group-onsemi 0:098463de4c5d 181 Case("Scan test", wifi_scan, greentea_failure_handler),
group-onsemi 0:098463de4c5d 182 Case("Connect test", wifi_connect, greentea_failure_handler),
group-onsemi 0:098463de4c5d 183 Case("Scan while connected test", wifi_connect_scan, greentea_failure_handler),
group-onsemi 0:098463de4c5d 184 Case("HTTP test", wifi_http, greentea_failure_handler),
group-onsemi 0:098463de4c5d 185 };
group-onsemi 0:098463de4c5d 186
group-onsemi 0:098463de4c5d 187 status_t greentea_test_setup(const size_t number_of_cases) {
group-onsemi 0:098463de4c5d 188 GREENTEA_SETUP(90, "default_auto");
group-onsemi 0:098463de4c5d 189 return greentea_test_setup_handler(number_of_cases);
group-onsemi 0:098463de4c5d 190 }
group-onsemi 0:098463de4c5d 191
group-onsemi 0:098463de4c5d 192
group-onsemi 0:098463de4c5d 193 int main() {
group-onsemi 0:098463de4c5d 194 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
group-onsemi 0:098463de4c5d 195 Harness::run(specification);
group-onsemi 0:098463de4c5d 196 }