THE WIRELESS TREETAP SYSTEM
5.4 SOFTWARE DEVELOPMENT
Table 5.2 The probe’s sensitivity levels.
Sensitivity level Threshold value
Always-trigger 0
High 6.0σn
Normal 8.0σn
Low 10.0σn
configures each attached probe by setting the probe’s trigger-threshold according to its gain and sensitivity settings.
5.4 SOFTWARE DEVELOPMENT
The WTT software consists of two major components: the probe firmware and the smart- phone application. The WTT firmware is a program which runs on the probe’s SAM4S microcontroller, this program is described in Section 5.4.1. An Android application was developed to run on a smartphone, this application is described in Section 5.4.2.
5.4.1 Probe firmware
The firmware is the software that runs on the probe’s SAM4S microcontroller. The firmware is written in the C programming language. Each probe contains identical firmware; any unit-specific configuration is done at runtime. The WTT-probe firmware is constructed in a modular architecture, organised into four library-level groups: application code (wtreetap), application libraries (libs), general purpose libraries (mmculib), and peripheral drivers (mat91lib). The firmware was implemented as a
baremetal application, i.e., there is no operating-system running on the microcontroller. A diagram of the program structure is shown in Figure 5.11.
The top-level program, wtreetap, contains the main loop, program logic, handling of communication messages, measurement control, and power-state control. It is written in a modular structure so that specific tasks are grouped in separate source files, e.g., power supply control functions are in psu.c. Coupling between modules are kept to a minimum. The modules at this level are application-specific in the sense that they are not expected to be reused, should another application be developed.
The application code depends on two groups of modules. The first,libs, contains device-specific and application-specific libraries. These are libraries and device drivers which could potentially be ported to another application. This group includes a driver for the Bluetooth device, drivers for the internal and external ADCs a driver for the synchronisation radio and a driver for the PGA. The second group of modules required
wtreetap
wtreetap data error
system psu wakeup
libs
lmx9838 iadc sadc
nRF24L01 pga sync
mmculib
pacer usb-cdc busart
tty
mat91lib
usart ssc tc
ac gpio
Figure 5.11 The dependency structure of the WTT probe’s firmware. The application code,
wtreetap, is decoupled from its dependent libraries: libs,mmculib, andmat91lib.
by the application belong to the mmculiblibrary3. This is a library for programming baremetal microcontroller applications. It provides high-level application programming interfaces (APIs) for: timing tasks, communication interfaces, and controlling a wide range of external devices. mmculib is designed to be independent of the underlying hardware. It depends on another library,mat91lib4, for providing access to the micro- controller’s peripherals. mat91libprovides APIs for accessing the MCU’s peripherals on the SAM4S device.
Listing 5.1 shows a pseudocode overview of the main operations the probe performs while it is not in low power (sleep) mode. The firmware utilises a pacer module, which calls the pacer_wait() function in the main loop. This function causes the CPU to wait until the pre-defined period has been exceeded, which is set to 1 ms. The functioncommand_poll() accepts a communication interface (either Bluetooth or USB), and handles messages received over this interface. The communication interfaces are implemented as unix-standard streams [Kernighan and Ritchie 1988]. This allows the hardware specific details of those devices to be abstracted. All communication to
3Available at
https://github.com/mph-/mmculib, accessed 24/08/2017.
5.4 SOFTWARE DEVELOPMENT 111
the probe is initiated synchronously by the USB or Bluetooth device communicating with it, i.e., the PC or smartphone app. Thus, the probe acts as a server, with the client initiating and controlling the device’s state. Once a measurement has begun, themeasure_data() function is periodically called. This function checks the received ADC buffers, which are populated concurrently to the main loop using direct memory access (DMA). The buffers are scanned to find a sample which exceeds the trigger threshold. Once a sample exceeds the threshold, the buffers are saved, sampling ceases, and the device’s state changes toTRIGGERED. Communication over the Bluetooth and synchronisation radio are ignored during sampling, i.e., in the WAITING state. This is done to minimise the effect of RF noise and digital switching from coupling into the front-end. A state machine showing the probe’s operational modes is shown in Figure 5.12.
Listing 5.1 Pseudocode listing of the WTT probe’s main loop.
void main() { state = STATE_STOPPED; initialise_hardware (); initialise_pacer (PACER_PERIOD); while (1) { pacer_wait (); command_poll (dev_usb); if (state == STATE_WAITING) { measure_data (); } else { command_poll (dev_bt); sync_poll (); } } }
5.4.2 Smartphone application development
An Android app was developed for controlling and coordinating the probes. Android was selected over its main rival, iOS, because of its high quality development tools, prevalence of Android hardware, and Android’s non-restrictive support of Bluetooth serial port profile (SPP)5. The core functions of the app are:
• Managing Bluetooth connections to probe devices.
5iOS does not support Bluetooth SPP without an approved MFi license
https://support.apple. com/en-nz/HT204387, accessed 5/7/2018.
Stopped Waiting Triggered Timeout Fetch data cmd: start Vin<threshold Vin> threshold measurement time exceeded cmd: get-state cmd: get-state cmd: get-state
Figure 5.12 State-machine diagram of the WTT probe. The nodes represent the probe’s states; the
5.4 SOFTWARE DEVELOPMENT 113