1 //
2 // Copyright (c) 2010-2020 LabMICRO FACET UNT
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 NUnit.Framework;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Peripherals.GPIOPort;
11 using Antmicro.Renode.Peripherals.Bus;
12 
13 namespace Antmicro.Renode.PeripheralsTests
14 {
15     [TestFixture]
16     public class LPC43xx_GPIO_Test
17     {
18         [Test]
InitTest()19         public void InitTest()
20         {
21             var machine = new Machine();
22             var gpio = new LPC43xx_GPIO(machine);
23             machine.SystemBus.Register(gpio, new BusRangeRegistration(0x4000A000, 0x400));
24 
25             // Given a just reseted gpio port
26             gpio.Reset();
27 
28             for(var port = 0; port < 8; port ++)
29             {
30                 // Then GPIO_DIR should have a value equal to cero
31                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_DIR + 4 * port), 0x00000000);
32                 // And GPIO_MASK should have a value equal to cero
33                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MASK + 4 * port), 0x00000000);
34                 // And GPIO_SET should have a value equal to cero
35                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_SET + 4 * port), 0x00000000);
36             }
37         }
38 
39         [Test]
ChangeDirectionToOutput()40         public void ChangeDirectionToOutput()
41         {
42             var machine = new Machine();
43             var gpio = new LPC43xx_GPIO(machine);
44             machine.SystemBus.Register(gpio, new BusRangeRegistration(0x4000A000, 0x400));
45 
46             // Given a just reseted gpio port
47             gpio.Reset();
48 
49             for(var port = 0; port < 8; port++)
50             {
51                 // When GPIO_DIR it's written to set all pins as outputs
52                 gpio.WriteDoubleWord(GPIO_DIR + 4 * port, 0xFFFFFFFF);
53                 // Then GPIO_DIR should confirm that all pins are outpus
54                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_DIR + 4 * port), 0xFFFFFFFF);
55                 // And GPIO_MASK should have retained the reset value
56                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MASK + 4 * port), 0x00000000);
57                 // And GPIO_PIN should have retained the reset value
58                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
59                 // And  GPIO_MPIN should have retained the reset value
60                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MPIN + 4 * port), 0x00000000);
61                 // And  GPIO_SET should have retained the reset value
62                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_SET + 4 * port), 0x00000000);
63             }
64         }
65 
66         [Test]
ChangeOutputsState()67         public void ChangeOutputsState()
68         {
69             var machine = new Machine();
70             var gpio = new LPC43xx_GPIO(machine);
71             machine.SystemBus.Register(gpio, new BusRangeRegistration(0x4000A000, 0x400));
72 
73             gpio.Reset();
74             for(var port = 0; port < 8; port++)
75             {
76                 // Given a gpio port with all pins configured as outputs
77                 gpio.WriteDoubleWord(GPIO_DIR + 4 * port, 0xFFFFFFFF);
78 
79                 // When GPIO_PIN are writed to set all outpus pins to high state
80                 gpio.WriteDoubleWord(GPIO_PIN + 4 * port, 0xFFFFFFFF);
81                 // Then GPIO_PIN should have the current value of the outputs
82                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0xFFFFFFFF);
83                 // And GPIO_SET should have the current value of the outputs
84                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_SET + 4 * port), 0xFFFFFFFF);
85 
86                 // When GPIO_PIN are writed to set all outpus pins to low state
87                 gpio.WriteDoubleWord(GPIO_PIN + 4 * port, 0x00000000);
88                 // Then GPIO_PIN should have the current value of the outputs
89                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
90                 // And GPIO_SET should have the current value of the outputs
91                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_SET + 4 * port), 0x00000000);
92 
93                 // When GPIO_SET are writed to set all outpus pins to high state
94                 gpio.WriteDoubleWord(GPIO_SET + 4 * port, 0xFFFFFFFF);
95                 // Then GPIO_PIN should have the current value of the outputs
96                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0xFFFFFFFF);
97 
98                 // When GPIO_SET are writed to not produce changes in the outputs
99                 gpio.WriteDoubleWord(GPIO_SET + 4 * port, 0x00000000);
100                 // Then GPIO_PIN should have retained the previous value of the outputs
101                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0xFFFFFFFF);
102 
103                 // When GPIO_CLR are writed to set all outpus pins to low state
104                 gpio.WriteDoubleWord(GPIO_CLR + 4 * port, 0xFFFFFFFF);
105                 // Then GPIO_PIN should have the current value of the outputs
106                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
107 
108                 // When GPIO_SET are writed to not produce changes in the outputs
109                 gpio.WriteDoubleWord(GPIO_CLR + 4 * port, 0x00000000);
110                 // Then GPIO_PIN should have retained the previous value of the outputs
111                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
112 
113                 // When GPIO_NOT are writed to change all outpus pins
114                 gpio.WriteDoubleWord(GPIO_NOT + 4 * port, 0xFFFFFFFF);
115                 // Then GPIO_PIN should have the current value of the outputs
116                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0xFFFFFFFF);
117 
118                 // When GPIO_NOT are writed to change all outpus pins
119                 gpio.WriteDoubleWord(GPIO_NOT + 4 * port, 0xFFFFFFFF);
120                 // Then GPIO_PIN should have the current value of the outputs
121                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
122 
123                 // When GPIO_NOT are writed to to not produce changes in the outputs
124                 gpio.WriteDoubleWord(GPIO_NOT + 4 * port, 0x00000000);
125                 // Then GPIO_PIN should have retained the previous value of the outputs
126                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x00000000);
127             }
128         }
129 
130         [Test]
ChangeMask()131         public void ChangeMask()
132         {
133             var machine = new Machine();
134             var gpio = new LPC43xx_GPIO(machine);
135             machine.SystemBus.Register(gpio, new BusRangeRegistration(0x4000A000, 0x400));
136 
137             gpio.Reset();
138             for(var port = 0; port < 8; port++)
139             {
140                 // When GPIO_MASK it's written to set filter out all pins
141                 gpio.WriteDoubleWord(GPIO_MASK + 4 * port, 0xFFFFFFFF);
142                 // Then GPIO_MASK should confirm that all pins are filtered out
143                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MASK + 4 * port), 0xFFFFFFFF);
144             }
145         }
146 
147         [Test]
ChangeMaskedOuputState()148         public void ChangeMaskedOuputState()
149         {
150             var machine = new Machine();
151             var gpio = new LPC43xx_GPIO(machine);
152             machine.SystemBus.Register(gpio, new BusRangeRegistration(0x4000A000, 0x400));
153 
154             gpio.Reset();
155             for(var port = 0; port < 8; port++)
156             {
157                 // Given a gpio port with all pins configured as outputs
158                 gpio.WriteDoubleWord(GPIO_DIR + 4 * port, 0xFFFFFFFF);
159                 // And a gpio port with all pins setted to high state
160                 gpio.WriteDoubleWord(GPIO_PIN + 4 * port, 0xFFFFFFFF);
161 
162                 // When GPIO_MASK it's written to set filter out lower half pins
163                 gpio.WriteDoubleWord(GPIO_MASK + 4 * port, 0x0000FFFF);
164                 // Then GPIO_MPORT should return filtered pins in low and not filtered pins in high
165                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MPIN + 4 * port), 0xFFFF0000);
166 
167                 // When GPIO_MPORT it's written to set filter pins to low
168                 gpio.WriteDoubleWord(GPIO_MPIN + 4 * port, 0x00000000);
169                 // Then GPIO_PORT should return not filtered pins in low and filtered pins in high
170                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_PIN + 4 * port), 0x0000FFFF);
171 
172                 // When GPIO_MASK it's written to set filter out upper half pins
173                 gpio.WriteDoubleWord(GPIO_MASK + 4 * port, 0xFFFF0000);
174                 // Then GPIO_MPORT should return filtered pins in low and not filtered pins in high
175                 Assert.AreEqual(gpio.ReadDoubleWord(GPIO_MPIN + 4 * port), 0x0000FFFF);
176             }
177         }
178 
179         private const uint GPIO_DIR = 0x2000;
180         private const uint GPIO_MASK = 0x2080;
181         private const uint GPIO_PIN = 0x2100;
182         private const uint GPIO_MPIN = 0x2180;
183         private const uint GPIO_SET = 0x2200;
184         private const uint GPIO_CLR = 0x2280;
185         private const uint GPIO_NOT = 0x2300;
186 
187     }
188 }
189 
190