1 // 2 // Copyright (c) 2010-2024 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System; 8 using Antmicro.Renode.Core; 9 using Antmicro.Renode.Core.CAN; 10 using Antmicro.Renode.Logging; 11 using Antmicro.Renode.Peripherals.UART; 12 13 namespace Antmicro.Renode.Peripherals.CAN 14 { 15 public class CANToUART : UARTBase, ICAN 16 { CANToUART(IMachine machine, uint rxFromCANId, uint txToCANId)17 public CANToUART(IMachine machine, uint rxFromCANId, uint txToCANId) : base(machine) 18 { 19 this.rxFromCANId = rxFromCANId; 20 this.txToCANId = txToCANId; 21 } 22 OnFrameReceived(CANMessageFrame message)23 public void OnFrameReceived(CANMessageFrame message) 24 { 25 if(message.Id == rxFromCANId) 26 { 27 this.NoisyLog("Received {0} bytes [{1}] on id 0x{2:X}", message.Data.Length, message.DataAsHex, message.Id); 28 foreach(var character in message.Data) 29 { 30 TransmitCharacter(character); 31 } 32 } 33 else 34 { 35 this.DebugLog("Received an unexpected message on id 0x{0:X} and length {1}!", message.Id, message.Data.Length); 36 } 37 } 38 WriteChar(byte value)39 public override void WriteChar(byte value) 40 { 41 var fs = FrameSent; 42 if(fs == null) 43 { 44 return; 45 } 46 this.NoisyLog("Transmitting byte 0x{0:X2}", value); 47 fs(new CANMessageFrame(txToCANId, new byte[] { value })); 48 } 49 50 public event Action<CANMessageFrame> FrameSent; 51 52 public override Bits StopBits => Bits.One; 53 public override Parity ParityBit => Parity.None; 54 public override uint BaudRate => 115200; 55 CharWritten()56 protected override void CharWritten() 57 { 58 // Intentionally left blank 59 } 60 QueueEmptied()61 protected override void QueueEmptied() 62 { 63 // Intentionally left blank 64 } 65 66 private readonly uint rxFromCANId; 67 private readonly uint txToCANId; 68 } 69 } 70