Skip to content

Commit

Permalink
Merge #83
Browse files Browse the repository at this point in the history
83: hail test: add more features r=ppannuto a=bradjc

This makes the hail test app use 12/13 grant regions (all but IPC).

Co-authored-by: Brad Campbell <[email protected]>
Co-authored-by: Pat Pannuto <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2020
2 parents 468e84a + 278f535 commit 3d96d35
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/tests/hail/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,39 @@
#include <adc.h>
#include <ambient_light.h>
#include <button.h>
#include <crc.h>
#include <gpio.h>
#include <humidity.h>
#include <led.h>
#include <ninedof.h>
#include <nrf51_serialization.h>
#include <rng.h>
#include <temperature.h>
#include <timer.h>


/////////////////////////////////////////////////////////////////////
// Software CRC implementation for validating CRC driver
//
// From http://home.thep.lu.se/~bjorn/crc/ (April 22, 2020)
static uint32_t crc32_for_byte(uint32_t r) {
for (int j = 0; j < 8; ++j)
r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1;
return r ^ (uint32_t)0xFF000000L;
}

static void reference_crc32(const void *data, size_t n_bytes, uint32_t* crc) {
static uint32_t table[0x100];
if (!*table)
for (size_t i = 0; i < 0x100; ++i)
table[i] = crc32_for_byte(i);
for (size_t i = 0; i < n_bytes; ++i)
*crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
}
/////////////////////////////////////////////////////////////////////



// Intervals for BLE advertising and connections
simple_ble_config_t ble_config = {
.platform_id = 0x13, // used as 4th octect in device BLE address
Expand Down Expand Up @@ -48,6 +73,16 @@ static void button_callback(__attribute__ ((unused)) int btn_num,
}
}

// Callback for gpio interrupts.
// - pin_num: The index of the pin associated with the callback.
// - pin_state: 1 if high, 0 if low.
static void gpio_callback( int pin_num,
int pin_state,
__attribute__ ((unused)) int arg2,
__attribute__ ((unused)) void *ud) {
printf("GPIO Interrupt: pin: %i, state: %i\n", pin_num, pin_state);
}

static void sample_sensors (void) {

// Sensors: temperature/humidity, acceleration, light
Expand Down Expand Up @@ -80,6 +115,16 @@ static void sample_sensors (void) {
int d6 = gpio_read(2);
int d7 = gpio_read(3);

// Random bytes
uint8_t rand[5];
rng_sync(rand, 5, 5);

// CRC of the random bytes
uint32_t crc;
crc_compute(rand, 5, CRC_32, &crc);
uint32_t reference_crc = 0;
reference_crc32(rand, 5, &reference_crc);

// print results
printf("[Hail Sensor Reading]\n");
printf(" Temperature: %d 1/100 degrees C\n", temp);
Expand All @@ -96,6 +141,9 @@ static void sample_sensors (void) {
printf(" D1: %d\n", d1);
printf(" D6: %d\n", d6);
printf(" D7: %d\n", d7);
printf(" Random: %#04x %#04x %#04x %#04x %#04x\n", rand[0], rand[1], rand[2], rand[3], rand[4]);
printf(" CRC: %#010lx (%s reference implementation)\n", crc,
(crc == reference_crc) ? "Matches" : "!! Does not match");
printf("\n");

// toggle the blue LED
Expand All @@ -122,6 +170,10 @@ int main(void) {
gpio_enable_input(2, PullDown); // D6
gpio_enable_input(3, PullDown); // D7

// Enable interrupts on D7
gpio_interrupt_callback(gpio_callback, NULL);
gpio_enable_interrupt(3, Change);

// sample sensors every second
while (1) {
sample_sensors();
Expand Down

0 comments on commit 3d96d35

Please sign in to comment.