As IoT devices become increasingly prevalent, embedded systems operating in resource-constrained environments must carefully account for their energy requirements. These requirements are typically expressed as worst-case energy consumption (WCEC), the maximum possible energy consumed across any possible execution. One approach to finding this is by performing a static enumeration of all possible program paths. However, this static analysis is complicated by peripherals, which are a critical feature for sensor-driven and reactive embedded platforms, e.g., an IoT wearable. To incorporate peripherals into the analysis, there are three main costs to consider. The first is the energy cost of executing code. The second is the energy cost of using an active peripheral, and the third is the cost from having peripherals remain active in the background. The key challenge, however, is that peripheral devices are often stateful, with their energy consumption varying based on their internal state. This state is typically invisible to both programmers and static analyses, which leads to peripherals being modeled under overly restrictive assumptions or unmodeled altogether, hindering accurate cost analysis for many embedded platforms.
We address this challenge by encoding peripheral state directly into the type system, making the hidden internal state visible at compile time. For the cost analysis, we utilize automatic amortized resource analysis (AARA), a well-known type-based approach that provides cost bounds for programs. However, AARA alone lacks knowledge of the internal peripheral state. To address this, we extend AARA with typestate oriented programming. By combining the two ideas, we are able to reason about the cost of using peripherals in a more precise way. To our knowledge, we present the first type system combining AARA and typestate to provide a unified approach to cost analysis, while extending to asynchronous and interrupt-driven peripherals and enabling robust bounds on a wide variety of embedded platforms.

