Create a Web-Enabled Digital Photo Frame: Step 3
Configuring the Components
Introduction to Configuration Options
Expanding and collapsing the Options
Each component has options. The Configuration Options should appear on the right side of the Project Graph.
To open the Configuration Options of a component, click on the component or an instance inside the component.
Some of the options are grouped and collapsed:
- Click on the + button to expand all configuration options.
- Click on the – button to collapse all settings.
Each group can also be expanded or collapsed using the + or - sign before the grouped options, as highlighted in the accompanying image.
Configuration settings color scheme
There is also a color scheme for each setting, and the colors are as follows:
- White: the setting has the default value.
- Green: the setting has a user-defined value.
- Light blue: the setting has been automatically set by the addition of a component and cannot be changed.
To revert to the default value, right-click on the green label, then select Clear User Setting.
MCU Clock
Internal FRC clock
The demonstration will use the default settings of the MCU: internal Fast RC Clock (FRC) with PLL at 200 MHz.
Setting the MCU to work with an External Clock
These are the instructions on how to set the clock to the existing 12 MHz External Clock on the Curiosity 2.0 Development Board (Y1).
Notes:
- This step is optional.
- In case the USB peripheral needs to be used, then this step becomes mandatory because the USB needs external clock to function.
- Using the EC at 12 MHz will result in a system clock speed set at 198 MHz because of PLL dividers and multipliers. Some of the accompanying screenshots will show 200 MHz (or 100 MHz clocks for reference clocks) because the FRC has been used.
To set up MCU Clocking, we will use the Clock Configuration plugin from the Project Graph.
Use the Maximize button to make the tool full-screen.
These are the changes to make. Please ignore the fact the values turn red:
- SPLLCLK: POSC
- Primary Oscillator: 12,000,000 Hz
- POSCMOD: EC
We have setup the oscillator, now the prescalers, multipliers, and dividers and other Fuse settings need to be setup, so that the MCU can start the PLL.
To do so, click on the button Auto-Calculate and hit Apply for 200,000,000 MHz system clock.
The system clock results will be 198 MHz because of the different multiplications and divisions of the input clock.
If you plan to use USB, please also set the UPLLFSEL for the USB PLL to 12 MHz
Restore the Clock Diagram window from the top right-hand corner.
CONSOLE Service
Configuration
The CONSOLE will receive commands from the terminal, which are usually short. It will display messages from the application, which are usually long enough. The buffers for various components will need to be increased from their default values.
Click on the CONSOLE and set the Console Print Buffer Size to 2048 bytes.
UART Peripheral
Configuration
Click on UART2 and on the right side you can see the configuration of it.
Set the TX Ring Buffer Size to a bigger value. The system will output many characters to the terminal.
The RX can stay small. This is what it is received from the terminal. Usually the commands are very small.
Pins
The UART 2 peripheral uses:
- Pin 21 – U2TX
- Pin 24 – U2RX
They need to be assigned into the Pin Configurator plugin.
Open the Pin Configuration plugin from the Project Graph.
Pin Settings tabs should be visible.
Locate pin 21 and assign the U2TX function to it.
Locate pin 24 and assign the U2RX function to it.
Both pins will be configured automatically when the function is selected.
COMMAND Service
Configuration
The COMMAND buffers should also be aligned with the console buffers. Set it to 2048 bytes.
RTOS Settings
The commands use string functions like sprintf and they need a stack for the RTOS of at least 4096 bytes. This is why we need to set this size to the RTOS.
File System Service
Configuration
Since there is a HTTP Server and more than one file can be loaded simultaneously on a web page (html, js, css, jpg etc.), and the application will also open files to be displayed on the Graphics Display, the FILE SYSTEM needs to be able to access more than one file at a time. Change the Maximum Simultaneous Access to 6.
We need to configure the FS to Auto Mount. This will give us the ability to not worry about initializing the SD card. Also, there are callback functions that get triggered when an SD card is inserted or removed. The code to handle this is available in the APP_FileHandler_SYS_FS_EventHandler callback function.
Since the web server will sit in the MCU Flash memory, and the pictures will sit on the SD card, the total number of media will have to be set to 2.
The Flash media format will be MPFS and the SD card will be formatted as FAT File System. This means that there will be two file system types and we need to enable the Microchip File System (MPFS).
There are two applications using the FILE SYSTEM, one is the application that displays files from the SD card, and the other is the TCPIP Stack that is serving the web server from the MPFS. Both listen to mount and unmount events, so this means two clients are listening. The number of File System Clients should be set to 2.
The other settings could be changed if needed. The application runs fine with the default settings.
Media Configuration
Each Media needs to be configured with the automount Options, so they need to be expanded and configured as necessary.
Media0 – FAT File System
Media0 will be the media mounted from the SD card, so it should be of Type SYS_FS_MEDIA_TYPE_SD_CARD with File System Type FAT and the volume will be mounted to the customized path /mnt/SDCard from the automatically set physical volume /dev/mmcblka1.
Media1 - Microchip File System
Media1 will be used for the Microchip File System (MPFS) and will be mounted from the MCU Flash, so the Media Type will have to be set to SYS_FS_MEDIA_TYPE_NVM and the File System Type should be set to MPFS2. The volume will be mounted to the customized path /mnt/myWebSite from the automatically set physical volume /dev/nvma1.
RTOS Settings
We need to speed up the FILE SYSTEM, so we configure the RTOS Task Delay to 1 ms.
SD Card (SPI) Driver
Configuration
We can leave the Driver Mode in Synchronous because we are using RTOS.
DMA will be used for SPI Transfers. This will be set into the Instance 0 Settings.
Instance 0 Settings
SD cards these days are extremely fast, so 25 MHz should be reached. It could be set to a higher speed, however the limitation in the SPI pins which are specified at 25 MHz max.
We need to detect when the SD card is removed from the system, and the reaction should be quite quick. Since the Card Detection Method setting is locked to Use Polling, the user needs to adjust this at a value coherent with the application demand. The default setting is 1 second. In this case, the detection should happen faster because there are file transfers between the SD card and the application. A setting of 1 ms would be better, which can be tweaked later. If it is too small it can cause problems with the application.
According to the Curiosity 2.0 schematic, the Chip Select Pin is connected to RD9 and it must be set into the SD card instance.
Also, to leave more room for the CPU to do other stuff, the SPI will use DMA for transmit and receive. The two DMA channels will be automatically configured by the tool. There is a DMA configuration plugin available to change the DMA settings. In this application, the DMA settings will be left at default.
RTOS Settings
We need to speed up the FILE SYSTEM, so we configure the RTOS Task Delay to 1 ms.
SPI Peripheral
Configuration
Because the files can be quite large (a few tens of KB),the SPI needs to be fast to read the files.
Set the SPI Speed to maximum. The datasheet mentions 25 MHz. We will also use DMA for transfers, as already configured in the SD card Instance 0.
Click on the Project Graph tab and then click on the SPI1 component to open its configuration options and make the necessary changes.
Pins
The demo uses the microSD click board from Mikroe:
The click board is placed into mikro BUS 1, and it is using SPI1.
The pins used are:
- Pin 69 - SDI1
- Pin 97 - RD9/SS1 - config as GPIO Out High - also configured in the SD card Instance 0.
- Pin 109 - SCK1
- Pin 121 – SDO1
They need to be assigned into the pin configurator plugin
Another way to set the pins into the PIN Configuration tool is to use the Pin Diagram:
- Click on the Pin Diagram. (Open PIN Configurator from Plugins if tabs were closed).
- Click on each pin and select its function: SDI1, SDO1 and SCK1.
- The Chip Select pin (97 - RD9) should be selected as GPIO.
For the CS pin (RD9), the direction and the reset value should also be selected. This can be done in the Pin Settings of the Pin Configuration tool. Select the Pin Settings tab and click on the Direction and Latch labels to toggle through their values.
MEMORY Driver
Settings
The MEMORY Driver is used to access the NVM to read and upload another MPFS File System binary file (in case the option is checked into the HTTP NET server).
The setting will set as Driver Mode: Synchronous.
Instance 0 Settings
The driver instance 0 is connected to the file system and to the NVM Peripheral.
The user needs to manually choose the Memory Device Type as SYS_FS_MEDIA_TYPE_NVM.
NVM Peripheral
The NVM needs to be set up for the media size. The setting here will be 128 KB. In case the file system size on the Web Server increases, this setting should be changed to accommodate the new size reported by the MPFS Generator. Just make sure the size is not larger than the available flash memory.
The start address is, by default, set in the second partition of the MCU so there is about 1 MB available.
The NVM Erase Buffer size is set by default to 16K, whichs allocates memory that is st used for something else. We set this to 0. If no RTSP is used in the project, all should be fine.
TCP/IP Stack
The TCP/IP Stack has a lot of components. They can be found under the View options and are grouped by the TCP/IP Stack Layers.
They can also be accessed from the TCP/IP Configuration plugin. The plugin will be used to select each component that needs changes.
Open the TCP/IP Configuration tool. Click on the Overview menu and all the components will be shown. Clicking on a component will open the Configuration Options for that component in MPLAB® X IDE.
The user could place the TCP/IP Configuration window on another screen because, when working inside MPLAB X IDE, the plugin window appears behind MPLAB X IDE.
HTTPNET SERVER Application Layer
Settings
Click on the HTTPNET SERVER inside the TCP/IP Configuration Overview.
The TX Buffer size can be set to double to speed up sending data to the web browser. The payload of a TCP packet is about 1500 bytes, so 1500 would be enough.
The HTTP Server needs to know what folder and subfolders will be visible from the web browser. The NMV Media inside the FILE SYSTEM is mounting the MPFS into /mnt/myWebSite, so the web pages will reside at the root of that folder. Change the Web Pages Directory to the FILE SYSTEM mount pointing to /mnt/myWebSite/.
The generation of the HTTP NET Custom Template needs to be disabled. The files are provided in the resource archive and instructions on how to generate the web pages to be a part of the project are provided later in this document. To disable it, two steps are needed:
Expand the custom template and delete the path given.
Uncheck the Include HTTP NET Custom Template option.
Advanced Settings
The advanced settings can be set as they are, except for the memory allocation and deallocation functions and for the processing of dynamic variables.
Processing of Dynamic Variables
The files on the SD card are not visible from the web browser, so it is necessary to make a request to get the file. Then the request must be processed, the file opened, and sent to the web browser, which will treat it as an image file.
For this, the demo uses dynamic variables. Because a file can be quite large, about 100 KB or even more for a bitmap, the dynamic variable processing requests will be sent with a limit of retries, and this should cover a large file size.
Note: The setting for a file generated for the web server is set to 1500 bytes per buffer, so about 400 retries in the worst case would be needed for a 150 KB file.
Malloc and Free
The functions called to allocate and free memory by the HTTPNET Server, pvPortMalloc and vPortFree, will come with FreeRTOS.
UDP Transport Layer
Configuration
UDP is used to get an IP address using the DHCP Client. It is also used by the Announce Application and, if needed, the DNS Client. Set the number of sockets for UDP to 2 for now, but this should be changed in case other UDP applications are added. This is done because each socket will need more heap, and when the user needs to decide on the heap size for the TCP/IP Stack, the UDP and the TCP number of sockets will influence the heap estimation.
TCP Transport Layer
Configuration
TCP is used by the HTTP Application, and the number of TCP sockets should be influenced by each application that is using them.
The HTTP Application uses four simultaneous connections, so the number of sockets for TCP can be reduced accordingly, to save memory.
Also, the user needs to remember that each application that is using TCP needs sockets to transfer data, and to update this value when other applications are added.
For this example, we can leave the default number of sockets as 10 by default, but this can be reduced to four or five if more memory is needed.
NETCONFIG – Instance 0
Configuration
The MAC of the MCU connects with the KSZ8061 PHY via RMII, and the MAC is responsible for having a MAC Address for the network. The PIC32MZ device comes pre-provisioned with an IEEE-unique MAC Address, and the TCPIP Stack is getting the MAC address from the PIC32MZ registers without user intervention. This is why the MAC Address is left empty.
The IP address of the NET object can be configured from the same place. This is the static IP address, and can be changed in case the DHCP client is not used to set an IP address to the board.
DHCP Start-Up flag
If the board is connected to a network with a DHCP server available to provide IP Addresses, then the option should be left to Enable DHCP Client.
If the board is connected to a network where its IP address will be static, then this option should be set to None and the IPV4 fields should be set with respect to the desired settings.
Having DHCP Client enabled on Start-Up will trigger a DHCP request for an IP address. If no DHCP servers are available, after a timeout, the Static IP Address will be used.
DNS Start-Up flag
The board will not make any requests to resolve DNS, so this option can be set to None.
In case the NTP service needs to be added to the project to have accurate time, then DNS Client will also be required to be added to the project and the DNS Flag should be also set to DNS Enable.
KSZ8061 PHY Layer
Configuration
The KSZ8061 RMII Daughter Board answers via MIIM at PHY Address 0. This needs to be changed into the KSZ8061 component.
Select KSZ8061 and change the PHY Address to 0.
Settings for how to connect with the MAC are available here.
MCU Configuration Bits
The MCU has configuration bits to set the Ethernet MAC Interface as MII or RMII. The default setting of the MCU is MII, so the user needs to make sure this configuration bit is set to RMII.
The PIC32MZ device has alternate Ethernet pins. The Curiosity 2.0 board uses the default Ethernet pins for (R)MII, so the user needs to check this setting also.
The configuration bits of the MCU are available in the system component in the Root view. Click on the System and expand the configuration options, System > Device & Project Configuration > PIC32MZ Device Configuration > Generate Fuse Settings > DEVCFG3. Verify that:
- FMIIEN is set to OFF (MII Enable OFF)
- FETHIO is set to ON (Ethernet Function on IO pins is ON)
RMII Interface – Pin setup
Usually, it is not needed to set up the Ethernet pins for the Ethernet module to function properly, however, the RMII and MDIO pins used for the MCU to communicate with the attached PHY and can have the Analog function, and this takes precedence over any other peripheral used. This can lead to communications issues between the MCU and the PHY, because when the PORT register is read, all pins configured as analog input channels are read as cleared (a low level).
For example, the pin 66 is the first analog function, and changing to ERXD0 will automatically configure the pin to be Digital.
Before setting the pin to desired function:
After setting the pin function:
The pins used for the RMII interface, as described in the Curiosity 2.0 schematic (DS70005400B-page 22) and the PIC32 MZ EF Datasheet (DS60001320H-page 538), are:
- Pin 7 - ETXD0
- Pin 8 - ETXD1
- Pin 27 - ERXCLK
- Pin 65 - ERXERR
- Pin 66 - ERXD1
- Pin 81 - ERXD0
- Pin 101 - ERXDV
- Pin 120 – ETXEN
And for the MIIM interface
- Pin 99 - EMDC
- Pin 115 - EMDIO
Use the Pin Diagram to set them up.
FreeRTOS
Advanced Settings
There is a setting needed in FreeRTOS so the project can compile called xTaskGetCurrentTaskHandle.
Also, the application has an example on how to debug heap usage for different components, except for the system heap, for this the uxTaskGetStackHighWaterMark component that needs to be included. Expand the Include components and check this option. Other components can be enabled in case the user needs them.
Core
Configuration
The core defines the application files, and each application has a task in RTOS.
We have three applications in this example.
The “app” does not quite do anything except parse the SD card for media files when the card is inserted, and blinking LEDs.
The “app_gfx” does what is neededf to manage the gGraphics sScreens and to change the media that is streamed to the display.
The “app_http” handles the requests that come from the web browser to the HTTP NET server inside the TCPIP stack.
All the applications use messages, commands, and string functions, so they would need 4096 bytes of stack.
It is not so important to call this task very often, so we can enable Use Task Delay and leave it at 50 ms, to let other tasks be called more often, like the TCP/IP Tasks, Graphics Tasks, File System Tasks, etc.
APP
APP_GFX
APP_HTTP
Memory settings
FreeRTOS
The FreeRTOS is set by default to use Heap 1. This heap management method is not quite efficient, so this application will use Heap 4 with 64 KB of heap (65,536 bytes), because there will be many tasks and each will use the stack that needs to be allocated to heap. Also, this chunk of memory is allocated away from the system heap, so care should be taken when computing the necessary system (linker) heap size.
Care should be taken in case the heap management type is changed. There could be heap models that are allocating memory into the system heap and will not allocate and manage the memory into a chunk away from the system heap.
For more details about heap types on FreeRTOS, please visit the FreeRTOS "Memory Management" page.
TCPIP Core
There is a heap estimator that comes with the TCPIP stack. The estimated value can be found in the TCPIP CORE component, in the BASIC CONFIGURATION layer.
Later, when it is necessary, the tool can be used to lower the usage of the dynamic memory by reducing the number of UDP/TCP sockets and other adjustments, which is not the object of this demo.
For fine-tuning, the heap estimator can be used.
For now, the heap estimation says the TCPIP Stack will need 33 KB, so a value of 33 KB can be used in the TCPIP Stack Dynamic RAM size option (33,792 bytes).
System
The TCP/IP stack, and later the graphics framework will need heap to function properly.
About 64 KB of memory should be enough for the heap. Remember that FreeRTOS uses Heap 4, and this is allocated as a chunk memory outside the system heap and managed by the RTOS.
Select the system component in the Project Graph, Root layer.
Expand the Configuration Options, System > Device & Project Configuration > Project Configuration > Tool Chain Selection > XC32 Global Options > Linker > General and set the heap size to 64 KB (65,536 bytes).