Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Wed Oct 10 00:33:53 2018 +0000
Revision:
0:8fdf9a60065b
how to make mbed librry

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /*
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2018, ARM Limited, All Rights Reserved
kadonotakashi 0:8fdf9a60065b 3 * SPDX-License-Identifier: Apache-2.0
kadonotakashi 0:8fdf9a60065b 4 *
kadonotakashi 0:8fdf9a60065b 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kadonotakashi 0:8fdf9a60065b 6 * not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 7 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 8 *
kadonotakashi 0:8fdf9a60065b 9 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 10 *
kadonotakashi 0:8fdf9a60065b 11 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kadonotakashi 0:8fdf9a60065b 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 14 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 15 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 16 */
kadonotakashi 0:8fdf9a60065b 17
kadonotakashi 0:8fdf9a60065b 18 #include "mbed.h"
kadonotakashi 0:8fdf9a60065b 19 #include "UDPSocket.h"
kadonotakashi 0:8fdf9a60065b 20 #include "greentea-client/test_env.h"
kadonotakashi 0:8fdf9a60065b 21 #include "unity/unity.h"
kadonotakashi 0:8fdf9a60065b 22 #include "utest.h"
kadonotakashi 0:8fdf9a60065b 23 #include "udp_tests.h"
kadonotakashi 0:8fdf9a60065b 24
kadonotakashi 0:8fdf9a60065b 25 using namespace utest::v1;
kadonotakashi 0:8fdf9a60065b 26
kadonotakashi 0:8fdf9a60065b 27 namespace {
kadonotakashi 0:8fdf9a60065b 28 static const int SIGNAL_SIGIO = 0x1;
kadonotakashi 0:8fdf9a60065b 29 static const int SIGIO_TIMEOUT = 5000; //[ms]
kadonotakashi 0:8fdf9a60065b 30 static const int WAIT2RECV_TIMEOUT = 1000; //[ms]
kadonotakashi 0:8fdf9a60065b 31 static const int RETRIES = 2;
kadonotakashi 0:8fdf9a60065b 32
kadonotakashi 0:8fdf9a60065b 33 static const double EXPECTED_LOSS_RATIO = 0.0;
kadonotakashi 0:8fdf9a60065b 34 static const double TOLERATED_LOSS_RATIO = 0.3;
kadonotakashi 0:8fdf9a60065b 35
kadonotakashi 0:8fdf9a60065b 36 UDPSocket sock;
kadonotakashi 0:8fdf9a60065b 37 Semaphore tx_sem(0, 1);
kadonotakashi 0:8fdf9a60065b 38
kadonotakashi 0:8fdf9a60065b 39 static const int BUFF_SIZE = 1200;
kadonotakashi 0:8fdf9a60065b 40 char rx_buffer[BUFF_SIZE] = {0};
kadonotakashi 0:8fdf9a60065b 41 char tx_buffer[BUFF_SIZE] = {0};
kadonotakashi 0:8fdf9a60065b 42
kadonotakashi 0:8fdf9a60065b 43 static const int PKTS = 22;
kadonotakashi 0:8fdf9a60065b 44 static const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \
kadonotakashi 0:8fdf9a60065b 45 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, \
kadonotakashi 0:8fdf9a60065b 46 1100, 1200
kadonotakashi 0:8fdf9a60065b 47 };
kadonotakashi 0:8fdf9a60065b 48 }
kadonotakashi 0:8fdf9a60065b 49
kadonotakashi 0:8fdf9a60065b 50 static void _sigio_handler(osThreadId id)
kadonotakashi 0:8fdf9a60065b 51 {
kadonotakashi 0:8fdf9a60065b 52 osSignalSet(id, SIGNAL_SIGIO);
kadonotakashi 0:8fdf9a60065b 53 }
kadonotakashi 0:8fdf9a60065b 54
kadonotakashi 0:8fdf9a60065b 55 void UDPSOCKET_ECHOTEST()
kadonotakashi 0:8fdf9a60065b 56 {
kadonotakashi 0:8fdf9a60065b 57 SocketAddress udp_addr;
kadonotakashi 0:8fdf9a60065b 58 get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
kadonotakashi 0:8fdf9a60065b 59 udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 UDPSocket sock;
kadonotakashi 0:8fdf9a60065b 62 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
kadonotakashi 0:8fdf9a60065b 63
kadonotakashi 0:8fdf9a60065b 64 int recvd;
kadonotakashi 0:8fdf9a60065b 65 int sent;
kadonotakashi 0:8fdf9a60065b 66 int s_idx = 0;
kadonotakashi 0:8fdf9a60065b 67 int packets_sent = 0;
kadonotakashi 0:8fdf9a60065b 68 int packets_recv = 0;
kadonotakashi 0:8fdf9a60065b 69 for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; pkt_s = ++s_idx) {
kadonotakashi 0:8fdf9a60065b 70 pkt_s = pkt_sizes[s_idx];
kadonotakashi 0:8fdf9a60065b 71
kadonotakashi 0:8fdf9a60065b 72 fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
kadonotakashi 0:8fdf9a60065b 73
kadonotakashi 0:8fdf9a60065b 74 for (int retry_cnt = 0; retry_cnt <= 2; retry_cnt++) {
kadonotakashi 0:8fdf9a60065b 75 memset(rx_buffer, 0, BUFF_SIZE);
kadonotakashi 0:8fdf9a60065b 76 sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
kadonotakashi 0:8fdf9a60065b 77 if (sent > 0) {
kadonotakashi 0:8fdf9a60065b 78 packets_sent++;
kadonotakashi 0:8fdf9a60065b 79 }
kadonotakashi 0:8fdf9a60065b 80 if (sent != pkt_s) {
kadonotakashi 0:8fdf9a60065b 81 printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
kadonotakashi 0:8fdf9a60065b 82 continue;
kadonotakashi 0:8fdf9a60065b 83 }
kadonotakashi 0:8fdf9a60065b 84 recvd = sock.recvfrom(NULL, rx_buffer, pkt_s);
kadonotakashi 0:8fdf9a60065b 85 if (recvd == pkt_s) {
kadonotakashi 0:8fdf9a60065b 86 break;
kadonotakashi 0:8fdf9a60065b 87 }
kadonotakashi 0:8fdf9a60065b 88 }
kadonotakashi 0:8fdf9a60065b 89 if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
kadonotakashi 0:8fdf9a60065b 90 packets_recv++;
kadonotakashi 0:8fdf9a60065b 91 }
kadonotakashi 0:8fdf9a60065b 92 }
kadonotakashi 0:8fdf9a60065b 93 // Packet loss up to 30% tolerated
kadonotakashi 0:8fdf9a60065b 94 if (packets_sent > 0) {
kadonotakashi 0:8fdf9a60065b 95 double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
kadonotakashi 0:8fdf9a60065b 96 printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
kadonotakashi 0:8fdf9a60065b 97 TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
kadonotakashi 0:8fdf9a60065b 98 }
kadonotakashi 0:8fdf9a60065b 99 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
kadonotakashi 0:8fdf9a60065b 100 }
kadonotakashi 0:8fdf9a60065b 101
kadonotakashi 0:8fdf9a60065b 102 void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
kadonotakashi 0:8fdf9a60065b 103 {
kadonotakashi 0:8fdf9a60065b 104 int expt2recv = *(int *)receive_bytes;
kadonotakashi 0:8fdf9a60065b 105 int recvd;
kadonotakashi 0:8fdf9a60065b 106 for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
kadonotakashi 0:8fdf9a60065b 107 recvd = sock.recvfrom(NULL, rx_buffer, expt2recv);
kadonotakashi 0:8fdf9a60065b 108 if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
kadonotakashi 0:8fdf9a60065b 109 wait_ms(WAIT2RECV_TIMEOUT);
kadonotakashi 0:8fdf9a60065b 110 --retry_cnt;
kadonotakashi 0:8fdf9a60065b 111 continue;
kadonotakashi 0:8fdf9a60065b 112 } else if (recvd == expt2recv) {
kadonotakashi 0:8fdf9a60065b 113 break;
kadonotakashi 0:8fdf9a60065b 114 }
kadonotakashi 0:8fdf9a60065b 115 }
kadonotakashi 0:8fdf9a60065b 116
kadonotakashi 0:8fdf9a60065b 117 drop_bad_packets(sock, -1); // timeout equivalent to set_blocking(false)
kadonotakashi 0:8fdf9a60065b 118
kadonotakashi 0:8fdf9a60065b 119 tx_sem.release();
kadonotakashi 0:8fdf9a60065b 120 }
kadonotakashi 0:8fdf9a60065b 121
kadonotakashi 0:8fdf9a60065b 122 void UDPSOCKET_ECHOTEST_NONBLOCK()
kadonotakashi 0:8fdf9a60065b 123 {
kadonotakashi 0:8fdf9a60065b 124 SocketAddress udp_addr;
kadonotakashi 0:8fdf9a60065b 125 get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
kadonotakashi 0:8fdf9a60065b 126 udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
kadonotakashi 0:8fdf9a60065b 127
kadonotakashi 0:8fdf9a60065b 128 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
kadonotakashi 0:8fdf9a60065b 129 sock.set_blocking(false);
kadonotakashi 0:8fdf9a60065b 130 sock.sigio(callback(_sigio_handler, Thread::gettid()));
kadonotakashi 0:8fdf9a60065b 131
kadonotakashi 0:8fdf9a60065b 132 int sent;
kadonotakashi 0:8fdf9a60065b 133 int s_idx = 0;
kadonotakashi 0:8fdf9a60065b 134 int packets_sent = 0;
kadonotakashi 0:8fdf9a60065b 135 int packets_recv = 0;
kadonotakashi 0:8fdf9a60065b 136 Thread *thread;
kadonotakashi 0:8fdf9a60065b 137 unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
kadonotakashi 0:8fdf9a60065b 138 TEST_ASSERT_NOT_NULL(stack_mem);
kadonotakashi 0:8fdf9a60065b 139
kadonotakashi 0:8fdf9a60065b 140 for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
kadonotakashi 0:8fdf9a60065b 141 pkt_s = pkt_sizes[s_idx];
kadonotakashi 0:8fdf9a60065b 142
kadonotakashi 0:8fdf9a60065b 143 thread = new Thread(osPriorityNormal,
kadonotakashi 0:8fdf9a60065b 144 OS_STACK_SIZE,
kadonotakashi 0:8fdf9a60065b 145 stack_mem,
kadonotakashi 0:8fdf9a60065b 146 "receiver");
kadonotakashi 0:8fdf9a60065b 147 TEST_ASSERT_EQUAL(osOK, thread->start(callback(udpsocket_echotest_nonblock_receiver, &pkt_s)));
kadonotakashi 0:8fdf9a60065b 148
kadonotakashi 0:8fdf9a60065b 149 for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
kadonotakashi 0:8fdf9a60065b 150 fill_tx_buffer_ascii(tx_buffer, pkt_s);
kadonotakashi 0:8fdf9a60065b 151
kadonotakashi 0:8fdf9a60065b 152 sent = sock.sendto(udp_addr, tx_buffer, pkt_s);
kadonotakashi 0:8fdf9a60065b 153 if (sent > 0) {
kadonotakashi 0:8fdf9a60065b 154 packets_sent++;
kadonotakashi 0:8fdf9a60065b 155 }
kadonotakashi 0:8fdf9a60065b 156 if (sent == NSAPI_ERROR_WOULD_BLOCK) {
kadonotakashi 0:8fdf9a60065b 157 if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
kadonotakashi 0:8fdf9a60065b 158 continue;
kadonotakashi 0:8fdf9a60065b 159 }
kadonotakashi 0:8fdf9a60065b 160 --retry_cnt;
kadonotakashi 0:8fdf9a60065b 161 } else if (sent != pkt_s) {
kadonotakashi 0:8fdf9a60065b 162 printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
kadonotakashi 0:8fdf9a60065b 163 continue;
kadonotakashi 0:8fdf9a60065b 164 }
kadonotakashi 0:8fdf9a60065b 165 if (tx_sem.wait(WAIT2RECV_TIMEOUT * 2) == 0) { // RX might wait up to WAIT2RECV_TIMEOUT before recvfrom
kadonotakashi 0:8fdf9a60065b 166 continue;
kadonotakashi 0:8fdf9a60065b 167 }
kadonotakashi 0:8fdf9a60065b 168 break;
kadonotakashi 0:8fdf9a60065b 169 }
kadonotakashi 0:8fdf9a60065b 170 thread->join();
kadonotakashi 0:8fdf9a60065b 171 delete thread;
kadonotakashi 0:8fdf9a60065b 172 if (memcmp(tx_buffer, rx_buffer, pkt_s) == 0) {
kadonotakashi 0:8fdf9a60065b 173 packets_recv++;
kadonotakashi 0:8fdf9a60065b 174 }
kadonotakashi 0:8fdf9a60065b 175 }
kadonotakashi 0:8fdf9a60065b 176 free(stack_mem);
kadonotakashi 0:8fdf9a60065b 177 // Packet loss up to 30% tolerated
kadonotakashi 0:8fdf9a60065b 178 if (packets_sent > 0) {
kadonotakashi 0:8fdf9a60065b 179 double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
kadonotakashi 0:8fdf9a60065b 180 printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
kadonotakashi 0:8fdf9a60065b 181 TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
kadonotakashi 0:8fdf9a60065b 182 }
kadonotakashi 0:8fdf9a60065b 183 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
kadonotakashi 0:8fdf9a60065b 184 }