22
22
#include < unistd.h>
23
23
#include < log.h>
24
24
#include < gpdevboard.h>
25
- #ifndef _WIN32
26
- #include < termios.h>
27
- #else
28
- #include < conio.h>
29
- #endif
25
+ #include < Greenpak4.h>
30
26
31
27
using namespace std ;
32
28
29
+ hdevice InitializeHardware (unsigned int nboard, SilegoPart expectedPart);
30
+ bool DoInitializeHardware (hdevice hdev, SilegoPart expectedPart);
31
+ bool PostProgramSetup (hdevice hdev);
32
+ bool IOReset (hdevice hdev);
33
+ bool IOSetup (hdevice hdev);
34
+ bool PowerSetup (hdevice hdev);
35
+
33
36
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34
37
// Entry point
35
38
@@ -69,35 +72,113 @@ int main(int argc, char* argv[])
69
72
// Set up logging
70
73
g_log_sinks.emplace (g_log_sinks.begin (), new STDLogSink (console_verbosity));
71
74
72
- // Print status message
75
+ // Hardware configuration
76
+ Greenpak4Device::GREENPAK4_PART part = Greenpak4Device::GREENPAK4_SLG46620;
77
+ SilegoPart spart = SilegoPart::SLG46620V;
78
+ Greenpak4IOB::PullDirection unused_pull = Greenpak4IOB::PULL_DOWN;
79
+ Greenpak4IOB::PullStrength unused_drive = Greenpak4IOB::PULL_1M;
80
+
81
+ // Print status message and set up the board
73
82
LogNotice (" GreenPAK timing characterization helper\n " );
74
- LogNotice (" Right now, just enables pins used by pmod-gpdevboard and sets up IO\n " );
83
+ hdevice hdev = InitializeHardware (nboard, spart);
84
+ if (!hdev)
85
+ return 1 ;
86
+
87
+ // Create the device object
88
+ Greenpak4Device device (part, unused_pull, unused_drive);
89
+ device.SetIOPrecharge (false );
90
+ device.SetDisableChargePump (false );
91
+ device.SetLDOBypass (false );
92
+ device.SetNVMRetryCount (1 );
93
+
94
+ // Configure pin 5 as an output and drive it high
95
+ auto iob = device.GetIOB (5 );
96
+ auto vdd = device.GetPower ();
97
+ iob->SetInput (" IN" , vdd);
98
+ iob->SetDriveType (Greenpak4IOB::DRIVE_PUSHPULL);
99
+ iob->SetDriveStrength (Greenpak4IOB::DRIVE_1X);
100
+
101
+ // Generate a bitstream
102
+ vector<uint8_t > bitstream;
103
+ device.WriteToBuffer (bitstream, 0 , false );
104
+ device.WriteToFile (" /tmp/test.txt" , 0 , false );
105
+
106
+ // Emulate the device
107
+ LogVerbose (" Loading new bitstream\n " );
108
+ if (!DownloadBitstream (hdev, bitstream, DownloadMode::EMULATION))
109
+ return 1 ;
110
+ if (!PostProgramSetup (hdev))
111
+ return 1 ;
112
+
113
+ // Wait a bit
114
+ usleep (5000 * 1000 );
115
+
116
+ // Done
117
+ LogNotice (" Done, resetting board\n " );
118
+ SetStatusLED (hdev, 0 );
119
+ Reset (hdev);
120
+ USBCleanup (hdev);
121
+
122
+ return 0 ;
123
+ }
124
+
125
+ // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126
+ // Board initialization
75
127
128
+ hdevice InitializeHardware (unsigned int nboard, SilegoPart expectedPart)
129
+ {
76
130
// Open the dev board
77
131
hdevice hdev = OpenBoard (nboard);
78
132
if (!hdev)
79
- return 1 ;
133
+ return NULL ;
80
134
81
- // Light up the status LED
82
- if (!SetStatusLED (hdev, 1 ))
83
- return 1 ;
135
+ // Light it up so we know it's busy
136
+ SetStatusLED (hdev, 1 );
84
137
85
- // DO NOT do anything that might make the IO voltage change, or send Vpp!
138
+ // Set it up
139
+ if (!DoInitializeHardware (hdev, expectedPart))
140
+ {
141
+ SetStatusLED (hdev, 0 );
142
+ Reset (hdev);
143
+ USBCleanup (hdev);
144
+ return NULL ;
145
+ }
86
146
87
- // Reset all signal generators we may have used during setup
88
- if (!ResetAllSiggens (hdev))
89
- return 1 ;
147
+ return hdev;
148
+ }
149
+
150
+ bool PostProgramSetup (hdevice hdev)
151
+ {
152
+ // Clear I/Os from programming mode
153
+ if (!IOReset (hdev))
154
+ return false ;
155
+
156
+ // Load the new configuration
157
+ if (!IOSetup (hdev))
158
+ return false ;
159
+ if (!PowerSetup (hdev))
160
+ return false ;
161
+
162
+ return true ;
163
+ }
90
164
91
- // Reset I/O config
165
+ bool IOReset (hdevice hdev)
166
+ {
92
167
IOConfig ioConfig;
93
168
for (size_t i = 2 ; i <= 20 ; i++)
94
169
ioConfig.driverConfigs [i] = TP_RESET;
95
170
if (!SetIOConfig (hdev, ioConfig))
96
171
return false ;
97
172
173
+ return true ;
174
+ }
175
+
176
+ bool IOSetup (hdevice hdev)
177
+ {
178
+ // Enable the I/O pins
98
179
unsigned int pins[] =
99
180
{
100
- 2 , 3 , 4 , 5 , 12 , 13 , 14 , 15
181
+ 3 , 4 , 5 , 12 , 13 , 14 , 15
101
182
};
102
183
103
184
// Configure I/O voltage
@@ -109,36 +190,44 @@ int main(int argc, char* argv[])
109
190
config.expansionEnabled [npin] = true ;
110
191
}
111
192
if (!SetIOConfig (hdev, config))
112
- return 1 ;
193
+ return false ;
194
+
195
+ return true ;
196
+ }
113
197
198
+ bool PowerSetup (hdevice hdev)
199
+ {
114
200
// Do not change this voltage! This is what the FPGA uses
201
+ // TODO: sweep from 3.15 to 3.45 to do voltage corner analysis
115
202
float voltage = 3.3 ;
116
- if (! ConfigureSiggen (hdev, 1 , voltage))
117
- return 1 ;
203
+ return ConfigureSiggen (hdev, 1 , voltage);
204
+ }
118
205
119
- // Hold the lock until something happens
120
- bool lock = true ;
121
- if (lock)
206
+ bool DoInitializeHardware (hdevice hdev, SilegoPart expectedPart)
207
+ {
208
+ // Figure out what's there
209
+ SilegoPart detectedPart = SilegoPart::UNRECOGNIZED;
210
+ vector<uint8_t > programmedBitstream;
211
+ BitstreamKind bitstreamKind;
212
+ if (!DetectPart (hdev, detectedPart, programmedBitstream, bitstreamKind))
213
+ return false ;
214
+
215
+ // Complain if we got something funny
216
+ if (expectedPart != detectedPart)
122
217
{
123
- LogNotice (" Holding lock on board, press any key to exit...\n " );
124
- SetStatusLED (hdev, 1 );
125
-
126
- #ifndef _WIN32
127
- struct termios oldt, newt;
128
- tcgetattr ( STDIN_FILENO, &oldt );
129
- newt = oldt;
130
- newt.c_lflag &= ~( ICANON | ECHO );
131
- tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
132
- getchar ();
133
- tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
134
- #else
135
- _getch ();
136
- #endif
218
+ LogError (" Detected wrong part (not what we're trying to characterize)\n " );
219
+ return false ;
137
220
}
138
221
139
- // Done
140
- LogNotice (" Done, resetting board\n " );
141
- SetStatusLED (hdev, 0 );
142
- Reset (hdev);
143
- USBCleanup (hdev);
222
+ // Get the board into a known state
223
+ if (!ResetAllSiggens (hdev))
224
+ return false ;
225
+ if (!IOReset (hdev))
226
+ return false ;
227
+ if (!IOSetup (hdev))
228
+ return false ;
229
+ if (!PowerSetup (hdev))
230
+ return false ;
231
+
232
+ return true ;
144
233
}
0 commit comments