Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
UARTDisplayer.cpp@20:3b82955f42f7, 2017-01-17 (annotated)
- Committer:
- dupm2216
- Date:
- Tue Jan 17 21:17:31 2017 +0000
- Revision:
- 20:3b82955f42f7
- Parent:
- 18:a21199781d20
- Child:
- 21:a111be2582be
Remove superfluous waits in uart communication
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 | |
dupm2216 | 16:c77e34bc69bc | 8 | UARTDisplayer::UARTDisplayer() |
GaiSensei | 2:b8a20f7e2912 | 9 | { |
dupm2216 | 16:c77e34bc69bc | 10 | this->device.init(); |
GaiSensei | 2:b8a20f7e2912 | 11 | } |
GaiSensei | 2:b8a20f7e2912 | 12 | |
GaiSensei | 2:b8a20f7e2912 | 13 | void UARTDisplayer::displayAngle(float angle) |
GaiSensei | 2:b8a20f7e2912 | 14 | { |
GaiSensei | 13:bb9669053eb3 | 15 | char digits[6]; |
GaiSensei | 13:bb9669053eb3 | 16 | std::snprintf(digits, sizeof digits, "%f", angle); |
GaiSensei | 2:b8a20f7e2912 | 17 | |
GaiSensei | 2:b8a20f7e2912 | 18 | if(digits[1] == '.') |
GaiSensei | 2:b8a20f7e2912 | 19 | { |
GaiSensei | 2:b8a20f7e2912 | 20 | device.write(0); |
GaiSensei | 2:b8a20f7e2912 | 21 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 22 | device.write((int)digits[2] - 48); |
GaiSensei | 2:b8a20f7e2912 | 23 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 24 | } |
GaiSensei | 2:b8a20f7e2912 | 25 | else |
GaiSensei | 2:b8a20f7e2912 | 26 | { |
GaiSensei | 2:b8a20f7e2912 | 27 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 28 | device.write((int)digits[1] - 48); |
GaiSensei | 2:b8a20f7e2912 | 29 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 30 | device.write((int)digits[4] - 48); |
GaiSensei | 2:b8a20f7e2912 | 31 | } |
GaiSensei | 2:b8a20f7e2912 | 32 | |
GaiSensei | 2:b8a20f7e2912 | 33 | device.write(DECIMAL_CONTROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 34 | device.write(FLOATING_POINT_DOT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 35 | } |
GaiSensei | 2:b8a20f7e2912 | 36 | |
GaiSensei | 2:b8a20f7e2912 | 37 | void UARTDisplayer::reset() |
GaiSensei | 2:b8a20f7e2912 | 38 | { |
GaiSensei | 13:bb9669053eb3 | 39 | device.write(CLEAR_DISPLAY_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 40 | device.write(CURSOR_CONROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 41 | device.write(MOST_LEFT_DIGIT_POSITION); |
GaiSensei | 13:bb9669053eb3 | 42 | } |
GaiSensei | 13:bb9669053eb3 | 43 | |
GaiSensei | 14:2f89279586cb | 44 | void HomemadeUART::write(const unsigned char value) |
GaiSensei | 13:bb9669053eb3 | 45 | { |
dupm2216 | 16:c77e34bc69bc | 46 | //UART1 Transmitter Holding Register |
dupm2216 | 16:c77e34bc69bc | 47 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, value); |
GaiSensei | 13:bb9669053eb3 | 48 | } |
GaiSensei | 13:bb9669053eb3 | 49 | |
GaiSensei | 13:bb9669053eb3 | 50 | void HomemadeUART::init() |
GaiSensei | 13:bb9669053eb3 | 51 | { |
dupm2216 | 16:c77e34bc69bc | 52 | //Power: In the PCONP register (Table 46), set bits PCUART1 |
dupm2216 | 16:c77e34bc69bc | 53 | //Default: enabled |
dupm2216 | 16:c77e34bc69bc | 54 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 4, 4, 0x01); |
dupm2216 | 16:c77e34bc69bc | 55 | |
dupm2216 | 16:c77e34bc69bc | 56 | //Peripheral clock: In the PCLKSEL0 register (Table 40), select PCLK_UART1 |
dupm2216 | 16:c77e34bc69bc | 57 | //Default: PCLK_peripheral = CCLK/4 |
dupm2216 | 16:c77e34bc69bc | 58 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC1A8), 8, 9, 0x00); |
dupm2216 | 18:a21199781d20 | 59 | |
dupm2216 | 16:c77e34bc69bc | 60 | set_lcr(); |
GaiSensei | 14:2f89279586cb | 61 | set_baud_rate(9600); |
dupm2216 | 15:b38d9d210e32 | 62 | set_fifo(); |
dupm2216 | 16:c77e34bc69bc | 63 | |
dupm2216 | 16:c77e34bc69bc | 64 | //Pins: Select UART pins through PINSEL registers |
dupm2216 | 16:c77e34bc69bc | 65 | //Pin Function Select Register 1 (TXD1) |
dupm2216 | 16:c77e34bc69bc | 66 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4002C000), 30, 31, 0x01); |
GaiSensei | 14:2f89279586cb | 67 | } |
GaiSensei | 14:2f89279586cb | 68 | |
GaiSensei | 14:2f89279586cb | 69 | void HomemadeUART::set_baud_rate(const unsigned int baud_rate) |
GaiSensei | 14:2f89279586cb | 70 | { |
dupm2216 | 16:c77e34bc69bc | 71 | const unsigned int m = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; |
dupm2216 | 16:c77e34bc69bc | 72 | const unsigned int n = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; |
GaiSensei | 14:2f89279586cb | 73 | const unsigned int in_frequency = 4000000; //Hz |
dupm2216 | 16:c77e34bc69bc | 74 | const unsigned int fcco = (2 * m * in_frequency) / n; |
dupm2216 | 15:b38d9d210e32 | 75 | const unsigned int x = fcco / (baud_rate * 16 * 4); |
GaiSensei | 14:2f89279586cb | 76 | |
dupm2216 | 16:c77e34bc69bc | 77 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =1 |
dupm2216 | 16:c77e34bc69bc | 78 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x01); |
dupm2216 | 16:c77e34bc69bc | 79 | //Set the baud rate |
dupm2216 | 16:c77e34bc69bc | 80 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, x); //UART1 Divisor Latch LSB Register (U1DLL |
dupm2216 | 16:c77e34bc69bc | 81 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010004), 0, 7, 0x00); //UART1 Divisor Latch MSB Register (U1DLM) |
dupm2216 | 16:c77e34bc69bc | 82 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =0 |
dupm2216 | 16:c77e34bc69bc | 83 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x00); |
GaiSensei | 14:2f89279586cb | 84 | } |
GaiSensei | 14:2f89279586cb | 85 | |
GaiSensei | 14:2f89279586cb | 86 | void HomemadeUART::set_fifo() |
GaiSensei | 14:2f89279586cb | 87 | { |
dupm2216 | 16:c77e34bc69bc | 88 | //UART FIFO: Use bit FIFO enable (bit 0) in register U0FCR (Table 297) to enable FIFO |
dupm2216 | 16:c77e34bc69bc | 89 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010008), 0, 0, 0x01); |
GaiSensei | 14:2f89279586cb | 90 | } |
GaiSensei | 14:2f89279586cb | 91 | |
GaiSensei | 14:2f89279586cb | 92 | void HomemadeUART::set_lcr() |
GaiSensei | 14:2f89279586cb | 93 | { |
dupm2216 | 16:c77e34bc69bc | 94 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 0, 1, 0x03); |
GaiSensei | 2:b8a20f7e2912 | 95 | } |