df

Dependencies:   mbed

Fork of APP1 by Team APP

UARTDisplayer.cpp

Committer:
dupm2216
Date:
2017-01-17
Revision:
15:b38d9d210e32
Parent:
14:2f89279586cb
Child:
16:c77e34bc69bc

File content as of revision 15:b38d9d210e32:

#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)
{
   
}

void UARTDisplayer::displayAngle(float angle)
{
    char digits[6];
    std::snprintf(digits, sizeof digits, "%f", angle);
    
    if(digits[1] == '.')
    {
        device.write(0);
        wait(0.001);
        device.write((int)digits[0] - 48);
        wait(0.001);
        device.write((int)digits[2] - 48);
        wait(0.001);
        device.write((int)digits[3] - 48);
        wait(0.001);
    }
    else
    {
        device.write((int)digits[0] - 48);
        wait(0.001);
        device.write((int)digits[1] - 48);
        wait(0.001);
        device.write((int)digits[3] - 48);
        wait(0.001);
        device.write((int)digits[4] - 48);
        wait(0.001);
    }
    
    device.write(DECIMAL_CONTROL_REGISTER);
    wait(0.001);
    device.write(FLOATING_POINT_DOT_POSITION);
    wait(0.001);
}

void UARTDisplayer::reset()
{
    device.write(CLEAR_DISPLAY_REGISTER);
    wait(0.001);
    device.write(CURSOR_CONROL_REGISTER);
    wait(0.001);
    device.write(MOST_LEFT_DIGIT_POSITION);
    wait(0.001);
}

HomemadeUART::HomemadeUART(PinName tx_pin) :
    tx_pin(tx_pin)
{
}

void HomemadeUART::write(const unsigned char value)
{
    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()
{
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23, 0x03);
    //printf("set_baud_rate\r\n");
    //printf("set_baud_rate\rmarde");
    set_baud_rate(9600);
    //printf("fifo\r\n");
    set_fifo();
    //printf("lcr\r\n");
    set_lcr();
}

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);

    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x01);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, x);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x00);
    
    /*homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x01);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, x);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x00);
    
    utility::blink();
    
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x01);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, x);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x00);*/
    
    
    //const unsigned char x_char = x;
    //assert(x == x_char);
    //printf("allo\r\n");
    
   // utility::blink();
}

void HomemadeUART::set_fifo()
{
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C008), 0, 0, 0x01);
    /*homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098008), 0, 0, 0x01);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4007C008), 0, 0, 0x01);*/
}

void HomemadeUART::set_lcr()
{
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 0, 1, 0x03);
   /* homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 0, 1, 0x03);
    homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 0, 1, 0x03);*/
}