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.Peripherals.Miscellaneous.S32K3XX_FlexIOModel;
9 
10 namespace Antmicro.Renode.Peripherals.Miscellaneous
11 {
12     public class UARTTransmitter : UARTDirectionBase
13     {
UARTTransmitter(IEmulationElement owner, Shifter shifter)14         public UARTTransmitter(IEmulationElement owner, Shifter shifter) : base(owner, shifter)
15         {
16             shifter.DataTransmitted += OnTransmission;
17         }
18 
19         public event Action<byte> CharReceived;
20 
LogSpecificWarnings()21         protected override void LogSpecificWarnings()
22         {
23             LogWarningNonEqual((uint)shifter.TimerPolarity, (uint)ShifterPolarity.OnPosedge, "timer polarity", shifter.Name);
24             if(shifter.Timer != null)
25             {
26                 LogWarningNonEqual((uint)shifter.Timer.Enable, (uint)TimerEnable.OnTriggerHigh, "enable configuration", shifter.Timer.Name);
27                 LogWarningNonEqual((uint)shifter.Timer.Disable, (uint)TimerDisable.OnTimerCompare, "disable configuration", shifter.Timer.Name);
28                 LogWarningNonEqual((uint)shifter.Timer.Output, (uint)TimerOutput.One, "output configuration", shifter.Timer.Name);
29                 LogWarningNonEqual((uint)shifter.Timer.ResetMode, (uint)TimerReset.Never, "reset configuration", shifter.Timer.Name);
30 
31                 LogWarningNonEqual((uint)shifter.Timer.TriggerPolarity, (uint)TimerTriggerPolarity.ActiveLow, "trigger polarity", shifter.Timer.Name);
32                 LogWarningNonEqual((uint)shifter.Timer.TriggerSource, (uint)TimerTriggerSource.Internal, "trigger source", shifter.Timer.Name);
33                 LogWarningNonEqual((uint)shifter.Timer.TriggerPolarity, (uint)TimerTriggerPolarity.ActiveLow, "trigger polarity", shifter.Timer.Name);
34                 LogWarningNonEqual(shifter.Timer.TriggerSelect, Timer.EncodeShifterAsTriggerSelect(shifter), "trigger select", shifter.Timer.Name);
35             }
36         }
37 
38         protected override string WarningPrefix => "Invalid configuration of transmitter: ";
39 
OnTransmission(uint value)40         private void OnTransmission(uint value)
41         {
42             // Ignore driver sending a frame with only ones (which is an idle state on bus).
43             if(value == uint.MaxValue && shifter.StartBit == ShifterStopBitConfiguration)
44             {
45                 return;
46             }
47 
48             LogWarnings();
49 
50             CharReceived?.Invoke((byte)value);
51             shifter.Status.SetFlag(true);
52             // Trigger timer expired (whole data frame sent).
53             shifter.Timer?.Status.SetFlag(true, true);
54         }
55     }
56 }
57