Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
serial_asynch.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include <stdio.h> 00017 #include "TestHarness.h" 00018 #include "mbed.h" 00019 00020 /* Serial asynch cross */ 00021 00022 #if !DEVICE_SERIAL || !DEVICE_SERIAL_ASYNCH 00023 #error serial_asynch requires asynch Serial 00024 #endif 00025 00026 // Device config 00027 #if defined(TARGET_K64F) 00028 #define TEST_SERIAL_ONE_TX_PIN PTC17 // uart3 00029 #define TEST_SERIAL_TWO_RX_PIN PTD2 // uart2 00030 00031 #elif defined(TARGET_K66F) 00032 #define TEST_SERIAL_ONE_TX_PIN PTD3 // uart2 00033 #define TEST_SERIAL_TWO_RX_PIN PTC16 // uart3 00034 00035 #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32GG_STK3700) || defined(TARGET_EFM32WG_STK3800) 00036 #define TEST_SERIAL_ONE_TX_PIN PD0 // usart1 00037 #define TEST_SERIAL_TWO_RX_PIN PC3 // usart2 00038 00039 #elif defined(TARGET_EFM32ZG_STK3200) 00040 #error "Target not supported (only 2 serial ports available, need 3)" 00041 00042 #elif defined(TARGET_EFM32HG_STK3400) 00043 #define TEST_SERIAL_ONE_TX_PIN PE10 // usart0 00044 #define TEST_SERIAL_TWO_RX_PIN PC1 // usart1 00045 00046 #elif defined(TARGET_B96B_F446VE) 00047 #define TEST_SERIAL_ONE_TX_PIN D1 // UART2 00048 #define TEST_SERIAL_TWO_RX_PIN D4 // UART5 00049 00050 #elif defined(TARGET_NUCLEO_L152RE) 00051 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00052 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00053 00054 #elif defined(TARGET_NUCLEO_F103RB) 00055 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00056 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00057 00058 #elif defined(TARGET_NUCLEO_F207ZG) 00059 #define TEST_SERIAL_ONE_TX_PIN PC_12 // UART5 00060 #define TEST_SERIAL_TWO_RX_PIN PC_11 // UART4 00061 00062 #elif defined(TARGET_DISCO_F334C8) 00063 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00064 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00065 00066 #elif defined(TARGET_NUCLEO_F302R8) 00067 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00068 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00069 00070 #elif defined(TARGET_NUCLEO_F303RE) 00071 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00072 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00073 00074 #elif defined(TARGET_NUCLEO_F334R8) 00075 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00076 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00077 00078 #elif defined(TARGET_DISCO_F429ZI) 00079 #define TEST_SERIAL_ONE_TX_PIN PD_5 // UART2 00080 #define TEST_SERIAL_TWO_RX_PIN PG_9 // UART6 00081 00082 #elif defined(TARGET_NUCLEO_F401RE) 00083 #define TEST_SERIAL_ONE_TX_PIN PB_6 // UART1 00084 #define TEST_SERIAL_TWO_RX_PIN PC_7 // UART6 00085 00086 #elif defined(TARGET_NUCLEO_F411RE) 00087 #define TEST_SERIAL_ONE_TX_PIN PB_6 // UART1 00088 #define TEST_SERIAL_TWO_RX_PIN PC_7 // UART6 00089 00090 #elif defined(TARGET_NUCLEO_F446RE) 00091 #define TEST_SERIAL_ONE_TX_PIN PB_6 // UART1 00092 #define TEST_SERIAL_TWO_RX_PIN PC_7 // UART6 00093 00094 #elif defined(TARGET_NUCLEO_F410RB) 00095 #define TEST_SERIAL_ONE_TX_PIN PB_6 // UART1 00096 #define TEST_SERIAL_TWO_RX_PIN PC_7 // UART6 00097 00098 #elif defined(TARGET_NUCLEO_F429ZI) 00099 #define TEST_SERIAL_ONE_TX_PIN PE_8 // UART7 00100 #define TEST_SERIAL_TWO_RX_PIN PG_9 // UART6 00101 00102 #elif defined(TARGET_NUCLEO_F446ZE) 00103 #define TEST_SERIAL_ONE_TX_PIN PB_6 // UART1 00104 #define TEST_SERIAL_TWO_RX_PIN PG_9 // UART6 00105 00106 #elif defined(TARGET_NUCLEO_L476RG) 00107 #define TEST_SERIAL_ONE_TX_PIN PB_10 // UART3 00108 #define TEST_SERIAL_TWO_RX_PIN PA_10 // UART1 00109 00110 #elif defined(TARGET_RZ_A1H) 00111 #define TEST_SERIAL_ONE_TX_PIN P8_14 // UART4 00112 #define TEST_SERIAL_TWO_RX_PIN P8_11 // UART5 00113 00114 #else 00115 00116 #error Target not supported 00117 #endif 00118 00119 // Test config 00120 #define SHORT_XFR 3 00121 #define LONG_XFR 16 00122 #define TEST_BYTE_TX_BASE 0x5555 00123 #define TEST_BYTE_RX 0x5A5A 00124 00125 volatile int tx_event_flag; 00126 volatile bool tx_complete; 00127 00128 volatile int rx_event_flag; 00129 volatile bool rx_complete; 00130 00131 void cb_tx_done(int event) 00132 { 00133 tx_complete = true; 00134 tx_event_flag = event; 00135 } 00136 00137 void cb_rx_done(int event) 00138 { 00139 rx_complete = true; 00140 rx_event_flag = event; 00141 } 00142 00143 TEST_GROUP(Serial_Asynchronous) 00144 { 00145 uint8_t tx_buf[LONG_XFR]; 00146 uint8_t rx_buf[LONG_XFR]; 00147 00148 Serial *serial_tx; 00149 Serial *serial_rx; 00150 event_callback_t tx_callback; 00151 event_callback_t rx_callback; 00152 00153 void setup() 00154 { 00155 serial_tx = new Serial(TEST_SERIAL_ONE_TX_PIN, NC); 00156 serial_rx = new Serial(NC, TEST_SERIAL_TWO_RX_PIN); 00157 tx_complete = false; 00158 tx_event_flag = 0; 00159 rx_complete = false; 00160 rx_event_flag = 0; 00161 tx_callback.attach(cb_tx_done); 00162 rx_callback.attach(cb_rx_done); 00163 00164 // Set the default value of tx_buf 00165 for (uint32_t i = 0; i < sizeof(tx_buf); i++) { 00166 tx_buf[i] = i + TEST_BYTE_TX_BASE; 00167 } 00168 memset(rx_buf, TEST_BYTE_RX, sizeof(rx_buf)); 00169 } 00170 00171 void teardown() 00172 { 00173 delete serial_tx; 00174 serial_tx = NULL; 00175 delete serial_rx; 00176 serial_rx = NULL; 00177 00178 } 00179 00180 uint32_t cmpnbufc(uint8_t expect, uint8_t *actual, uint32_t offset, uint32_t end, const char *file, uint32_t line) 00181 { 00182 uint32_t i; 00183 for (i = offset; i < end; i++){ 00184 if (expect != actual[i]) { 00185 break; 00186 } 00187 } 00188 if (i < end) { 00189 CHECK_EQUAL_LOCATION((int)expect, (int)actual[i], file, line); 00190 } 00191 CHECK_EQUAL_LOCATION(end, i, file, line); 00192 return i; 00193 } 00194 00195 uint32_t cmpnbuf(uint8_t *expect, uint8_t *actual, uint32_t offset, uint32_t end, const char *file, uint32_t line) 00196 { 00197 uint32_t i; 00198 for (i = offset; i < end; i++){ 00199 if (expect[i] != actual[i]) { 00200 break; 00201 } 00202 } 00203 if (i < end) { 00204 CHECK_EQUAL_LOCATION((int)expect[i], (int)actual[i], file, line); 00205 } 00206 CHECK_EQUAL_LOCATION(end, i, file, line); 00207 return i; 00208 } 00209 00210 }; 00211 00212 TEST(Serial_Asynchronous, short_tx_0_rx) 00213 { 00214 int rc; 00215 rc = serial_tx->write(tx_buf, SHORT_XFR, tx_callback, -1); 00216 CHECK_EQUAL(0, rc); 00217 00218 while (!tx_complete); 00219 00220 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00221 // rx buffer unchanged 00222 cmpnbufc(TEST_BYTE_RX, rx_buf, 0, sizeof(rx_buf), __FILE__, __LINE__); 00223 } 00224 00225 TEST(Serial_Asynchronous, short_tx_short_rx) 00226 { 00227 int rc; 00228 serial_rx->read(rx_buf, SHORT_XFR, rx_callback, -1); 00229 rc = serial_tx->write(tx_buf, SHORT_XFR, tx_callback, -1); 00230 CHECK_EQUAL(0, rc); 00231 00232 while ((!tx_complete) || (!rx_complete)); 00233 00234 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00235 CHECK_EQUAL(SERIAL_EVENT_RX_COMPLETE, rx_event_flag); 00236 00237 // Check that the receive buffer contains the fill byte. 00238 cmpnbuf(tx_buf, rx_buf, 0, SHORT_XFR, __FILE__, __LINE__); 00239 // Check that remaining portion of the receive buffer contains the rx test byte 00240 cmpnbufc(TEST_BYTE_RX, rx_buf, SHORT_XFR, sizeof(rx_buf), __FILE__, __LINE__); 00241 } 00242 00243 TEST(Serial_Asynchronous, long_tx_long_rx) 00244 { 00245 int rc; 00246 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1); 00247 rc = serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00248 CHECK_EQUAL(0, rc); 00249 00250 while ((!tx_complete) || (!rx_complete)); 00251 00252 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00253 CHECK_EQUAL(SERIAL_EVENT_RX_COMPLETE, rx_event_flag); 00254 00255 // Check that the receive buffer contains the fill byte. 00256 cmpnbuf(tx_buf, rx_buf, 0, LONG_XFR, __FILE__, __LINE__); 00257 // Check that remaining portion of the receive buffer contains the rx test byte 00258 cmpnbufc(TEST_BYTE_RX, rx_buf, LONG_XFR, sizeof(rx_buf), __FILE__, __LINE__); 00259 } 00260 00261 TEST(Serial_Asynchronous, rx_parity_error) 00262 { 00263 int rc; 00264 // Set different parity for RX and TX 00265 serial_rx->format(8, SerialBase::Even, 1); 00266 serial_tx->format(8, SerialBase::Odd, 1); 00267 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1); 00268 rc = serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00269 CHECK_EQUAL(0, rc); 00270 00271 while ((!tx_complete) || (!rx_complete)); 00272 00273 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00274 CHECK_EQUAL(SERIAL_EVENT_RX_PARITY_ERROR, rx_event_flag); 00275 } 00276 00277 TEST(Serial_Asynchronous, rx_framing_error) 00278 { 00279 int rc; 00280 serial_tx->baud(4800); 00281 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1); 00282 rc = serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00283 CHECK_EQUAL(0, rc); 00284 00285 while ((!tx_complete) || (!rx_complete)); 00286 00287 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00288 CHECK_EQUAL(SERIAL_EVENT_RX_FRAMING_ERROR, rx_event_flag); 00289 } 00290 00291 TEST(Serial_Asynchronous, char_matching_success) 00292 { 00293 // match found 00294 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1, (uint8_t)(TEST_BYTE_TX_BASE+5)); 00295 serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00296 00297 while ((!tx_complete) || (!rx_complete)); 00298 00299 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00300 CHECK_EQUAL(SERIAL_EVENT_RX_CHARACTER_MATCH, rx_event_flag); 00301 00302 cmpnbufc(TEST_BYTE_RX, rx_buf, 5, sizeof(rx_buf), __FILE__, __LINE__); 00303 } 00304 00305 TEST(Serial_Asynchronous, char_matching_failed) 00306 { 00307 // no match found (specified match char is not in tx buffer) 00308 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1, (uint8_t)(TEST_BYTE_TX_BASE + sizeof(tx_buf))); 00309 serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00310 00311 while ((!tx_complete) || (!rx_complete)); 00312 00313 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00314 CHECK_EQUAL(SERIAL_EVENT_RX_COMPLETE, rx_event_flag); 00315 00316 cmpnbuf(tx_buf, rx_buf, 0, LONG_XFR, __FILE__, __LINE__); 00317 } 00318 00319 TEST(Serial_Asynchronous, char_matching_with_complete) 00320 { 00321 serial_rx->read(rx_buf, LONG_XFR, rx_callback, -1, (uint8_t)(TEST_BYTE_TX_BASE + sizeof(tx_buf) - 1)); 00322 serial_tx->write(tx_buf, LONG_XFR, tx_callback, -1); 00323 00324 while ((!tx_complete) || (!rx_complete)); 00325 00326 CHECK_EQUAL(SERIAL_EVENT_TX_COMPLETE, tx_event_flag); 00327 CHECK_EQUAL((SERIAL_EVENT_RX_COMPLETE | SERIAL_EVENT_RX_CHARACTER_MATCH), rx_event_flag); 00328 00329 cmpnbuf(tx_buf, rx_buf, 0, LONG_XFR, __FILE__, __LINE__); 00330 }
Generated on Tue Jul 12 2022 12:28:48 by
1.7.2
