1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using Antmicro.Renode.Core;
9 using Antmicro.Renode.Peripherals.Bus;
10 using Antmicro.Renode.Logging;
11 
12 namespace Antmicro.Renode.Peripherals.Timers
13 {
14     public sealed class CC2538SleepTimer : ComparingTimer, IDoubleWordPeripheral, IKnownSize
15     {
CC2538SleepTimer(IMachine machine)16         public CC2538SleepTimer(IMachine machine) : base(machine.ClockSource, 32768, compare: uint.MaxValue, limit: uint.MaxValue, enabled: true)
17         {
18             IRQ = new GPIO();
19         }
20 
ReadDoubleWord(long offset)21         public uint ReadDoubleWord(long offset)
22         {
23             switch((Registers)offset)
24             {
25             case Registers.CountAndCompare0:
26                 lastValue = (uint)Value;
27                 return (uint)(byte)lastValue;
28             case Registers.CountAndCompare1:
29                 return (uint)(byte)(lastValue >> 8);
30             case Registers.CountAndCompare2:
31                 return (uint)(byte)(lastValue >> 16);
32             case Registers.CountAndCompare3:
33                 return (uint)(byte)(lastValue >> 24);
34             case Registers.LoadStatus:
35                 return 1u; // we're always ready and have the value already loaded
36             default:
37                 this.LogUnhandledRead(offset);
38                 return 0;
39             }
40         }
41 
WriteDoubleWord(long offset, uint value)42         public void WriteDoubleWord(long offset, uint value)
43         {
44             switch((Registers)offset)
45             {
46             case Registers.CountAndCompare0:
47                 lastCompare &= 0xFFFFFF00;
48                 lastCompare |= value;
49                 Compare = lastCompare;
50                 break;
51             case Registers.CountAndCompare1:
52                 lastCompare &= 0xFFFF00FF;
53                 lastCompare |= (value << 8);
54                 break;
55             case Registers.CountAndCompare2:
56                 lastCompare &= 0xFF00FFFF;
57                 lastCompare |= (value << 16);
58                 break;
59             case Registers.CountAndCompare3:
60                 lastCompare &= 0x00FFFFFF;
61                 lastCompare |= (value << 24);
62                 break;
63             default:
64                 this.LogUnhandledWrite(offset, value);
65                 break;
66             }
67         }
68 
Reset()69         public override void Reset()
70         {
71             IRQ.Unset();
72             base.Reset();
73             lastValue = 0;
74             lastCompare = 0;
75         }
76 
77         public long Size
78         {
79             get
80             {
81                 return 0x30;
82             }
83         }
84 
85         public GPIO IRQ { get; private set; }
86 
OnCompareReached()87         protected override void OnCompareReached()
88         {
89             IRQ.Set();
90             IRQ.Unset();
91         }
92 
93         private uint lastValue;
94         private uint lastCompare;
95 
96         private enum Registers
97         {
98             CountAndCompare0 = 0x00,
99             CountAndCompare1 = 0x04,
100             CountAndCompare2 = 0x08,
101             CountAndCompare3 = 0x0C,
102             LoadStatus       = 0x10,
103             CaptureControl   = 0x14,
104             CaptureStatus    = 0x18,
105             CaptureValue0    = 0x1C,
106             CaptureValue1    = 0x20,
107             CaptureValue2    = 0x24,
108             CaptureValue3    = 0x28
109         }
110     }
111 }
112