<commit_before>/* Read an accelerometer repeatedly in a loop.  Most code adapted from the
 * dlwisp41 firmware. */

#include <enerc.h>
#include <msp430.h>
#include "dlwisp41.h"

#define NUM_READINGS 1000

unsigned numreadings = 0;

// status register &= ~i
inline void __bic_status_register (unsigned i) {
    asm volatile ("BIC.W %0, R2" ::"m"(i));
}

// status register |= i
inline void __bis_status_register (unsigned i) {
    asm volatile ("BIS.W %0, R2" ::"m"(i));
}

typedef struct {
    APPROX unsigned x;
    APPROX unsigned y;
    APPROX unsigned z;
} accel_reading;

void read_sensor (accel_reading *reading) {
    unsigned char i;

    // turn off comparator
    P1OUT &= ~RX_EN_PIN;

    // slow down clock
    BCSCTL1 = XT2OFF + RSEL1; // select internal resistor (still has effect when
    // DCOR=1)
    DCOCTL = DCO1+DCO0; // set DCO step.

    if(!is_power_good())
        sleep();

    // Clear out any lingering voltage on the accelerometer outputs
    ADC10AE0 = 0;

    P2OUT &= ~(ACCEL_X | ACCEL_Y | ACCEL_Z);
    P2DIR |=   ACCEL_X | ACCEL_Y | ACCEL_Z;
    P2DIR &= ~(ACCEL_X | ACCEL_Y | ACCEL_Z);

    P1DIR |= ACCEL_POWER;
    P1OUT |= ACCEL_POWER;
    ADC10AE0 |= ACCEL_X | ACCEL_Y | ACCEL_Z;

    // a little time for regulator to stabilize active mode current AND
    // filter caps to settle.
    for(i = 0; i < 225; i++);
    RECEIVE_CLOCK;

    // x
    ADC10CTL0 &= ~ENC;
    ADC10CTL0 = SREF_0 + ADC10SHT_1 + ADC10ON;
    ADC10CTL1 = ADC10DIV_2 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + INCH_ACCEL_X;
    ADC10CTL0 |= ENC;
    ADC10CTL0 |= ADC10SC;
    while (ADC10CTL1 & ADC10BUSY);    // wait while ADC finishes work
    reading->x = ADC10MEM & 0x3ff;

    // y
    ADC10CTL0 &= ~ENC;
    ADC10CTL0 = SREF_0 + ADC10SHT_1 + ADC10ON;
    ADC10CTL1 = ADC10DIV_2 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + INCH_ACCEL_Y;
    ADC10CTL0 |= ENC;
    ADC10CTL0 |= ADC10SC;
    while (ADC10CTL1 & ADC10BUSY);    // wait while ADC finishes work
    reading->y = ADC10MEM & 0x3ff;

    // z
    ADC10CTL0 &= ~ENC;
    ADC10CTL0 = SREF_0 + ADC10SHT_1 + ADC10ON;
    ADC10CTL1 = ADC10DIV_2 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + INCH_ACCEL_Z;
    ADC10CTL0 |= ENC;
    ADC10CTL0 |= ADC10SC;
    while (ADC10CTL1 & ADC10BUSY);    // wait while ADC finishes work
    reading->z = ADC10MEM & 0x3ff;

    // Power off sensor and adc
    P1DIR &= ~ACCEL_POWER;
    P1OUT &= ~ACCEL_POWER;
    ADC10CTL0 &= ~ENC;
    ADC10CTL1 = 0;       // turn adc off
    ADC10CTL0 = 0;       // turn adc off

    // Store sensor read count
    numreadings++;
}

unsigned short is_power_good (void) {
    return P2IN & VOLTAGE_SV_PIN;
}

inline void sleep (void) {
    P1OUT &= ~RX_EN_PIN;
    // enable port interrupt for voltage supervisor
    P2IES = 0;
    P2IFG = 0;
    P2IE |= VOLTAGE_SV_PIN;
    P1IE = 0;
    P1IFG = 0;
    TACTL = 0;

    __bic_status_register(GIE); // temporarily disable GIE so we can sleep and enable interrupts
    // at the same time
    P2IE |= VOLTAGE_SV_PIN; // Enable Port 2 interrupt

    if (is_power_good())
        P2IFG = VOLTAGE_SV_PIN;

    __bis_status_register(LPM4_bits | GIE);

    return;
}

