|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "drivers/RawSerial.h" |
| 17 | + |
| 18 | +using namespace mbed; |
| 19 | + |
| 20 | +/* UART TEST FOR GREEN TEA |
| 21 | + * |
| 22 | + * Greentea-client uses RawSerial class to handle UART communication. |
| 23 | + * RawSerial class must provide valid implementation of the following |
| 24 | + * methods (used by Greentea-client): |
| 25 | + * - int RawSerial::getc() |
| 26 | + * - int RawSerial::putc(int c). |
| 27 | + * |
| 28 | + * Greentea-client uses USB serial port for communication with the |
| 29 | + * following transmission parameters: |
| 30 | + * - 8 data bits, |
| 31 | + * - 1 stop bit, |
| 32 | + * - none parity bit, |
| 33 | + * - configurable baudrate. |
| 34 | + * |
| 35 | + * Greentea-client operates in pooling mode only. |
| 36 | + * Flow control is disabled. |
| 37 | + * |
| 38 | + * UART buffer size is limited to 120 bytes. |
| 39 | + * |
| 40 | + * TEST SCENARIO |
| 41 | + * This part of the test is executed on mbed board. |
| 42 | + * |
| 43 | + * After startup it will try to communicate via UART with host part of the test. |
| 44 | + * This part sends 4 messages with different length to the host and waits |
| 45 | + * for the response (host should respond with the same message). |
| 46 | + * Last message "PASSED" is send to inform that all responses from host were |
| 47 | + * successfully received. |
| 48 | + * |
| 49 | + * To perform this test with serial port baud rate different than default (9600), |
| 50 | + * call host test with appropriate --baudrate parameter. |
| 51 | + * |
| 52 | + */ |
| 53 | + |
| 54 | +/* Default baud rate if undefined. */ |
| 55 | +#ifndef BAUD_RATE |
| 56 | +#define BAUD_RATE 9600 |
| 57 | +#endif |
| 58 | + |
| 59 | +#define NUMBER_OF_TEST_STRINGS 5 |
| 60 | +#define NUMBER_OF_TEST_RETRIES 10 |
| 61 | + |
| 62 | +typedef enum |
| 63 | +{ |
| 64 | + PASSED, FAILED |
| 65 | +} test_status; |
| 66 | + |
| 67 | +/* Array of string to be transmitted. */ |
| 68 | +const char *test_strings[NUMBER_OF_TEST_STRINGS] = |
| 69 | + { "x", |
| 70 | + "mbed", "Green Tea UART bare metal test.", |
| 71 | + "Long Message-Long Message-Long Message-Long Message-Long Message-Long Message-Long Message-Long Message-Long Message--.", |
| 72 | + "PASSED" }; |
| 73 | + |
| 74 | +/* Function writes provided string to the serial port. */ |
| 75 | +void write_line(RawSerial &serial, const char *str) |
| 76 | +{ |
| 77 | + while (true) { |
| 78 | + serial.putc(*str); |
| 79 | + if (*str == '\0') { |
| 80 | + break; |
| 81 | + } |
| 82 | + |
| 83 | + str++; |
| 84 | + }; |
| 85 | +} |
| 86 | + |
| 87 | +/* Function reads provided string from the serial port. */ |
| 88 | +int read_line(RawSerial &serial, char *buffer) |
| 89 | +{ |
| 90 | + int count = 0; |
| 91 | + while (true) { |
| 92 | + *buffer = (char) serial.getc(); |
| 93 | + count++; |
| 94 | + if (*buffer == '\0') { |
| 95 | + return count; |
| 96 | + } |
| 97 | + |
| 98 | + buffer += 1; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/* Function compares provided strings and returns 0 if are equal. */ |
| 103 | +int strCmp(const char string1[], char string2[]) |
| 104 | +{ |
| 105 | + for (int i = 0;; i++) { |
| 106 | + if (string1[i] != string2[i]) { |
| 107 | + return string1[i] < string2[i] ? -1 : 1; |
| 108 | + } |
| 109 | + |
| 110 | + if (string1[i] == '\0') { |
| 111 | + return 0; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/* Function writes provided message to the serial port and |
| 117 | + * waits for the response from the Host. Host should |
| 118 | + * respond with the same message. |
| 119 | + * |
| 120 | + * If valid response has been received, then function returns |
| 121 | + * PASSED status, otherwise transmission is repeated. |
| 122 | + * |
| 123 | + * If max number of transmission attempts is reached, then |
| 124 | + * function returns FAILED status. |
| 125 | + * |
| 126 | + * */ |
| 127 | +test_status test_uart_communication(RawSerial &serial, const char * msg) |
| 128 | +{ |
| 129 | + char buffer[120]; |
| 130 | + |
| 131 | + for (int i = 0; i < NUMBER_OF_TEST_RETRIES; i++) { |
| 132 | + write_line(serial, msg); |
| 133 | + read_line(serial, buffer); |
| 134 | + |
| 135 | + if (strCmp(msg, buffer) == 0) { |
| 136 | + return PASSED; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return FAILED; |
| 141 | +} |
| 142 | + |
| 143 | +int main() |
| 144 | +{ |
| 145 | + RawSerial serial(USBTX, USBRX, BAUD_RATE); |
| 146 | + |
| 147 | + /* Start the test. */ |
| 148 | + for (unsigned int i = 0; i < (sizeof(test_strings) / sizeof(test_strings[0])); i++) { |
| 149 | + if (test_uart_communication(serial, test_strings[i]) == FAILED) { |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + while (true); |
| 155 | +} |
| 156 | + |
0 commit comments