blob: 57f346855ff0e452b01d5a4581f2655dcd8463e5 [file] [log] [blame]
Ryan Harkined043b72019-11-05 18:14:04 +00001#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-only
3# Copyright (C) 2019 Linaro Ltd.
4
5# shellcheck disable=SC1091
6. ../../lib/sh-test-lib
7OUTPUT="$(pwd)/output"
8RESULT_FILE="${OUTPUT}/result.txt"
9
10usage() {
11 echo "\
12 Usage: $0
13 [-t </dev/ttyX>] [-r </dev/ttyX]
14
15 <t>:
16 The transmit UART used for the loopback tests
17 <r>:
18 The receive UART used for the loopback tests
19
20 This test will perform loopback transmission between two UARTs,
21 using various baud rates, parity and stop bit settings.
22 "
23}
24
25
26while getopts "t:r:h" opts; do
27 case "$opts" in
28 t) TXUART="${OPTARG}" ;;
29 r) RXUART="${OPTARG}" ;;
30 h|*) usage ; exit 1 ;;
31 esac
32done
33
34param_err=0
35if [[ -z "${TXUART}" ]]; then
36 echo "ERROR: you must use option -t to specify TXUART"
37 param_err=1
38fi
39
40if [[ -z "${RXUART}" ]]; then
41 echo "ERROR: you must use option -r to specify RXUART"
42 param_err=1
43fi
44
45if [[ ! param_err -eq 0 ]]; then
46 usage
47 exit 1
48fi
49
50char_device_exists () {
51 local device=$1
52
53 if [[ -c ${device} ]]; then
54 echo true
55 else
56 echo false
57 fi
58}
59
60wait_for_device () {
61 local device=$1
62 local retries=20
63
64 if [[ -z ${device} ]]; then
65 echo "You must specifiy a valid device"
66 exit 1
67 fi
68
69 echo -n "Waiting for ${device}: "
70 for ((i=0;i<retries;++i)); do
71 local exists
72 exists=$(char_device_exists "${device}")
73 if [[ "${exists}" == "true" ]]; then
74 echo "done"
75 sleep 0.5 # allow the device some time to settle after being plugged in
76 return
77 fi
78 echo -n "."
79 sleep 0.5
80 done
81 echo "failed"
82 echo "uart-loopback fail" >> "${RESULT_FILE}"
83 exit 1
84}
85
86test_one () {
87 LENGTH=$1
88 detect_abi
89 # shellcheck disable=SC2154
90 case "$abi" in
91 armeabi|arm64|x86_64) ;;
92 *) warn_msg "Unsupported architecture"; exit 1 ;;
93 esac
94
95 DIR="$( dirname "$0" )"
96 if ! "${DIR}"/uart-loopback."${abi}" -o "${TXUART}" -i "${RXUART}" -s "${LENGTH}" -r
97 then
98 ERRORS=$((ERRORS+1));
99 fi
100}
101
102# Test transfers of lengths that typically throw problems
103test_one_cfg () {
104 local settings="$*"
105 local errors="${ERRORS}"
106
107 # shellcheck disable=SC2086
108 stty -F "${TXUART}" ${settings}
109 # shellcheck disable=SC2086
110 stty -F "${RXUART}" ${settings}
111
112 for length in $(seq 1 33); do
113 test_one "${length}"
114 done
115
116 test_one 4095
117 test_one 4096
118 test_one 4097
119
120 if [ "${errors}" = "${ERRORS}" ]; then
121 echo " pass"
122 else
123 echo " fail"
124 fi
125}
126
127uart-loopback () {
128 # Note that we specify the _changes_ to the tty settings, so don't comment one out!
129 baudrates=(9600 38400 115200 230400)
130 for baud in "${baudrates[@]}" ;
131 do
132 echo -n "${baud}:8n1:raw"
133 test_one_cfg "${baud} -parenb -cstopb -crtscts cs8 -ignbrk -brkint -parmrk -istrip -inlcr -igncr -icrnl -ixon -opost -echo -echonl -icanon -isig -iexten"
134
135 echo -n "${baud}:8o1"
136 test_one_cfg "parenb parodd"
137
138 echo -n "${baud}:8e1"
139 test_one_cfg "-parodd"
140
141 echo -n "${baud}:8n2"
142 test_one_cfg "-parenb cstopb"
143
144 echo -n "${baud}:8n1:CTS/RTS"
145 test_one_cfg "-cstopb crtscts"
146
147 # This is the same as the first test, putting the UART back into 115200
148 echo -n "${baud}:8n1:raw"
149 test_one_cfg "-crtscts"
150 done
151
152 echo
153 echo "Tests complete with ${ERRORS} Errors."
154}
155
156loopback_test () {
157 local logfile
158 logfile=$(mktemp "/tmp/uart-loopback.log.XXXXXXXXXXXX")
159
160 echo "Testing data transfer from ${TXUART} to ${RXUART}" | tee "${logfile}"
161 uart-loopback "${TXUART}" "${RXUART}" | tee -a "${logfile}"
162 sed -i 's/\.//g' "${logfile}"
163 grep -e "pass" -e "fail" "${logfile}" >> "${RESULT_FILE}"
164}
165
166create_out_dir "${OUTPUT}"
167wait_for_device "${TXUART}"
168wait_for_device "${RXUART}"
169loopback_test