#ifdef __clang__
// work around a bug wherein clang doesn't know that main() belongs in section
// .init9 or that it has to be aligned in a certain way (mspgcc4 introduced
// these things)
__attribute__((section(".init9"), aligned(2)))
#endif
int main (void) {
    accel_reading cur_reading;
    APPROX unsigned max_x, max_y, max_z;
    APPROX unsigned min_x, min_y, min_z;
    APPROX unsigned avg_x, avg_y, avg_z;
    unsigned i;

    for (i = 0; i < NUM_READINGS; ++i) {
        read_sensor(&cur_reading); // ACCEPT_PERMIT

        if (ENDORSE(cur_reading.x > max_x)) max_x = cur_reading.x;
        if (ENDORSE(cur_reading.y > max_y)) max_y = cur_reading.y;
        if (ENDORSE(cur_reading.z > max_z)) max_z = cur_reading.z;

        if (ENDORSE(cur_reading.x < min_x)) min_x = cur_reading.x;
        if (ENDORSE(cur_reading.y < min_y)) min_y = cur_reading.y;
        if (ENDORSE(cur_reading.z < min_z)) min_z = cur_reading.z;

        // TODO: moving average
    }

    return 19; // lucky 0x13<commit_msg>drastically simplify msp430-accel benchmark:  - 1-dimensional, not 3-dimensional, accelerometer  - fewer accelerometer readings total  - remove superfluous stuff that is not meaningful in simulation<commit_after>/* Read an accelerometer repeatedly in a loop.  Most code adapted from the
 * dlwisp41 firmware. */

#include <enerc.h>
#include <msp430.h>
#include "dlwisp41.h"

#define NUM_READINGS 250u

APPROX unsigned int numreadings = 0;

// status register &= ~i
inline void __bic_status_register (unsigned i) {
    asm volatile ("BIC.W %0, R2" ::"m"(i));
}

// status register |= i
inline void __bis_status_register (unsigned i) {
    asm volatile ("BIS.W %0, R2" ::"m"(i));
}

APPROX unsigned read_accel() {
    APPROX unsigned char i;
    APPROX unsigned reading;

    // slow down clock
    BCSCTL1 = XT2OFF + RSEL1; // select internal resistor (still has effect when
    // DCOR=1)
    DCOCTL = DCO1+DCO0; // set DCO step.

    // Clear out any lingering voltage on the accelerometer outputs
    ADC10AE0 = 0;

    P2OUT &= ~(ACCEL_X | ACCEL_Y | ACCEL_Z);
    P2DIR |=   ACCEL_X | ACCEL_Y | ACCEL_Z;
    P2DIR &= ~(ACCEL_X | ACCEL_Y | ACCEL_Z);

    P1DIR |= ACCEL_POWER;
    P1OUT |= ACCEL_POWER;
    ADC10AE0 |= ACCEL_X | ACCEL_Y | ACCEL_Z;

    // a little time for regulator to stabilize active mode current AND
    // filter caps to settle.
    for(i = 0; ENDORSE(i < 225); i++);
    RECEIVE_CLOCK;

    ADC10CTL0 &= ~ENC;
    ADC10CTL0 = SREF_0 + ADC10SHT_1 + ADC10ON;
    ADC10CTL1 = ADC10DIV_2 + ADC10SSEL_0 + SHS_0 + CONSEQ_0 + INCH_ACCEL_X;
    ADC10CTL0 |= ENC;
    ADC10CTL0 |= ADC10SC;
    while (ADC10CTL1 & ADC10BUSY);    // wait while ADC finishes work
    reading = ADC10MEM & 0x3ff;

    // Power off sensor and adc
    P1DIR &= ~ACCEL_POWER;
    P1OUT &= ~ACCEL_POWER;
    ADC10CTL0 &= ~ENC;
    ADC10CTL1 = 0;       // turn adc off
    ADC10CTL0 = 0;       // turn adc off

    return reading;
}

#ifdef __clang__
// work around a bug wherein clang doesn't know that main() belongs in section
// .init9 or that it has to be aligned in a certain way (mspgcc4 introduced
// these things)
__attribute__((section(".init9"), aligned(2)))
#endif
int main (void) {
    WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer

    APPROX unsigned cur_reading = 0;
    APPROX unsigned max = 0;
    APPROX unsigned min = 0xffffu;
    APPROX unsigned avg = 0xbeefu;

    for (; ENDORSE(numreadings != NUM_READINGS); ++numreadings) {
        cur_reading = read_accel(); // ACCEPT_PERMIT

        if (ENDORSE(cur_reading > max)) max = cur_reading;
        if (ENDORSE(cur_reading < min)) min = cur_reading;

        // TODO: moving average
    }

    return 19; // lucky 0x13
{'fraction_non_alphanumeric': 0.10965942454492072, 'fraction_numerical': 0.044920728126834994, 'mean_word_length': 2.8082727780883174, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6241585', 'n_tokens_mistral': 2841, 'n_tokens_neox': 2525, 'n_words': 712}