Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UARTTester.cpp Source File

UARTTester.cpp

00001 /*
00002  * Copyright (c) 2019, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "UARTTester.h"
00019 #include "fpga_config.h"
00020 
00021 void UARTTester::set_baud(uint32_t baudrate)
00022 {
00023     uint32_t divisor = TESTER_CLOCK_FREQUENCY_HZ / baudrate;
00024     // Baud divisor is only 16 bits
00025     MBED_ASSERT((divisor & 0xFFFF0000) == 0);
00026     write(TESTER_UART_BAUD_DIVISOR, (uint8_t *)&divisor, TESTER_UART_BAUD_DIVISOR_SIZE);
00027 }
00028 
00029 void UARTTester::set_bits(uint8_t data_bits)
00030 {
00031     // Check for supported range
00032     MBED_ASSERT((data_bits >= 1) && (data_bits <= 16));
00033     write(TESTER_UART_BIT_COUNT, &data_bits, sizeof(data_bits));
00034 }
00035 
00036 void UARTTester::set_stops(uint8_t stop_bits)
00037 {
00038     // Check for supported range
00039     MBED_ASSERT((stop_bits >= 1) && (stop_bits <= 16));
00040     write(TESTER_UART_STOP_COUNT, &stop_bits, sizeof(stop_bits));
00041 }
00042 
00043 void UARTTester::set_parity(bool enable, bool odd_n_even)
00044 {
00045     uint8_t parity = (enable ? TESTER_UART_PARITY_ENABLE : 0) |
00046                      (odd_n_even ? TESTER_UART_PARITY_ODD_N_EVEN : 0);
00047     write(TESTER_UART_PARITY, &parity, sizeof(parity));
00048 }
00049 
00050 void UARTTester::rx_start()
00051 {
00052     uint8_t data = TESTER_UART_RX_CONTROL_ENABLE;
00053     write(TESTER_UART_RX_CONTROL, &data, sizeof(data));
00054 }
00055 
00056 void UARTTester::rx_stop()
00057 {
00058     uint8_t data = 0;
00059     write(TESTER_UART_RX_CONTROL, &data, sizeof(data));
00060 }
00061 
00062 uint32_t UARTTester::rx_get_checksum()
00063 {
00064     uint32_t checksum = 0;
00065     read(TESTER_UART_RX_CHECKSUM, (uint8_t *)&checksum, sizeof(checksum));
00066     return checksum;
00067 }
00068 
00069 uint32_t UARTTester::rx_get_count()
00070 {
00071     uint32_t count = 0;
00072     read(TESTER_UART_RX_COUNT, (uint8_t *)&count, sizeof(count));
00073     return count;
00074 }
00075 
00076 uint16_t UARTTester::rx_get_data(int prev)
00077 {
00078     MBED_ASSERT((prev >= 1) && (prev <= 4));
00079     uint16_t data = 0;
00080     read(TESTER_UART_RX_PREV_1 - (prev - 1) * 2, (uint8_t *)&data, sizeof(data));
00081     return data;
00082 }
00083 
00084 uint32_t UARTTester::rx_get_parity_errors()
00085 {
00086     uint32_t errors = 0;
00087     read(TESTER_UART_RX_PARITY_ERRORS, (uint8_t *)&errors, sizeof(errors));
00088     return errors;
00089 }
00090 
00091 uint32_t UARTTester::rx_get_stop_errors()
00092 {
00093     uint32_t errors = 0;
00094     read(TESTER_UART_RX_STOP_ERRORS, (uint8_t *)&errors, sizeof(errors));
00095     return errors;
00096 }
00097 
00098 uint32_t UARTTester::rx_get_framing_errors()
00099 {
00100     uint32_t errors = 0;
00101     read(TESTER_UART_RX_FRAMING_ERRORS, (uint8_t *)&errors, sizeof(errors));
00102     return errors;
00103 }
00104 
00105 void UARTTester::tx_start(bool cts_enabled)
00106 {
00107     uint32_t control = TESTER_UART_TX_CONTROL_ENABLE | (cts_enabled ? TESTER_UART_TX_CONTROL_ENABLE_CTS : 0);
00108     write(TESTER_UART_TX_CONTROL, (uint8_t *)&control, sizeof(control));
00109 }
00110 
00111 void UARTTester::tx_stop()
00112 {
00113     uint32_t control = 0;
00114     write(TESTER_UART_TX_CONTROL, (uint8_t *)&control, sizeof(control));
00115 }
00116 
00117 void UARTTester::tx_set_delay(uint32_t delay_ns)
00118 {
00119     uint32_t delay_clks = (delay_ns + TESTER_CLOCK_PERIOD_NS - 1) / TESTER_CLOCK_PERIOD_NS;
00120     write(TESTER_UART_TX_DELAY, (uint8_t *)&delay_clks, sizeof(delay_clks));
00121 }
00122 
00123 void UARTTester::tx_set_count(uint32_t count)
00124 {
00125     write(TESTER_UART_TX_COUNT, (uint8_t *)&count, sizeof(count));
00126 }
00127 
00128 void UARTTester::tx_set_next(uint16_t value)
00129 {
00130     write(TESTER_UART_TX_NEXT, (uint8_t *)&value, sizeof(value));
00131 }
00132 
00133 void UARTTester::cts_deassert_delay(uint32_t delay_ns)
00134 {
00135     uint32_t delay_clks = delay_ns / TESTER_CLOCK_PERIOD_NS;
00136     write(TESTER_UART_CTS_DEACTIVATE_DELAY, (uint8_t *)&delay_clks, sizeof(delay_clks));
00137 }