Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
UARTDisplayer.cpp@14:2f89279586cb, 2017-01-16 (annotated)
- Committer:
- GaiSensei
- Date:
- Mon Jan 16 02:28:40 2017 +0000
- Revision:
- 14:2f89279586cb
- Parent:
- 13:bb9669053eb3
- Child:
- 15:b38d9d210e32
Bonne nuit; ; Tout est bris?; ; Ajouter mask dans write_bits pour que les reserved soit des 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GaiSensei | 2:b8a20f7e2912 | 1 | #include "UARTDisplayer.hpp" |
GaiSensei | 13:bb9669053eb3 | 2 | #include <cstdio> |
GaiSensei | 13:bb9669053eb3 | 3 | #include "Utility.hpp" |
GaiSensei | 14:2f89279586cb | 4 | #include "HomemadeMbed.hpp" |
GaiSensei | 13:bb9669053eb3 | 5 | #include "mbed.h" //Needed for function wait |
GaiSensei | 14:2f89279586cb | 6 | #include <cassert> |
GaiSensei | 2:b8a20f7e2912 | 7 | |
GaiSensei | 13:bb9669053eb3 | 8 | UARTDisplayer::UARTDisplayer(PinName tx_pin) : |
GaiSensei | 13:bb9669053eb3 | 9 | device(tx_pin) |
GaiSensei | 2:b8a20f7e2912 | 10 | { |
GaiSensei | 2:b8a20f7e2912 | 11 | |
GaiSensei | 2:b8a20f7e2912 | 12 | } |
GaiSensei | 2:b8a20f7e2912 | 13 | |
GaiSensei | 2:b8a20f7e2912 | 14 | void UARTDisplayer::displayAngle(float angle) |
GaiSensei | 2:b8a20f7e2912 | 15 | { |
GaiSensei | 13:bb9669053eb3 | 16 | char digits[6]; |
GaiSensei | 13:bb9669053eb3 | 17 | std::snprintf(digits, sizeof digits, "%f", angle); |
GaiSensei | 2:b8a20f7e2912 | 18 | |
GaiSensei | 2:b8a20f7e2912 | 19 | if(digits[1] == '.') |
GaiSensei | 2:b8a20f7e2912 | 20 | { |
GaiSensei | 2:b8a20f7e2912 | 21 | device.write(0); |
GaiSensei | 2:b8a20f7e2912 | 22 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 23 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 24 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 25 | device.write((int)digits[2] - 48); |
GaiSensei | 2:b8a20f7e2912 | 26 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 27 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 28 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 29 | } |
GaiSensei | 2:b8a20f7e2912 | 30 | else |
GaiSensei | 2:b8a20f7e2912 | 31 | { |
GaiSensei | 2:b8a20f7e2912 | 32 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 33 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 34 | device.write((int)digits[1] - 48); |
GaiSensei | 2:b8a20f7e2912 | 35 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 36 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 37 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 38 | device.write((int)digits[4] - 48); |
GaiSensei | 2:b8a20f7e2912 | 39 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 40 | } |
GaiSensei | 2:b8a20f7e2912 | 41 | |
GaiSensei | 2:b8a20f7e2912 | 42 | device.write(DECIMAL_CONTROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 43 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 44 | device.write(FLOATING_POINT_DOT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 45 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 46 | } |
GaiSensei | 2:b8a20f7e2912 | 47 | |
GaiSensei | 2:b8a20f7e2912 | 48 | void UARTDisplayer::reset() |
GaiSensei | 2:b8a20f7e2912 | 49 | { |
GaiSensei | 13:bb9669053eb3 | 50 | device.write(CLEAR_DISPLAY_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 51 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 52 | device.write(CURSOR_CONROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 53 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 54 | device.write(MOST_LEFT_DIGIT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 55 | wait(0.001); |
GaiSensei | 13:bb9669053eb3 | 56 | } |
GaiSensei | 13:bb9669053eb3 | 57 | |
GaiSensei | 13:bb9669053eb3 | 58 | HomemadeUART::HomemadeUART(PinName tx_pin) : |
GaiSensei | 13:bb9669053eb3 | 59 | tx_pin(tx_pin) |
GaiSensei | 13:bb9669053eb3 | 60 | { |
GaiSensei | 13:bb9669053eb3 | 61 | } |
GaiSensei | 13:bb9669053eb3 | 62 | |
GaiSensei | 14:2f89279586cb | 63 | void HomemadeUART::write(const unsigned char value) |
GaiSensei | 13:bb9669053eb3 | 64 | { |
GaiSensei | 14:2f89279586cb | 65 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, value); |
GaiSensei | 14:2f89279586cb | 66 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, value); |
GaiSensei | 14:2f89279586cb | 67 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, value); |
GaiSensei | 13:bb9669053eb3 | 68 | } |
GaiSensei | 13:bb9669053eb3 | 69 | |
GaiSensei | 13:bb9669053eb3 | 70 | void HomemadeUART::init() |
GaiSensei | 13:bb9669053eb3 | 71 | { |
GaiSensei | 14:2f89279586cb | 72 | printf("HomemadeUART::init()\r\n"); |
GaiSensei | 14:2f89279586cb | 73 | |
GaiSensei | 14:2f89279586cb | 74 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23, 0x03); |
GaiSensei | 14:2f89279586cb | 75 | assert(0x03 == homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23)); |
GaiSensei | 14:2f89279586cb | 76 | |
GaiSensei | 14:2f89279586cb | 77 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 78 | |
GaiSensei | 14:2f89279586cb | 79 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 80 | assert(false); |
GaiSensei | 14:2f89279586cb | 81 | |
GaiSensei | 14:2f89279586cb | 82 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 83 | assert(false); |
GaiSensei | 14:2f89279586cb | 84 | |
GaiSensei | 14:2f89279586cb | 85 | /* |
GaiSensei | 13:bb9669053eb3 | 86 | int* register_address1 = reinterpret_cast<int*>(0x400FC0C4); |
GaiSensei | 13:bb9669053eb3 | 87 | const int previous_register_value1 = *register_address1; |
GaiSensei | 13:bb9669053eb3 | 88 | int new_register_value1 = previous_register_value1; |
GaiSensei | 13:bb9669053eb3 | 89 | new_register_value1 = utility::update_bit(new_register_value1, 22, 1); |
GaiSensei | 13:bb9669053eb3 | 90 | new_register_value1 = utility::update_bit(new_register_value1, 23, 1); |
GaiSensei | 13:bb9669053eb3 | 91 | *register_address1 = new_register_value1; |
GaiSensei | 2:b8a20f7e2912 | 92 | |
GaiSensei | 13:bb9669053eb3 | 93 | int* register_address2 = reinterpret_cast<int*>(0x4000C00C); |
GaiSensei | 13:bb9669053eb3 | 94 | const int previous_register_value2 = *register_address2; |
GaiSensei | 13:bb9669053eb3 | 95 | int new_register_value2 = previous_register_value2; |
GaiSensei | 13:bb9669053eb3 | 96 | new_register_value2 = utility::update_bit(new_register_value2, 7, 1); |
GaiSensei | 13:bb9669053eb3 | 97 | *register_address2 = new_register_value2; |
GaiSensei | 13:bb9669053eb3 | 98 | |
GaiSensei | 13:bb9669053eb3 | 99 | int* register_address3 = reinterpret_cast<int*>(0x4009800C); |
GaiSensei | 13:bb9669053eb3 | 100 | const int previous_register_value3 = *register_address3; |
GaiSensei | 13:bb9669053eb3 | 101 | int new_register_value3 = previous_register_value3; |
GaiSensei | 13:bb9669053eb3 | 102 | new_register_value3 = utility::update_bit(new_register_value3, 7, 1); |
GaiSensei | 13:bb9669053eb3 | 103 | *register_address3 = new_register_value3; |
GaiSensei | 13:bb9669053eb3 | 104 | |
GaiSensei | 13:bb9669053eb3 | 105 | int* register_address4 = reinterpret_cast<int*>(0x4009C00C); |
GaiSensei | 13:bb9669053eb3 | 106 | const int previous_register_value4 = *register_address4; |
GaiSensei | 13:bb9669053eb3 | 107 | int new_register_value4 = previous_register_value4; |
GaiSensei | 13:bb9669053eb3 | 108 | new_register_value4 = utility::update_bit(new_register_value4, 7, 1); |
GaiSensei | 13:bb9669053eb3 | 109 | *register_address4 = new_register_value4; |
GaiSensei | 14:2f89279586cb | 110 | */ |
GaiSensei | 14:2f89279586cb | 111 | |
GaiSensei | 14:2f89279586cb | 112 | set_baud_rate(9600); |
GaiSensei | 14:2f89279586cb | 113 | } |
GaiSensei | 14:2f89279586cb | 114 | |
GaiSensei | 14:2f89279586cb | 115 | void HomemadeUART::set_baud_rate(const unsigned int baud_rate) |
GaiSensei | 14:2f89279586cb | 116 | { |
GaiSensei | 14:2f89279586cb | 117 | const unsigned int m_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; |
GaiSensei | 14:2f89279586cb | 118 | const unsigned int n_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; |
GaiSensei | 14:2f89279586cb | 119 | const unsigned int in_frequency = 4000000; //Hz |
GaiSensei | 14:2f89279586cb | 120 | const unsigned int fcco = (2 * m_value * in_frequency) / n_value; |
GaiSensei | 14:2f89279586cb | 121 | |
GaiSensei | 14:2f89279586cb | 122 | const unsigned int x = fcco / (baud_rate * 16 * 4); |
GaiSensei | 14:2f89279586cb | 123 | printf("m_value: %d\r\n", m_value); |
GaiSensei | 14:2f89279586cb | 124 | printf("n_value: %d\r\n", n_value); |
GaiSensei | 14:2f89279586cb | 125 | printf("in_frequency: %d\r\n", in_frequency); |
GaiSensei | 14:2f89279586cb | 126 | printf("fcco: %d\r\n", fcco); |
GaiSensei | 14:2f89279586cb | 127 | printf("x: %d\r\n", x); |
GaiSensei | 14:2f89279586cb | 128 | |
GaiSensei | 14:2f89279586cb | 129 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, x); |
GaiSensei | 14:2f89279586cb | 130 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, x); |
GaiSensei | 14:2f89279586cb | 131 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, x); |
GaiSensei | 14:2f89279586cb | 132 | } |
GaiSensei | 14:2f89279586cb | 133 | |
GaiSensei | 14:2f89279586cb | 134 | void HomemadeUART::set_fifo() |
GaiSensei | 14:2f89279586cb | 135 | { |
GaiSensei | 14:2f89279586cb | 136 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C008), 0, 0, 1); |
GaiSensei | 14:2f89279586cb | 137 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098008), 0, 0, 1); |
GaiSensei | 14:2f89279586cb | 138 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4007C008), 0, 0, 1); |
GaiSensei | 14:2f89279586cb | 139 | } |
GaiSensei | 14:2f89279586cb | 140 | |
GaiSensei | 14:2f89279586cb | 141 | void HomemadeUART::set_lcr() |
GaiSensei | 14:2f89279586cb | 142 | { |
GaiSensei | 14:2f89279586cb | 143 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 0, 1, 1); |
GaiSensei | 14:2f89279586cb | 144 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 0, 1, 1); |
GaiSensei | 14:2f89279586cb | 145 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 0, 1, 1); |
GaiSensei | 2:b8a20f7e2912 | 146 | } |