ECE 477 Digital Systems Senior Design Project Rev 8/09 Homework 9: Software Design Considerations Team Code Name: __Home Enhancement Suite__________________ Group No. __10__ Team Member Completing This Homework: _____Allen Humphreys___________________ E-mail Address of Team Member: aehumphr@ purdue.edu NOTE: This is the last in a series of four “design component” homework assignments, each of which is to be completed by one team member. The body of the report should be 3-5 pages, not including this cover sheet, references, attachments or appendices. Evaluation: SCORE DESCRIPTION Excellent – among the best papers submitted for this assignment. Very few corrections needed for version submitted in Final Report. Very good – all requirements aptly met. Minor additions/corrections needed for 9 version submitted in Final Report. Good – all requirements considered and addressed. Several noteworthy 8 additions/corrections needed for version submitted in Final Report. Average – all requirements basically met, but some revisions in content should 7 be made for the version submitted in the Final Report. Marginal – all requirements met at a nominal level. Significant revisions in 6 content should be made for the version submitted in the Final Report. Below the passing threshold – major revisions required to meet report * requirements at a nominal level. Revise and resubmit. * Resubmissions are due within one week of the date of return, and will be awarded a score of “6” provided all report requirements have been met at a nominal level. 10 Comments: Comments from the grader will be inserted here. ECE 477 Digital Systems Senior Design Project Rev 8/09 1.0 Introduction The Home Enhancement Suite is a set-top box coupled with two remote RFID units, which will use WRL-08664 Xbee RF modules to communicate [5]. The main unit will control a room’s lights, door locks, and television based on user information received from the RFID readers. This unit’s functions are enabled by a PIC32MX695F512L microcontroller, which has all the necessary communication units (SPI, UART, and Ethernet) on chip to facilitate straightforward and expedient software development [2]. This project will also provide a web-based interface for changing user settings and remotely controlling the available devices. The majority of the application program will be a cooperative multitasking loop combined with timer interrupts that controls the web server, lights, and locks with the possible use of an interrupt service routine for television control. 2.0 Software Design Considerations As with almost all computer systems, memory usage is a concern for the Home Enhancement Suite. Table 1 shows the physical memory address mapping for the microcontroller. The microcontroller has 128 KB of RAM and 512 KB of program flash. Examining the table in Appendix D shows that a minimum of ~70 KB of RAM is required by the stack applications alone, while up to 120 KB of RAM is required for the speed optimized solution. Although this example is not for the project’s exact microcontroller, it does show that the original decision to use the PIC32MX664F064L, which only has 32 KB of RAM, was truly an infeasible option. With the current software design considerations, there is no user controlled stack. Essentially, data placement is handled by the compiler. -1- ECE 477 Digital Systems Senior Design Project Configuration 0x1FC02FFF Registers 0x1FC02FF0 Boot Flash 0x1FC02FEF (12 KB) 0x1FC00000 Special Function 0x1F8FFFFF Rev 8/09 Registers 0x1F800000 Program Flash 0x1D07FFFF (512 KB) 0x1D000000 RAM 0x0001FFFF (128 KB) 0x00000000 Table 1 Physical Memory Address Layout The central device of the Home Enhancement Suite is a plastic box that is approximately the size of a personal network router. At its heart, the unit has a PIC32MX695F512L microcontroller. In addition, it has several auxiliary ICs that facilitate the physical interfaces to external functions. There are also several functions that do not require additional interfacing hardware. The instant save button, available on the unit to allow anyone to save the current light intensity as their preferred setting, is connected to general purpose input/output (GPIO) port E, pin 5 (RE5). The consumer electronics control (CEC) bus line is a single-wire bus that connects microcontroller GPIO pin RE4 to the HDMI cable connector, which is used to control TV functions including volume and channel. GPIO pin RG0 is used to control an electric strike door locking mechanism through an optoisolator, which allows the device to control security. These three output pins are controlled by corresponding tri-state function (TRISx) and open-drain -2- ECE 477 Digital Systems Senior Design Project Rev 8/09 configuration (ODCx) registers. The save button is a simple digital input, so TRISE5 is set to ‘1’ with a default value of ‘0’ on ODCE5 (when it’s an input this has no effect anyway). RG0 is a simple digital output, so TRISG0 is set to ‘0’ and ODCG0 is set to ‘0’. The CEC line requires RE4 to be an open-drain output, so TRISE4 is set to ‘0’ while ODCE4 is set to ‘1’. The main unit has an ambient light sensor that is used to determine the current light level. The sensor is connected to analog input port 2 (AN2) of the analog to digital converter (ADC), which is controlled by six control registers. ADC1 control register 1 (AD1CON1) and AD1 port configuration register (AD1PCFG) are the two primary registers that must be set to allow basic operation of the ADC. AD1PCFG is set to 0xFFFB to allow analog input on AN2 and leave the remaining 15 inputs digital. AD1CON1<13> is set to ‘1’, stopping the ADC module in idle mode. AD1CON1<10:8> is set to “100” to allow for 32-bit unsigned integer output. AD1CON1<7:5> is set to “000” for manual triggering of a conversion. AD1CON1<2> is set to ‘0’ indicating automatic sampling start. AD1CON1<1> is used for manual conversion triggering via writing a ‘0’ when a result is desired. SPI1 – Micromint Serial PLIX chip Due to a misunderstanding of the communication interface described the in the data sheet, this IC is connected to an SPI1 module, but it should be connected to a UART module. Setup of the pins will be determined by how the situation is remedied. Currently, two options are available. The first is to “fly-wire” the connections to different pins on the microcontroller. And, the second is to use the currently wired pins as GPIO pins and implement a software based UART module. Each solution has potential challenges, but the project team has to meet and discuss what will be done. Several UART modules are used for various off-chip communications. UART1 interfaces with the WRL-08664 RF Module. UART2 uses only the transmit line to control what is displayed on the Spark Fun SerLCD v2.5 LCD. UART5 uses only receive line, which receives a string of characters from the ID-12 RFID receiver that is unique to a single RFID tag. The UART modules are controlled by five special purpose registers as well as the TRISx registers for indicating input or output and the ODCx register to enable open-drain output. The preliminary initialization code can be viewed here. The microcontroller’s onboard Ethernet media access controller (MAC) module interfaces with a Texas Instruments DP83848C PHYTER 10/100Mb/s Ethernet physical layer -3- ECE 477 Digital Systems Senior Design Project Rev 8/09 transceiver chip using the RMII protocol. The initialization routines are provided in Microchip’s TCP/IP stack example [3]. All the software is written in embedded C in the MPLAB X v1.0 IDE provided by Microchip using the MPLAB C32 v2.02 compiler. The main control loop operates as a “cooperative multitasking” loop, as described in the Microchip TCPIP demo application comments. The loop is essentially a flag-driven polling-loop with a timer interrupt to control flag setting and task switching. For this setup to work, each task has to be written such that it can be performed in smaller portions with appropriate buffering to allow queuing of work. For example, the StackApplications() module will process one packet of data per call. Ensuring that no single task can monopolize the processor is essential to prevent buffer overruns and keep throughput at an acceptable level. Debugging is facilitated by four LEDs driven by GPIO pins RE0-RE3 with TRISE values of ‘0’ to allow for digital output. Further debugging messages/information can be displayed on the main unit’s LCD by holding the instant save button down for 10 seconds. 3.0 Software Design Narrative All initializations routines are called from the main() function before it enters the cooperative multitasking (infinite) loop. The InitializeBoard() routine initializes all on-chip peripheral control registers, including the GPIO pins. This must been done first to ensure proper functioning of the software initialization modules. The Microchip-provided TCPIP stack includes the InitAppConfig() and StackInit() modules that open and prepare the desired software features of the TCPIP stack. InitAppConfig() initializes values stored in the APP_CONFIG data structure. There is only one of these structures and the variable, AppConfig, is globally available. One example of a field that is set by InitAppConfig() is the MyIPAddr.Val, which is set to a default IP address defined in a header file. Later, the DHCP client receives a dynamic IP from a DHCP server on the network and the default value is updated. The StackInit() module is the last initialization module called. It, in turn, calls ARPInit(), TCPInit(), and HTTPInit() to setup all software modules related to the corresponding services. For example, the ARPInit() clears the cached media layer address to guarantee no incorrect behavior out of reset. The provided code only caches one ARP lookup, but it could be expanded if multiple simultaneous users slow down Ethernet communications. -4- ECE 477 Digital Systems Senior Design Project Rev 8/09 Status: Chip initializations are written but not tested. Using the example code, the stack initializations have successfully been used to setup an example web server. Hotlink: InitializeBoard(), InitAppConfig(), StackInit() The X10 devices used by this project do not provide a method for querying the lighting unit to determine its current level. To properly facilitate the instant save feature for a user’s preferred light setting; the main unit must be calibrated so it can “read” the light setting from the ambient light sensor. When the unit boots up and determines there is no saved light information, it will send a command turning the lights on. At that level, and each one after, a reading from the light sensor will be taken. The calibration module repeatedly sends the dim command, which turns the intensity down by one level. Once the sensor reading stops changing, the module records the value as the lowest light setting. After the dimmest level is determined, the module repeatedly sends the bright command, which turns the intensity up by one level. During this loop, the sensor reading is saved into its corresponding entry in the lighting level table. Status: This code has not been written. Development cannot continue until the wiring problem has been solved. The cooperative multitasking loop continuously calls StackTask(), StackApplications(), and ReceiveUser(). Each module is written such that any time consuming tasks can be paused and resumed the next time it is called. Both StackTask() and StackApplications() are provided by Microchip in their TCPIP demonstration application. Status: Using the example code, the stack tasks have successfully served web pages using an HTTP web server over TCP. Hotlink: StackTsk.c contains StackTask() and StackApplications() ReceiveUser() checks the the local RFID reader buffer (UART5) and the wireless module buffer (UART1) for a waiting user ID. Then it performs a series of logical tests. First, it determines whether the ID is valid. If it is, then it checks to see if there is already a user logged in. If there is not one and the user ID is from UART1, it calls the UpdateDoor() module. If there is a user logged in, and the new user has a higher priority than the current one, it calls the UpdateLights() module. After the remaining update modules are serviced, the final task of the multitasking loop of checking the state of the instant save button is performed. If a button press is registered, than the InstantSave() module is called, otherwise, the loop restarts. Status: This code has not been written. -5- ECE 477 Digital Systems Senior Design Project Rev 8/09 When the conditions are met for changing the current state of the lights, an ambient light reading must be performed to determine the current light setting as described in the InstantSave() description below. If the lights are off and should be on, the command $UONHMM01 is sent to the PLIX Serial chip. If the lights are on and should be off, the command $AUOHMM01 is sent to the PLIX Serial chip. If the lights are on and need to be adjusted, the microcontroller sends a $DIMHMMNN or $BRTHMMNN command with NN being set to the number of steps up or down the lights need to go to reach the desired setting. All values of the house code H and unit code MM need to be set according to the command format table in Appendix C. Status: This code has not been written. The final setup of the UpdateTV() module will probably change as a complete understanding of the CEC protocol is necessary to continue development. CEC messages are sent of the CEC bus with properly addressed header and data blocks. The data blocks contain the CEC commands and they’re formatted into hexadecimal opcodes and their corresponding arguments. Status: This code has not been written. Updating the LCD is performed in the UpdateLCD() module. This section essentially places the cursor at the desired location using the control commands provided by the SerLCD interface. Once the cursor is place, ASCII characters are sent and printed with no special formatting required. Status: This code has not been written. The InstantSave() module initiates ADC sampling by setting bit 1 of the AD1CON1 register to ‘1’. After a sufficient delay, sampling is halted by clearing bit 1 to ‘0’. Then the code waits for bit 0 of the AD1CON1 register to be a ‘1’ indicating the conversion is complete. Finally, ADCBUF0 is read and compared to the light calibration table. The closest light level setting is then stored into the current user’s preferred light setting. Status: This code has not been written. 4.0 Summary The Home Enhancement Suite provides a centralized, automated control of commonly used household functions. The software for the PIC32MX695F512L is written in embedded C, and makes use of built in ADC, UART, and Ethernet modules to offer a wide range of features. The -6- ECE 477 Digital Systems Senior Design Project Rev 8/09 rough-draft register initializations for the on-chip peripherals have been completed and the reasons for them were outlined. Software development is in its very early stages of completion, and some software cannot continue until hardware issues are resolved. -7- ECE 477 Digital Systems Senior Design Project Rev 8/09 List of References [1] Quantum Data, Inc. “Designing CEC into your next HDMI Product” [Online]. Available: http://www.quantumdata.com/pdf/CEC_White_Paper.pdf [2] Microchip Technology. PIC32MX5XX/6XX/7XX Family Data Sheet (Rev. G) [Online]. Available: http://ww1.microchip.com/downloads/en/DeviceDoc/61156G.pdf [3] Microchip Technology. TCP/IP Demo App [Online]. Available: TCPIP Demonstration Application [4] Microchip Technology. . Available: https://engineering.purdue.edu/477grp10/Datasheets/TCPIP_C32_Memory_Usage.htm [5] Digi International. Product Manual (v1.xEx) [Online]. Available: http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf IMPORTANT: Use standard IEEE format for references, and CITE ALL REFERENCES listed in the body of your report. Any URLs cited should be “hot” links. -8- ECE 477 Digital Systems Senior Design Project Rev 8/09 Appendix A: Flowchart/Pseudo-code for Main Program Start Calibrate Lights Yes Peripheral Initializations ReceiveUser() First Boot Up? Valid User? Yes User Present? No No InitAppConfig() Higher Priority? From Door? Yes No StackTask() StackInit() Yes Yes UpdateLights() No Fetch Data Packet IP? Yes Valid? Yes No Yes Get IP Header UpdateLCD() No TCP? UpdateTV() No No StackApplications End ReceiveUser() No Received User ID? No Save Button Pressed? Yes InstantSave() -9- UpdateDoor() ECE 477 Digital Systems Senior Design Project Rev 8/09 Appendix B: Hierarchical Block Diagram of Code Organization main() InitializeBoard() CalibrateLights() InitAppConfig() StackInit() StackTask() while(1) StackApplictions() UpdateDoor() -10- ReceiveUser() UpdateLights() InstantSave() UpdateTV() UpdateLCD() ECE 477 Digital Systems Senior Design Project Command ALO BRT DINA UON AUO DIM OFF Rev 8/09 Appendix C: PLIX Serial Command Set X10 Function Example All Lights On $ALOHMMNN Bright $BRTHMMNN Receive X10 data Not Applicable On $UONHMMNN All Units Off $AUOHMMNN Dim $DIMHMMNN Off $OFFHMMNN In the examples, H is the house code, which is a letter A-P; MM is the unit code, a number 01 through 16; and NN is the number of times to repeat the command. Module Approx Stack Code Appendix D: Memory Usage Approximation Table Program Memory Program Memory Global Data Memory Space Optimization Speed Optimization (-0s) (-O3) 14192 32636 108 662 738 8 AutoIP 2644 2764 64 DHCP Client 3808 3908 60 Announce DNS Client 2632 3180 56 15438 18580 137 ICMP Client 1360 1336 36 ICMP Server 360 372 0 NBNS 1568 1676 20 SMTP Client 7090 10326 63 SNMP Agent 3185 11191 193 SNTP Client 898 1018 32 TCP 13924 24656 214 UDP 2356 2476 20 Total 70117 114857 1011 HTTP2 Server/MPFS2 -11-