This repository was archived by the owner on Apr 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
398 lines (328 loc) · 14.4 KB
/
init.c
File metadata and controls
398 lines (328 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
////////////////////////////////////////////////////////////////////////////////
/// @file
/// @brief Initialization.
////////////////////////////////////////////////////////////////////////////////
// *****************************************************************************
// ************************** System Include Files *****************************
// *****************************************************************************
#include <sys/attribs.h>
// *****************************************************************************
// ************************** User Include Files *******************************
// *****************************************************************************
#include "tcpip/tcpip.h"
#include "ksz8895.h"
#include "init.h"
#include "can.h"
#include "uart.h"
// *****************************************************************************
// ************************** Macros *******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Defines ******************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Definitions **************************************
// *****************************************************************************
// *****************************************************************************
// ************************** Function Prototypes ******************************
// *****************************************************************************
static void InitCPU( void );
static void InitGPIO( void );
static void InitTMR( void );
static void InitSPI( void );
static void InitI2C( void );
static void InitINT( void );
static void InitWDT( void );
static void InitTCPIPStack( void );
static void InitADC( void );
// *****************************************************************************
// ************************** Global Functions *********************************
// *****************************************************************************
void InitBoard()
{
// Initialize microcontroller peripherals.
InitCPU();
InitGPIO();
InitTMR();
InitSPI();
InitINT();
UARTInit();
CANInit();
InitADC();
InitI2C();
// Initialize software libraries.
InitTCPIPStack();
// Initialize watchdog timer.
InitWDT();
}
// *****************************************************************************
// ************************** Static Functions *********************************
// *****************************************************************************
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize internal CPU operation.
///
/// This function initialize internal CPU operation.
////////////////////////////////////////////////////////////////////////////////
static void InitCPU( void )
{
switch (DEVIDbits.VER)
{
case 0x0:
case 0x1:
case 0x2:
case 0x3:
{
// NOTE: See errata #43: CPU: Constant Data Access from Flash
break;
}
case 0x4:
case 0x5:
default:
{
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize General-Purpose Input/Output (GPIO) operation.
///
/// The function configures GPIO for correct electrical operation and
/// default/initial value.
////////////////////////////////////////////////////////////////////////////////
static void InitGPIO( void )
{
// Disable JTAG port.
DDPCONbits.JTAGEN = 0;
// Red Status LED (RF3)
TRISFbits.TRISF3 = 0; // Output
ODCFbits.ODCF3 = 1; // Open Drain
LATFbits.LATF3 = 1; // Off
// SPI2 SS (RG9)
TRISGbits.TRISG9 = 0; // Output
ODCGbits.ODCG9 = 0; // Normal
LATGbits.LATG9 = 1; // High
//-----------------------------------------------------
// Software SPI Bit-bang CLK (RB6)
AD1PCFGbits.PCFG6 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB6 = 0; // Normal
TRISBbits.TRISB6 = 0; // Output
LATBbits.LATB6 = 0; // Logic Low
// Software SPI Bit-bang MOSI (RB9)
AD1PCFGbits.PCFG9 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB9 = 0; // Normal
TRISBbits.TRISB9 = 0; // Output
LATBbits.LATB9 = 0; // Logic Low
// Software SPI Bit-bang MISO (RB10)
AD1PCFGbits.PCFG10 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB10 = 0; // Normal
TRISBbits.TRISB10 = 1; // Input
// Software SPI Bit-bang !SS for Ethernet Switch (RB7)
AD1PCFGbits.PCFG7 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB7 = 0; // Normal
TRISBbits.TRISB7 = 0; // Output
LATBbits.LATB7 = 1; // Logic High
//-----------------------------------------------------
// Microhard Nano !CONFIG (RC13)
TRISCSET = _TRISC_TRISC13_MASK; // Input (see microhard source module)
// Microhard Nano Enable (RC15)
TRISCCLR = _TRISC_TRISC15_MASK; // Set as discrete output.
ODCCSET = _ODCC_ODCC15_MASK; // Set as CMOS drivers (i.e. not open-drain).
LATCCLR = _LATC_LATC15_MASK; // Set the output low (0).
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize timer operation.
///
/// This function initializes timers used for tracking core time and those
/// for bit-banging interfaces.
////////////////////////////////////////////////////////////////////////////////
static void InitTMR( void )
{
// Core Timer
IPC0bits.CTIP = 7; // Set core timer interrupt priority.
IPC0bits.CTIS = 0; // Set core timer interrupt subpriority.
IFS0CLR = _IFS0_CTIF_MASK; // Clear core timer interrupt flag.
IEC0SET = _IEC0_CTIE_MASK; // Enable CT interrupts.
// Write to compare register for the core-timer to maximum value so the
// core-timer will serve as a continuous roll-over counter.
_mtc0(_CP0_COMPARE, _CP0_COMPARE_SELECT, 0xFFFFFFFF);
// Initialize Timer 4 (software I2C clock)
ConfigIntTimer4(T4_INT_OFF | T4_INT_PRIOR_6 | T4_INT_SUB_PRIOR_0);
OpenTimer4(T4_ON | T4_IDLE_CON | T4_PS_1_1, 800); // 100 kHz
// Timer 5 (software SPI clock)
ConfigIntTimer5(T5_INT_OFF | T5_INT_PRIOR_7 | T5_INT_SUB_PRIOR_0);
OpenTimer5(T5_ON | T5_IDLE_CON | T5_PS_1_1, 800); // 100 kHz
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize SPI operation.
///
/// Hardware registers are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitSPI( void )
{
// SPI2
SPI2CONCLR = 0xFFFFFFFF;
SPI2CONSET = _SPI2CON_CKP_MASK; // Clock idles high.
SPI2CONSET = _SPI2CON_MSTEN_MASK; // Master mode.
SPI2CONSET = _SPI2CON_ENHBUF_MASK; // Enhanced buffer mode.
SPI2BRG = 15; // 2.5 MHz SPI clock.
SPI2CONbits.STXISEL = 0b10; // TX interrupt when buffer is empty
// by one-half or more.
SPI2CONbits.SRXISEL = 0b10; // RX interrupt when buffer is full
// by one-half or more.
IPC7bits.SPI2IP = 7; // Set SPI2 interrupt priority.
IPC7bits.SPI2IS = 0; // Set SPI2 interrupt subpriority.
IFS1CLR = _IFS1_SPI2EIF_MASK; // Clear SPI2 error interrupt flag.
IFS1CLR = _IFS1_SPI2RXIF_MASK; // Clear SPI2 RX interrupt flag.
IFS1CLR = _IFS1_SPI2TXIF_MASK; // Clear SPI2 TX interrupt flag.
IEC1CLR = _IEC1_SPI2EIE_MASK; // Disable SPI2 error interrupt.
IEC1CLR = _IEC1_SPI2RXIE_MASK; // Disable SPI2 RX interrupt.
IEC1CLR = _IEC1_SPI2TXIE_MASK; // Disable SPI2 TX interrupt.
SPI2CONSET = _SPI2CON_ON_MASK; // Enable SPI peripheral.
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize I2C operation.
///
/// Hardware registers are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitI2C( void )
{
// RB3 = SMBCLK
AD1PCFGbits.PCFG3 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB3 = 1; // Open Drain
LATBbits.LATB3 = 1; // Off
TRISBbits.TRISB3 = 0; // Output
// RB4 = SMBDAT
AD1PCFGbits.PCFG4 = 1; // Analog input pin in digital mode.
ODCBbits.ODCB4 = 1; // Open Drain
LATBbits.LATB4 = 1; // Off
TRISBbits.TRISB4 = 0; // Output
return;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize interrupt operation.
///
/// Hardware registers are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitINT( void )
{
INTCONSET = _INTCON_MVEC_MASK; // Enable multi-vectored mode.
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize Watchdog Timer (WDT) operation.
///
/// Hardware registers are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitWDT( void )
{
if ((RCONbits.POR == 1) && (RCONbits.BOR == 1))
{
// Initial power on detected. Clear status bits.
RCONCLR = _RCON_CMR_MASK;
RCONCLR = _RCON_EXTR_MASK;
RCONCLR = _RCON_WDTO_MASK;
RCONCLR = _RCON_BOR_MASK;
RCONCLR = _RCON_POR_MASK;
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize the TCPIP stack.
///
/// Hardware registers and software are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitTCPIPStack( void )
{
static ROM uint8_t SerializedMACAddress[6] =
{MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_BYTE2,
MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4,
MY_DEFAULT_MAC_BYTE5, MY_DEFAULT_MAC_BYTE6};
TickInit();
#if defined(STACK_USE_MPFS2)
MPFSInit();
#endif
// Initialize Stack and application related NV variables into AppConfig.
// Start out zeroing all AppConfig bytes to ensure all fields are
// deterministic for checksum generation
memset((void*)&AppConfig, 0x00, sizeof(AppConfig));
AppConfig.Flags.bIsDHCPEnabled = false;
AppConfig.Flags.bInConfigMode = true;
// Use default MAC address of the module
memcpypgm2ram((void*)&AppConfig.MyMACAddr,
(ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr));
AppConfig.MyIPAddr.Val =
MY_DEFAULT_IP_ADDR_BYTE1 |
MY_DEFAULT_IP_ADDR_BYTE2 << 8ul |
MY_DEFAULT_IP_ADDR_BYTE3 << 16ul |
MY_DEFAULT_IP_ADDR_BYTE4 << 24ul;
AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
AppConfig.MyMask.Val =
MY_DEFAULT_MASK_BYTE1 |
MY_DEFAULT_MASK_BYTE2 << 8ul |
MY_DEFAULT_MASK_BYTE3 << 16ul |
MY_DEFAULT_MASK_BYTE4 << 24ul;
AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
AppConfig.MyGateway.Val =
MY_DEFAULT_GATE_BYTE1 |
MY_DEFAULT_GATE_BYTE2 << 8ul |
MY_DEFAULT_GATE_BYTE3 << 16ul |
MY_DEFAULT_GATE_BYTE4 << 24ul;
AppConfig.PrimaryDNSServer.Val =
MY_DEFAULT_PRIMARY_DNS_BYTE1 |
MY_DEFAULT_PRIMARY_DNS_BYTE2 << 8ul |
MY_DEFAULT_PRIMARY_DNS_BYTE3 << 16ul |
MY_DEFAULT_PRIMARY_DNS_BYTE4 << 24ul;
AppConfig.SecondaryDNSServer.Val =
MY_DEFAULT_SECONDARY_DNS_BYTE1 |
MY_DEFAULT_SECONDARY_DNS_BYTE2 << 8ul |
MY_DEFAULT_SECONDARY_DNS_BYTE3 << 16ul |
MY_DEFAULT_SECONDARY_DNS_BYTE4 << 24ul;
// Load the default NetBIOS Host Name
memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*) MY_DEFAULT_HOST_NAME, 16);
FormatNetBIOSName(AppConfig.NetBIOSName);
// Initialize core stack layers (MAC, ARP, TCP, UDP) and
// application modules (HTTP, SNMP, etc.)
StackInit();
return;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief Initialize the ADC operation.
///
/// Hardware registers are initialized for required operation.
////////////////////////////////////////////////////////////////////////////////
static void InitADC( void )
{
// Configure the 10-bit Analog to Digital converter.
// AN5 = Battery Voltage
CloseADC10();
asm("nop");
asm("nop");
asm("nop");
// AD1CON1: ADC Control Register 1
AD1CON1bits.SIDL = 1; // Discontinue operation in Idle mode.
AD1CON1bits.FORM = 0b000; // 16-bit integer data output format.
AD1CON1bits.SSRC = 0b111; // Auto convert.
AD1CON1bits.CLRASAM = 0; // Normal operation
AD1CON1bits.ASAM = 0; // Sampling begins when SAMP bit is set.
AD1CON1bits.DONE = 0;
// AD1CON2: ADC Control Register 2
AD1CON2bits.VCFG = 0; // VR+ = AVdd, VR- = AVss
AD1CON2bits.OFFCAL = 0; // Disable Offset Calibration mode.
AD1CON2bits.CSCNA = 0; // Scan inputs.
AD1CON2bits.SMPI = 0b0000; // Interrupts for each sample/convert.
AD1CON2bits.BUFM = 0; // Buffer configured as one 16-word buffer.
AD1CON2bits.ALTS = 0; // Always use MUX A input mux settings.
// AD1CON3: ADC Control Register 3
AD1CON3bits.ADRC = 0; // Clock derived from Peripheral Bus Clock.
AD1CON3bits.SAMC = 0b11111; // Auto-sample Time = 31 TAD.
AD1CON3bits.ADCS = 0x0F; // ADC Conversion Clock = 32 * TPB = TAD.
// AD1CHS: ADC Input Select Register
AD1CHSbits.CH0NA = 0; // Channel 0 negative input is VR-.
AD1CHSbits.CH0SA = 5; // Channel 0 positive input is AN5.
// AD1PCFG: ADC Port Configuration Register
AD1PCFG = 0xFFDF; // All Analog input pins in Digital mode,
// except AN5.
// AD1CSSL: ADC Input Scan Select Register
AD1CSSL = 0;
EnableADC10();
DelayMs(1);
return;
}