Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
Diff: UARTDisplayer.cpp
- Revision:
- 14:2f89279586cb
- Parent:
- 13:bb9669053eb3
- Child:
- 15:b38d9d210e32
diff -r bb9669053eb3 -r 2f89279586cb UARTDisplayer.cpp --- a/UARTDisplayer.cpp Mon Jan 16 00:06:45 2017 +0000 +++ b/UARTDisplayer.cpp Mon Jan 16 02:28:40 2017 +0000 @@ -1,7 +1,9 @@ #include "UARTDisplayer.hpp" #include <cstdio> #include "Utility.hpp" +#include "HomemadeMbed.hpp" #include "mbed.h" //Needed for function wait +#include <cassert> UARTDisplayer::UARTDisplayer(PinName tx_pin) : device(tx_pin) @@ -58,13 +60,29 @@ { } -void HomemadeUART::write(const int value) +void HomemadeUART::write(const unsigned char value) { - //TODO + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, value); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, value); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, value); } void HomemadeUART::init() { + printf("HomemadeUART::init()\r\n"); + + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23, 0x03); + assert(0x03 == homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23)); + + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x01); + + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x01); + assert(false); + + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x01); + assert(false); + + /* int* register_address1 = reinterpret_cast<int*>(0x400FC0C4); const int previous_register_value1 = *register_address1; int new_register_value1 = previous_register_value1; @@ -89,4 +107,40 @@ int new_register_value4 = previous_register_value4; new_register_value4 = utility::update_bit(new_register_value4, 7, 1); *register_address4 = new_register_value4; + */ + + set_baud_rate(9600); +} + +void HomemadeUART::set_baud_rate(const unsigned int baud_rate) +{ + const unsigned int m_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; + const unsigned int n_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; + const unsigned int in_frequency = 4000000; //Hz + const unsigned int fcco = (2 * m_value * in_frequency) / n_value; + + const unsigned int x = fcco / (baud_rate * 16 * 4); + printf("m_value: %d\r\n", m_value); + printf("n_value: %d\r\n", n_value); + printf("in_frequency: %d\r\n", in_frequency); + printf("fcco: %d\r\n", fcco); + printf("x: %d\r\n", x); + + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, x); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, x); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, x); +} + +void HomemadeUART::set_fifo() +{ + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C008), 0, 0, 1); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098008), 0, 0, 1); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4007C008), 0, 0, 1); +} + +void HomemadeUART::set_lcr() +{ + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 0, 1, 1); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 0, 1, 1); + homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 0, 1, 1); } \ No newline at end of file