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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
//*****************************************************************************
//
// boot_demo_emac_flash.c - Ethernet boot loader example application.
//
// Copyright (c) 2013-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the DK-TM4C129X Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_flash.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_nvic.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/flash.h"
#include "driverlib/uart.h"
#include "driverlib/usb.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "utils/ustdlib.h"
#include "utils/lwiplib.h"
#include "utils/swupdate.h"
#include "grlib/grlib.h"
#include "grlib/widget.h"
#include "grlib/canvas.h"
#include "grlib/pushbutton.h"
#include "drivers/frame.h"
#include "drivers/kentec320x240x16_ssd2119.h"
#include "drivers/pinout.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Ethernet Boot Loader Demo (boot_demo_emac_flash)</h1>
//!
//! An example to demonstrate the use of remote update signaling with the
//! flash-based Ethernet boot loader. This application configures the Ethernet
//! controller and acquires an IP address which is displayed on the screen
//! along with the board's MAC address. It then listens for a ``magic packet''
//! telling it that a firmware upgrade request is being made and, when this
//! packet is received, transfers control into the boot loader to perform the
//! upgrade.
//!
//! This application is intended for use with flash-based ethernet boot
//! loader (boot_emac_flash).
//!
//! The link address for this application is set to 0x4000, the link address
//! has to be multiple of the flash erase block size(16KB=0x4000).
//! You may change this address to a 16KB boundary higher than the last
//! address occupied by the boot loader binary as long as you also rebuild the
//! boot loader itself after modifying its bl_config.h file to set
//! APP_START_ADDRESS to the same value.
//!
//! The boot_demo_flash application can be used along with this application to
//! easily demonstrate that the boot loader is actually updating the on-chip
//! flash.
//!
//*****************************************************************************
//*****************************************************************************
//
// The number of SysTick ticks per second.
//
//*****************************************************************************
#define TICKS_PER_SECOND 100
//*****************************************************************************
//
// A global flag used to indicate if a remote firmware update request has been
// received.
//
//*****************************************************************************
static volatile bool g_bFirmwareUpdate = false;
//*****************************************************************************
//
// Buffers used to hold the Ethernet MAC and IP addresses for the board.
//
//*****************************************************************************
#define SIZE_MAC_ADDR_BUFFER 32
#define SIZE_IP_ADDR_BUFFER 32
char g_pcMACAddr[SIZE_MAC_ADDR_BUFFER];
char g_pcIPAddr[SIZE_IP_ADDR_BUFFER];
//*****************************************************************************
//
// Forward reference to various widget structures.
//
//*****************************************************************************
extern tCanvasWidget g_sBackground;
//*****************************************************************************
//
// The canvas widget used to display the board's Ethernet IP address.
//
//*****************************************************************************
Canvas(g_sIPAddr, &g_sBackground, 0, 0,
&g_sKentec320x240x16_SSD2119, 10, 180, 300, 20,
(CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT),
ClrBlack, 0, ClrWhite, g_psFontCmss18b, g_pcIPAddr, 0, 0);
//*****************************************************************************
//
// The canvas widget used to display the board's Ethernet MAC address. This
// is required if using the Ethernet boot loader.
//
//*****************************************************************************
Canvas(g_sMACAddr, &g_sBackground, &g_sIPAddr, 0,
&g_sKentec320x240x16_SSD2119, 10, 200, 300, 20,
(CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT),
ClrBlack, 0, ClrWhite, g_psFontCmss18b, g_pcMACAddr, 0, 0);
//*****************************************************************************
//
// The canvas widget used to display the current status.
//
//*****************************************************************************
Canvas(g_sStatus, &g_sBackground, &g_sMACAddr, 0,
&g_sKentec320x240x16_SSD2119, 60, 110, 200, 40,
(CANVAS_STYLE_FILL | CANVAS_STYLE_TEXT),
ClrBlack, 0, ClrWhite, g_psFontCmss22b, "Waiting for signal.", 0, 0);
//*****************************************************************************
//
// The canvas widget acting as the background to the display.
//
//*****************************************************************************
Canvas(g_sBackground, WIDGET_ROOT, 0, &g_sStatus,
&g_sKentec320x240x16_SSD2119, 10, 23, 300, (240 - 33),
CANVAS_STYLE_FILL, ClrBlack, 0, 0, 0, 0, 0, 0);
//*****************************************************************************
//
// The system clock frequency.
//
//*****************************************************************************
uint32_t g_ui32SysClock;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
//*****************************************************************************
//
// This is the handler for this SysTick interrupt. We use this to provide the
// required timer call to the lwIP stack.
//
//*****************************************************************************
void
SysTickHandler(void)
{
//
// Call the lwIP timer.
//
lwIPTimer(1000 / TICKS_PER_SECOND);
}
//*****************************************************************************
//
// This function is called by the software update module whenever a remote
// host requests to update the firmware on this board. We set a flag that
// will cause the main loop to exit and transfer control to the bootloader.
//
// IMPORTANT:
// Note that this callback is made in interrupt context and, since it is not
// permitted to transfer control to the boot loader from within an interrupt,
// we can't just call SoftwareUpdateBegin() here.
//
//*****************************************************************************
void SoftwareUpdateRequestCallback(void)
{
g_bFirmwareUpdate = true;
}
//*****************************************************************************
//
// Perform the initialization steps required to start up the Ethernet controller
// and lwIP stack.
//
//*****************************************************************************
void
SetupForEthernet(void)
{
uint32_t ui32User0, ui32User1;
uint8_t pui8MACAddr[6];
//
// Configure SysTick for a 100Hz interrupt.
//
ROM_SysTickPeriodSet(g_ui32SysClock / TICKS_PER_SECOND);
ROM_SysTickEnable();
ROM_SysTickIntEnable();
//
// Get the MAC address from the UART0 and UART1 registers in NV ram.
//
ROM_FlashUserGet(&ui32User0, &ui32User1);
//
// Convert the 24/24 split MAC address from NV ram into a MAC address
// array.
//
pui8MACAddr[0] = ui32User0 & 0xff;
pui8MACAddr[1] = (ui32User0 >> 8) & 0xff;
pui8MACAddr[2] = (ui32User0 >> 16) & 0xff;
pui8MACAddr[3] = ui32User1 & 0xff;
pui8MACAddr[4] = (ui32User1 >> 8) & 0xff;
pui8MACAddr[5] = (ui32User1 >> 16) & 0xff;
//
// Format this address into the string used by the relevant widget.
//
usnprintf(g_pcMACAddr, SIZE_MAC_ADDR_BUFFER,
"MAC: %02X-%02X-%02X-%02X-%02X-%02X",
pui8MACAddr[0], pui8MACAddr[1], pui8MACAddr[2], pui8MACAddr[3],
pui8MACAddr[4], pui8MACAddr[5]);
//
// Remember that we don't have an IP address yet.
//
usnprintf(g_pcIPAddr, SIZE_IP_ADDR_BUFFER, "IP: Not assigned");
//
// Initialize the lwIP TCP/IP stack.
//
lwIPInit(g_ui32SysClock, pui8MACAddr, 0, 0, 0, IPADDR_USE_DHCP);
//
// Start the remote software update module.
//
SoftwareUpdateInit(SoftwareUpdateRequestCallback);
}
//*****************************************************************************
//
// Initialize UART0 and set the appropriate communication parameters.
//
//*****************************************************************************
void
SetupForUART(void)
{
//
// Enable UART0 peripheral.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure the UART for 115200, n, 8, 1
//
ROM_UARTConfigSetExpClk(UART0_BASE, g_ui32SysClock, 115200,
(UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
UART_CONFIG_WLEN_8));
//
// Enable the UART operation.
//
ROM_UARTEnable(UART0_BASE);
}
//*****************************************************************************
//
// Enable the USB controller
//
//*****************************************************************************
void
SetupForUSB(void)
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
ROM_USBClockEnable(USB0_BASE, 8, USB_CLOCK_INTERNAL);
}
//*****************************************************************************
//
// Demonstrate the use of the boot loader.
//
//*****************************************************************************
int
main(void)
{
tContext sContext;
uint32_t ui32IPAddr;
//
// Run from the PLL at 120 MHz.
//
g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000);
//
// Configure the device pins.
//
PinoutSet();
//
// Initialize the display driver.
//
Kentec320x240x16_SSD2119Init(g_ui32SysClock);
//
// Initialize the graphics context.
//
GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);
//
// Draw the application frame.
//
FrameDraw(&sContext, "boot-demo-emac-flash");
//
// Initialize the peripherals that each of the boot loader flavors
// supports. Although this example is only intended for use with the
// Ethernet boot loader, we initialize the other two peripherals too just
// in case it is used with the USB or serial boot loaders.
//
SetupForUART();
SetupForEthernet();
SetupForUSB();
//
// Add the compile-time defined widgets to the widget tree.
//
WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sBackground);
//
// Paint the widget tree to make sure they all appear on the display.
//
WidgetPaint(WIDGET_ROOT);
//
// We don't have an IP address yet so clear the variable to tell us to
// check until we are assigned one.
//
ui32IPAddr = 0;
//
// Loop forever, processing widget messages.
//
while(!g_bFirmwareUpdate)
{
//
// Do we have an IP address yet? If not, check to see if we've been
// assigned one since the last time we checked.
//
if(ui32IPAddr == 0 || ui32IPAddr == 0xFFFFFFFF)
{
//
// What is our current IP address?
//
ui32IPAddr = lwIPLocalIPAddrGet();
//
// If it's non zero, update the display.
//
if(ui32IPAddr!= 0 && ui32IPAddr != 0xFFFFFFFF)
{
usprintf(g_pcIPAddr, "IP: %d.%d.%d.%d",
ui32IPAddr & 0xff, (ui32IPAddr >> 8) & 0xff,
(ui32IPAddr >> 16) & 0xff, ui32IPAddr >> 24);
WidgetPaint((tWidget *)&g_sIPAddr);
}
}
//
// Process any messages from or for the widgets.
//
WidgetMessageQueueProcess();
}
//
// If we drop out, the user has sent "Magic Package" via LMFlash
// so we tidy up and transfer control to the boot loader.
//
//
// Tell the user that we got their instruction.
//
CanvasTextSet(&g_sStatus, "Updating...");
WidgetPaint((tWidget *)&g_sStatus);
//
// Process all remaining messages on the queue (including the paint message
// we just posted).
//
WidgetMessageQueueProcess();
//
// Transfer control to the bootloader.
//
SoftwareUpdateBegin(g_ui32SysClock);
//
// The previous function never returns but we need to stick in a return
// code here to keep the compiler from generating a warning.
//
return(0);
}
|