Skip to content

Instantly share code, notes, and snippets.

@gustavosinbandera1
Created April 27, 2020 05:22
Show Gist options
  • Save gustavosinbandera1/75b60fc25ff7bb513eb42148be566134 to your computer and use it in GitHub Desktop.
Save gustavosinbandera1/75b60fc25ff7bb513eb42148be566134 to your computer and use it in GitHub Desktop.
// Copyright (c) 2010-2018 Antmicro
// Copyright (c) 2011-2015 Realtime Embedded
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using System;
using System.Threading;
using System.Diagnostics;
using Antmicro.Renode.Peripherals.Bus;
using System.Collections.Generic;
using Antmicro.Renode.Core;
using Antmicro.Renode.Logging;
using Antmicro.Renode.Peripherals.Miscellaneous;
using Antmicro.Migrant;
using System.IO;
using System;
using System.IO.Ports;
using System.Collections;
namespace Antmicro.Renode.Peripherals.UART
{
[AllowedTranslations(AllowedTranslation.ByteToDoubleWord | AllowedTranslation.WordToDoubleWord)]
public class PL012 : IDoubleWordPeripheral, IUART, IKnownSize
{
//public RcvThread receiver = new RcvThread();
public static bool complete = true;
int index = 0;
public byte[] array = new byte[255];
int length;
public PL012(Machine machine, int size = 0x1000)
{
q = new Queue();
this.machine = machine;
this.size = size;
IRQ = new GPIO();
Reset();
idHelper = new PrimeCellIDHelper(size, new byte[] { 0x11, 0x10, 0x14, 0x00, 0x0D, 0xF0, 0x05, 0xB1 }, this);
}
public long Size
{
get
{
return size;
}
}
public void OpenPort()
{
string[] lines = { "some text1", "some text2", "some text3" };
File.WriteAllLines(@"/home/turpial-text.txt", lines);
Console.WriteLine("\nHello TURPIAL FROM RENODE TO FILE\n");
SerialPort Serial_tty = new SerialPort();
Serial_tty.PortName = "/dev/ttyUSB0"; //Assign the port name,
//here ttyUSB0 is the name of the USB to Serial Converter used.
try
{
Serial_tty.Open();//Open the Port
Console.WriteLine("Serial Port {0} Opened", Serial_tty.PortName);
}
catch
{
Console.WriteLine("ERROR in Opening Serial Port");
}
}
public void WriteChar(byte value)
{
}
[field: Transient]
public event Action<byte> CharReceived;
public GPIO IRQ { get; private set; }
public Bits StopBits { get { return (lineControl & (uint)LineControl.TwoStopBitsSelect) == 0 ? Bits.One : Bits.Two; } }
public Parity ParityBit
{
get
{
var pen = lineControl & (uint)LineControl.ParityEnable;
if (pen == 0)
{
return Parity.None;
}
else
{
var eps = lineControl & (uint)LineControl.EvenParitySelect;
var sps = lineControl & (uint)LineControl.StickParitySelect;
if (eps == 0)
{
return sps == 0 ? Parity.Odd : Parity.Forced1;
}
else
{
return sps == 0 ? Parity.Even : Parity.Forced0;
}
}
}
}
public uint BaudRate
{
get
{
var divisor = (16 * ((integerBaudRate & 0xFFFF) + ((fractionalBaudRate & 0x1F) / 64)));
return (divisor > 0) ? (UARTClockFrequency / divisor) : 0;
}
}
public void Reset()
{
lock (UartLock)
{
control = flags = integerBaudRate = fractionalBaudRate = irdaLowPowerCounter = interruptMask = 0;
flags |= (uint)Flags.TransmitFifoEmpty;
flags |= (uint)Flags.ReceiveFifoEmpty;
control |= (uint)Control.TransmitEnable;
control |= (uint)Control.ReceiveEnable;
readFifo = new Queue<uint>(receiveFifoSize); // typed chars are stored here
}
}
public uint ReadDoubleWord(long offset)
{
lock (UartLock)
{
uint retVal;
switch ((Register)offset)
{
case Register.Data:
if (readFifo.Count == 0)
{
flags |= (uint)Flags.ReceiveFifoEmpty;
retVal = 0;
}
else
{
retVal = readFifo.Dequeue();
flags &= ~(uint)Flags.ReceiveFifoFull;
if (readFifo.Count == 0)
{
flags |= (uint)Flags.ReceiveFifoEmpty;
}
else
{
flags &= ~(uint)Flags.ReceiveFifoEmpty;
}
}
return retVal;
case Register.LineControl:
return lineControl;
case Register.Control:
return control;
default:
return idHelper.Read(offset);
}
}
}
public void WriteDoubleWord(long offset, uint value)
{
lock (UartLock)
{
switch ((Register)offset)
{
case Register.Data:
OnCharReceived((byte)value);
break;
case Register.Control:
control = value;
break;
default:
this.LogUnhandledWrite(offset, value);
break;
}
}
}
private void OnCharReceived(byte b)
{
Console.Write("{0:X}-", b);
if (complete == true)
{
length = (int)b;
Array.Resize(ref array, length);
complete = false;
}
else
{
array[index] = b;
index++;
if (index == length)
{
Console.WriteLine("\n************!!!END OF THE PACKET!!!!**********");
index = 0;
complete = true;
File.WriteAllBytes(@"/home/turpial-text.txt", array);
Console.WriteLine("\nHello TURPIAL FROM RENODE TO FILE\n");
executeCommand("/home/gustavosinbandera1/LOCHA/mi_make/examples/readfile /home/turpial-text.txt");
}
}
}
private void executeCommand(string command) {
Process proc = new System.Diagnostics.Process ();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
while (!proc.StandardOutput.EndOfStream) {
Console.WriteLine (proc.StandardOutput.ReadLine ());
}
}
private uint MaskedInterruptStatus
{
get { return rawInterruptStatus & interruptMask; }
}
private bool ReceiveEnabled
{
get
{
return (control & (uint)1u << 9) != 0;
}
}
private bool UartEnabled
{
get { return (control & (uint)1u) != 0; }
}
private bool LoopbackTesting
{
get { return (control & (uint)1u << 7) != 0; }
}
public static Queue q;
private object UartLock = new object();
private const uint UARTClockFrequency = 24000000;
private Queue<uint> readFifo;
private const int transmitFifoSize = 16;
private const int receiveFifoSize = 16;
private const int readFifoTriggerLevel = 1;
private uint flags;
private uint lineControl;
private uint control;
private uint dmaControl;
private uint interruptMask;
private uint rawInterruptStatus;
private uint irdaLowPowerCounter;
private uint integerBaudRate;
private uint fractionalBaudRate;
private uint interruptFIFOLevel;
private readonly int size;
private readonly PrimeCellIDHelper idHelper;
private readonly Machine machine;
private const uint UartEnable = 0x0001;
private const uint LoopbackEnable = 0x0080;
private const uint TxEnable = 0x0100;
private enum Register
{
Data = 0x000,
ReceiveStatus = 0x004, //aka ErrorClear
Flag = 0x018,
IrDALowPowerCounter = 0x020,
IntegerBaudRate = 0x024,
FractionalBaudRate = 0x028,
LineControl = 0x02c,
Control = 0x030,
InterruptFIFOLevel = 0x034,
InterruptMask = 0x038,
RawInterruptStatus = 0x03c,
MaskedInterruptStatus = 0x040,
InterruptClear = 0x044,
DMAControl = 0x048,
UARTPeriphID0 = 0xFE0,
UARTPeriphID1 = 0xFE4,
UARTPeriphID2 = 0xFE8,
UARTPeriphID3 = 0xFEC,
UARTPCellID0 = 0xFF0,
UARTPCellID1 = 0xFF4,
UARTPCellID2 = 0xFF8,
UARTPCellID3 = 0xFFC
}
[Flags]
private enum LineControl : uint
{
TwoStopBitsSelect = 1u << 3,
ParityEnable = 1u << 1,
EvenParitySelect = 1u << 2,
StickParitySelect = 1u << 7
}
[Flags]
private enum Flags : uint
{
TransmitFifoEmpty = 1u << 7,
ReceiveFifoEmpty = 1u << 4,
ReceiveFifoFull = 1u << 6
}
[Flags]
private enum Control : uint
{
TransmitEnable = 1u << 8,
ReceiveEnable = 1u << 9
}
}
public class RcvThread
{
private int i = 0;
// This is the lock for i, it must be an instance of any class.
// By convenience a new object is being used.
private object lock_i = new object();
Thread receiverThread;
public RcvThread()
{
// Creating our two threads. The ThreadStart delegate is points to
// the method being run in a new thread.
//receiverThread = new Thread(new ThreadStart(this.loop));
receiverThread = new Thread(this.loop);
Thread.Sleep(10);
}
public void Start(byte data)
{
receiverThread.Start(data);
}
public void Stop()
{
receiverThread.Abort();
}
public void loop(object data)
{
byte temp = (byte)data;
int length = (int)temp;
Console.WriteLine("el valor dle thread es ----> {0:D}", length);
do
{
if (PL012.q.Count > 0)
{
}
else
{
}
Thread.Sleep(1);
}
while (true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment