| 1 | /**@file |
| 2 | @brief Test baudrate calculator code |
| 3 | |
| 4 | @author Thomas Jarosch |
| 5 | */ |
| 6 | |
| 7 | /*************************************************************************** |
| 8 | * * |
| 9 | * This program is free software; you can redistribute it and/or modify * |
| 10 | * it under the terms of the GNU Lesser General Public License * |
| 11 | * version 2.1 as published by the Free Software Foundation; * |
| 12 | * * |
| 13 | ***************************************************************************/ |
| 14 | |
| 15 | #include <ftdi.h> |
| 16 | |
| 17 | #define BOOST_TEST_DYN_LINK |
| 18 | #include <boost/test/unit_test.hpp> |
| 19 | #include <boost/foreach.hpp> |
| 20 | #include <vector> |
| 21 | #include <map> |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | extern "C" int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi, |
| 26 | unsigned short *value, unsigned short *index); |
| 27 | |
| 28 | /// Basic initialization of libftdi for every test |
| 29 | class BaseFTDIFixture |
| 30 | { |
| 31 | protected: |
| 32 | ftdi_context *ftdi; |
| 33 | |
| 34 | public: |
| 35 | BaseFTDIFixture() |
| 36 | : ftdi(NULL) |
| 37 | { |
| 38 | ftdi = ftdi_new(); |
| 39 | } |
| 40 | |
| 41 | ~BaseFTDIFixture() |
| 42 | { |
| 43 | delete ftdi; |
| 44 | ftdi = NULL; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | BOOST_FIXTURE_TEST_SUITE(Baudrate, BaseFTDIFixture) |
| 49 | |
| 50 | /// Helper class to store the convert_baudrate_UT_export result |
| 51 | struct calc_result |
| 52 | { |
| 53 | int actual_baudrate; |
| 54 | unsigned short divisor; |
| 55 | unsigned short fractional_bits; |
| 56 | unsigned short clock; |
| 57 | |
| 58 | calc_result(int actual, unsigned short my_divisor, unsigned short my_fractional_bits, unsigned short my_clock) |
| 59 | : actual_baudrate(actual) |
| 60 | , divisor(my_divisor) |
| 61 | , fractional_bits(my_fractional_bits) |
| 62 | , clock(my_clock) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | calc_result() |
| 67 | : actual_baudrate(0) |
| 68 | , divisor(0) |
| 69 | , fractional_bits(0) |
| 70 | , clock(0) |
| 71 | { |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * @brief Test convert_baudrate code against a list of baud rates |
| 77 | * |
| 78 | * @param baudrates Baudrates to check |
| 79 | **/ |
| 80 | static void test_baudrates(ftdi_context *ftdi, const map<int, calc_result> &baudrates) |
| 81 | { |
| 82 | typedef std::pair<int, calc_result> baudrate_type; |
| 83 | BOOST_FOREACH(const baudrate_type &baudrate, baudrates) |
| 84 | { |
| 85 | unsigned short calc_value = 0, calc_index = 0; |
| 86 | int calc_baudrate = convert_baudrate_UT_export(baudrate.first, ftdi, &calc_value, &calc_index); |
| 87 | |
| 88 | const calc_result *res = &baudrate.second; |
| 89 | |
| 90 | unsigned short divisor = calc_value & 0x3fff; |
| 91 | unsigned short fractional_bits = (calc_value >> 14); |
| 92 | unsigned short clock = (calc_index & 0x200) ? 120 : 48; |
| 93 | |
| 94 | switch (ftdi->type) |
| 95 | { |
| 96 | case TYPE_232H: |
| 97 | case TYPE_2232H: |
| 98 | case TYPE_4232H: |
| 99 | fractional_bits |= (calc_index & 0x100) ? 4 : 0; |
| 100 | break; |
| 101 | case TYPE_R: |
| 102 | case TYPE_2232C: |
| 103 | case TYPE_BM: |
| 104 | fractional_bits |= (calc_index & 0x001) ? 4 : 0; |
| 105 | break; |
| 106 | default:; |
| 107 | } |
| 108 | // Aid debugging since this test is a generic function |
| 109 | BOOST_CHECK_MESSAGE(res->actual_baudrate == calc_baudrate && res->divisor == divisor && res->fractional_bits == fractional_bits |
| 110 | && res->clock == clock, |
| 111 | "\n\nERROR: baudrate calculation failed for --" << baudrate.first << " baud--. Details below: "); |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(res->actual_baudrate, calc_baudrate); |
| 114 | BOOST_CHECK_EQUAL(res->divisor, divisor); |
| 115 | BOOST_CHECK_EQUAL(res->fractional_bits, fractional_bits); |
| 116 | BOOST_CHECK_EQUAL(res->clock, clock); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | BOOST_AUTO_TEST_CASE(TypeAMFixedBaudrates) |
| 121 | { |
| 122 | ftdi->type = TYPE_AM; |
| 123 | |
| 124 | map<int, calc_result> baudrates; |
| 125 | baudrates[183] = calc_result(183, 16383, 3, 48); |
| 126 | baudrates[300] = calc_result(300, 10000, 0, 48); |
| 127 | baudrates[600] = calc_result(600, 5000, 0, 48); |
| 128 | baudrates[1200] = calc_result(1200, 2500, 0, 48); |
| 129 | baudrates[2400] = calc_result(2400, 1250, 0, 48); |
| 130 | baudrates[4800] = calc_result(4800, 625, 0, 48); |
| 131 | baudrates[9600] = calc_result(9600, 312, 1, 48); |
| 132 | baudrates[19200] = calc_result(19200, 156, 2, 48); |
| 133 | baudrates[38400] = calc_result(38400, 78, 3, 48); |
| 134 | baudrates[57600] = calc_result(57554, 52, 3, 48); |
| 135 | baudrates[115200] = calc_result(115385, 26, 0, 48); |
| 136 | baudrates[230400] = calc_result(230769, 13, 0, 48); |
| 137 | baudrates[460800] = calc_result(461538, 6, 1, 48); |
| 138 | baudrates[921600] = calc_result(923077, 3, 2, 48); |
| 139 | baudrates[1000000] = calc_result(1000000, 3, 0, 48); |
| 140 | baudrates[1090512] = calc_result(1000000, 3, 0, 48); |
| 141 | baudrates[1090909] = calc_result(1000000, 3, 0, 48); |
| 142 | baudrates[1090910] = calc_result(1200000, 2, 1, 48); |
| 143 | baudrates[1200000] = calc_result(1200000, 2, 1, 48); |
| 144 | baudrates[1333333] = calc_result(1333333, 2, 2, 48); |
| 145 | baudrates[1411764] = calc_result(1411765, 2, 3, 48); |
| 146 | baudrates[1500000] = calc_result(1500000, 2, 0, 48); |
| 147 | baudrates[2000000] = calc_result(2000000, 1, 0, 48); |
| 148 | baudrates[3000000] = calc_result(3000000, 0, 0, 48); |
| 149 | |
| 150 | test_baudrates(ftdi, baudrates); |
| 151 | } |
| 152 | |
| 153 | BOOST_AUTO_TEST_CASE(TypeBMFixedBaudrates) |
| 154 | { |
| 155 | // Unify testing of chips behaving the same |
| 156 | std::vector<enum ftdi_chip_type> test_types; |
| 157 | test_types.push_back(TYPE_BM); |
| 158 | test_types.push_back(TYPE_2232C); |
| 159 | test_types.push_back(TYPE_R); |
| 160 | |
| 161 | map<int, calc_result> baudrates; |
| 162 | baudrates[183] = calc_result(183, 16383, 7, 48); |
| 163 | baudrates[300] = calc_result(300, 10000, 0, 48); |
| 164 | baudrates[600] = calc_result(600, 5000, 0, 48); |
| 165 | baudrates[1200] = calc_result(1200, 2500, 0, 48); |
| 166 | baudrates[2400] = calc_result(2400, 1250, 0, 48); |
| 167 | baudrates[4800] = calc_result(4800, 625, 0, 48); |
| 168 | baudrates[9600] = calc_result(9600, 312, 1, 48); |
| 169 | baudrates[19200] = calc_result(19200, 156, 2, 48); |
| 170 | baudrates[38400] = calc_result(38400, 78, 3, 48); |
| 171 | baudrates[57600] = calc_result(57553, 52, 3, 48); |
| 172 | baudrates[115200] = calc_result(115384, 26, 0, 48); |
| 173 | baudrates[230400] = calc_result(230769, 13, 0, 48); |
| 174 | baudrates[460800] = calc_result(461538, 6, 1, 48); |
| 175 | baudrates[921600] = calc_result(923076, 3, 2, 48); |
| 176 | baudrates[1000000] = calc_result(1000000, 3, 0, 48); |
| 177 | baudrates[1050000] = calc_result(1043478, 2, 7, 48); |
| 178 | baudrates[1400000] = calc_result(1411764, 2, 3, 48); |
| 179 | baudrates[1500000] = calc_result(1500000, 2, 0, 48); |
| 180 | baudrates[2000000] = calc_result(2000000, 1, 0, 48); |
| 181 | baudrates[3000000] = calc_result(3000000, 0, 0, 48); |
| 182 | |
| 183 | BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types) |
| 184 | { |
| 185 | ftdi->type = test_chip_type; |
| 186 | test_baudrates(ftdi, baudrates); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(TypeHFixedBaudrates) |
| 191 | { |
| 192 | // Unify testing of chips behaving the same |
| 193 | std::vector<enum ftdi_chip_type> test_types; |
| 194 | test_types.push_back(TYPE_2232H); |
| 195 | test_types.push_back(TYPE_4232H); |
| 196 | test_types.push_back(TYPE_232H); |
| 197 | |
| 198 | map<int, calc_result> baudrates; |
| 199 | baudrates[183] = calc_result(183, 16383, 7, 48); |
| 200 | baudrates[300] = calc_result(300, 10000, 0, 48); |
| 201 | baudrates[600] = calc_result(600, 5000, 0, 48); |
| 202 | baudrates[1200] = calc_result(1200, 10000, 0, 120); |
| 203 | baudrates[2400] = calc_result(2400, 5000, 0, 120); |
| 204 | baudrates[4800] = calc_result(4800, 2500, 0, 120); |
| 205 | baudrates[9600] = calc_result(9600, 1250, 0, 120); |
| 206 | baudrates[19200] = calc_result(19200, 625, 0, 120); |
| 207 | baudrates[38400] = calc_result(38400, 312, 1, 120); |
| 208 | baudrates[57600] = calc_result(57588, 208, 4, 120); |
| 209 | baudrates[115200] = calc_result(115246, 104, 3, 120); |
| 210 | baudrates[230400] = calc_result(230215, 52, 3, 120); |
| 211 | baudrates[460800] = calc_result(461538, 26, 0, 120); |
| 212 | baudrates[921600] = calc_result(923076, 13, 0, 120); |
| 213 | baudrates[1000000] = calc_result(1000000, 12, 0, 120); |
| 214 | baudrates[1000000] = calc_result(1000000, 12, 0, 120); |
| 215 | baudrates[6000000] = calc_result(6000000, 2, 0, 120); |
| 216 | baudrates[4173913] = calc_result(4173913, 2, 7, 120); |
| 217 | baudrates[8000000] = calc_result(8000000, 1, 0, 120); |
| 218 | baudrates[12000000] = calc_result(12000000, 0, 0, 120); |
| 219 | |
| 220 | BOOST_FOREACH(const enum ftdi_chip_type &test_chip_type, test_types) |
| 221 | { |
| 222 | ftdi->type = test_chip_type; |
| 223 | test_baudrates(ftdi, baudrates); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | BOOST_AUTO_TEST_SUITE_END() |