Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
UARTDisplayer.cpp@21:a111be2582be, 2017-01-18 (annotated)
- Committer:
- dupm2216
- Date:
- Wed Jan 18 02:38:05 2017 +0000
- Revision:
- 21:a111be2582be
- Parent:
- 20:3b82955f42f7
Add code header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dupm2216 | 21:a111be2582be | 1 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 2 | // APP 1: Systèmes à microprocesseurs // |
dupm2216 | 21:a111be2582be | 3 | // // |
dupm2216 | 21:a111be2582be | 4 | // Université de Sherbrooke // |
dupm2216 | 21:a111be2582be | 5 | // Génie informatique // |
dupm2216 | 21:a111be2582be | 6 | // Session 5, Hiver 2017 // |
dupm2216 | 21:a111be2582be | 7 | // // |
dupm2216 | 21:a111be2582be | 8 | // Date: 17 janvier 2017 // |
dupm2216 | 21:a111be2582be | 9 | // // |
dupm2216 | 21:a111be2582be | 10 | // Auteurs: Maxime Dupuis, dupm2216 // |
dupm2216 | 21:a111be2582be | 11 | // Bruno Allaire-Lemay, allb2701 // |
dupm2216 | 21:a111be2582be | 12 | ///////////////////////////////////////////////////////////// |
dupm2216 | 21:a111be2582be | 13 | |
GaiSensei | 2:b8a20f7e2912 | 14 | #include "UARTDisplayer.hpp" |
GaiSensei | 13:bb9669053eb3 | 15 | #include <cstdio> |
GaiSensei | 13:bb9669053eb3 | 16 | #include "Utility.hpp" |
GaiSensei | 14:2f89279586cb | 17 | #include "HomemadeMbed.hpp" |
GaiSensei | 13:bb9669053eb3 | 18 | #include "mbed.h" //Needed for function wait |
GaiSensei | 14:2f89279586cb | 19 | #include <cassert> |
GaiSensei | 2:b8a20f7e2912 | 20 | |
dupm2216 | 16:c77e34bc69bc | 21 | UARTDisplayer::UARTDisplayer() |
GaiSensei | 2:b8a20f7e2912 | 22 | { |
dupm2216 | 16:c77e34bc69bc | 23 | this->device.init(); |
GaiSensei | 2:b8a20f7e2912 | 24 | } |
GaiSensei | 2:b8a20f7e2912 | 25 | |
GaiSensei | 2:b8a20f7e2912 | 26 | void UARTDisplayer::displayAngle(float angle) |
GaiSensei | 2:b8a20f7e2912 | 27 | { |
GaiSensei | 13:bb9669053eb3 | 28 | char digits[6]; |
GaiSensei | 13:bb9669053eb3 | 29 | std::snprintf(digits, sizeof digits, "%f", angle); |
GaiSensei | 2:b8a20f7e2912 | 30 | |
GaiSensei | 2:b8a20f7e2912 | 31 | if(digits[1] == '.') |
GaiSensei | 2:b8a20f7e2912 | 32 | { |
GaiSensei | 2:b8a20f7e2912 | 33 | device.write(0); |
GaiSensei | 2:b8a20f7e2912 | 34 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 35 | device.write((int)digits[2] - 48); |
GaiSensei | 2:b8a20f7e2912 | 36 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 37 | } |
GaiSensei | 2:b8a20f7e2912 | 38 | else |
GaiSensei | 2:b8a20f7e2912 | 39 | { |
GaiSensei | 2:b8a20f7e2912 | 40 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 41 | device.write((int)digits[1] - 48); |
GaiSensei | 2:b8a20f7e2912 | 42 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 43 | device.write((int)digits[4] - 48); |
GaiSensei | 2:b8a20f7e2912 | 44 | } |
GaiSensei | 2:b8a20f7e2912 | 45 | |
GaiSensei | 2:b8a20f7e2912 | 46 | device.write(DECIMAL_CONTROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 47 | device.write(FLOATING_POINT_DOT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 48 | } |
GaiSensei | 2:b8a20f7e2912 | 49 | |
GaiSensei | 2:b8a20f7e2912 | 50 | void UARTDisplayer::reset() |
GaiSensei | 2:b8a20f7e2912 | 51 | { |
GaiSensei | 13:bb9669053eb3 | 52 | device.write(CLEAR_DISPLAY_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 53 | device.write(CURSOR_CONROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 54 | device.write(MOST_LEFT_DIGIT_POSITION); |
GaiSensei | 13:bb9669053eb3 | 55 | } |
GaiSensei | 13:bb9669053eb3 | 56 | |
GaiSensei | 14:2f89279586cb | 57 | void HomemadeUART::write(const unsigned char value) |
GaiSensei | 13:bb9669053eb3 | 58 | { |
dupm2216 | 16:c77e34bc69bc | 59 | //UART1 Transmitter Holding Register |
dupm2216 | 16:c77e34bc69bc | 60 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, value); |
GaiSensei | 13:bb9669053eb3 | 61 | } |
GaiSensei | 13:bb9669053eb3 | 62 | |
GaiSensei | 13:bb9669053eb3 | 63 | void HomemadeUART::init() |
GaiSensei | 13:bb9669053eb3 | 64 | { |
dupm2216 | 16:c77e34bc69bc | 65 | //Power: In the PCONP register (Table 46), set bits PCUART1 |
dupm2216 | 16:c77e34bc69bc | 66 | //Default: enabled |
dupm2216 | 16:c77e34bc69bc | 67 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 4, 4, 0x01); |
dupm2216 | 16:c77e34bc69bc | 68 | |
dupm2216 | 16:c77e34bc69bc | 69 | //Peripheral clock: In the PCLKSEL0 register (Table 40), select PCLK_UART1 |
dupm2216 | 16:c77e34bc69bc | 70 | //Default: PCLK_peripheral = CCLK/4 |
dupm2216 | 16:c77e34bc69bc | 71 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC1A8), 8, 9, 0x00); |
dupm2216 | 18:a21199781d20 | 72 | |
dupm2216 | 16:c77e34bc69bc | 73 | set_lcr(); |
GaiSensei | 14:2f89279586cb | 74 | set_baud_rate(9600); |
dupm2216 | 15:b38d9d210e32 | 75 | set_fifo(); |
dupm2216 | 16:c77e34bc69bc | 76 | |
dupm2216 | 16:c77e34bc69bc | 77 | //Pins: Select UART pins through PINSEL registers |
dupm2216 | 16:c77e34bc69bc | 78 | //Pin Function Select Register 1 (TXD1) |
dupm2216 | 16:c77e34bc69bc | 79 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4002C000), 30, 31, 0x01); |
GaiSensei | 14:2f89279586cb | 80 | } |
GaiSensei | 14:2f89279586cb | 81 | |
GaiSensei | 14:2f89279586cb | 82 | void HomemadeUART::set_baud_rate(const unsigned int baud_rate) |
GaiSensei | 14:2f89279586cb | 83 | { |
dupm2216 | 16:c77e34bc69bc | 84 | const unsigned int m = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; |
dupm2216 | 16:c77e34bc69bc | 85 | const unsigned int n = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; |
GaiSensei | 14:2f89279586cb | 86 | const unsigned int in_frequency = 4000000; //Hz |
dupm2216 | 16:c77e34bc69bc | 87 | const unsigned int fcco = (2 * m * in_frequency) / n; |
dupm2216 | 15:b38d9d210e32 | 88 | const unsigned int x = fcco / (baud_rate * 16 * 4); |
GaiSensei | 14:2f89279586cb | 89 | |
dupm2216 | 16:c77e34bc69bc | 90 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =1 |
dupm2216 | 16:c77e34bc69bc | 91 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x01); |
dupm2216 | 16:c77e34bc69bc | 92 | //Set the baud rate |
dupm2216 | 16:c77e34bc69bc | 93 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, x); //UART1 Divisor Latch LSB Register (U1DLL |
dupm2216 | 16:c77e34bc69bc | 94 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010004), 0, 7, 0x00); //UART1 Divisor Latch MSB Register (U1DLM) |
dupm2216 | 16:c77e34bc69bc | 95 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =0 |
dupm2216 | 16:c77e34bc69bc | 96 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x00); |
GaiSensei | 14:2f89279586cb | 97 | } |
GaiSensei | 14:2f89279586cb | 98 | |
GaiSensei | 14:2f89279586cb | 99 | void HomemadeUART::set_fifo() |
GaiSensei | 14:2f89279586cb | 100 | { |
dupm2216 | 16:c77e34bc69bc | 101 | //UART FIFO: Use bit FIFO enable (bit 0) in register U0FCR (Table 297) to enable FIFO |
dupm2216 | 16:c77e34bc69bc | 102 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010008), 0, 0, 0x01); |
GaiSensei | 14:2f89279586cb | 103 | } |
GaiSensei | 14:2f89279586cb | 104 | |
GaiSensei | 14:2f89279586cb | 105 | void HomemadeUART::set_lcr() |
GaiSensei | 14:2f89279586cb | 106 | { |
dupm2216 | 16:c77e34bc69bc | 107 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 0, 1, 0x03); |
GaiSensei | 2:b8a20f7e2912 | 108 | } |