summaryrefslogtreecommitdiff
path: root/usb-bmp280/usb-bmp280.c
blob: 13be70d0f13985b9be406bd45ff1a8f9749da9f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  spreadspace avr utils - usb-bmp180 example
 *
 *
 *  Copyright (C) 2016 Bernhard Tittelbach <bernhard@tittelbach.org>
 *  basically this is refactored and enhanced code from:
 *   https://github.com/sparkfun/BMP180_Breakout_Arduino_Library
 *   Please buy the sparkfun people Beer when you see them!!!!
 *
 *  This file is part of spreadspace avr utils.
 *
 *  spreadspace avr utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  spreadspace avr utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>

#include <LUFA/Drivers/Peripheral/SPI.h>

#include "util.h"
#include "usbio.h"
#include "bmp280.h"

#define ALTITUDE_GRAZ 353.0 // Altitude of Graz in Austria

#define PIN_CS_S0 PD1
#define REG_CS_S0 PIND
#define PIN_CS_S1 PD0
#define REG_CS_S1 PIND

#define OP_SETBIT |=
#define OP_CLEARBIT &= ~
#define OP_CHECK &
#define PIN_SW(PORTDDRREG, PIN, OP) PORTDDRREG OP (1 << PIN)
#define PINMODE_OUTPUT(REG, PIN) *(&REG+1) |= (1 << PIN)  //just use DDR instead of PORT
#define PINMODE_INPUT(REG, PIN) *(&REG+1) &= ~(1 << PIN)  //just use DDR instead of PORT
#define PINREG(x) x
#define DDRREG(x) *(&x+1)
#define PORTREG(x) *(&x+2)

#define HIGHv OP_SETBIT
#define LOWv OP_CLEARBIT

#define CS_SENSOR_0(LOWHIGH) (PIN_SW(PORTREG(REG_CS_S0),PIN_CS_S0,LOWHIGH))
#define CS_SENSOR_1(LOWHIGH) (PIN_SW(PORTREG(REG_CS_S1),PIN_CS_S1,LOWHIGH))
#define CS_SENSOR(x,LOWHIGH) CS_SENSOR_##x(LOWHIGH)

/*
=== Our Coefficients ===
T1: 28483, T2: 26628, T3: 50
P1: 37848, P2: -10585, P3: 3024, P4: 9882, P5: -217, P6: -7, P7: 15500, P8: -14600, P9: 6000

=== Adafruit Arduino Reference Coefficients ===
T1: 28483, T2: 26628, T3: 50
P1: 37848, P2: -10585, P3: 3024, P4: 9882, P5: -217, P6: -7, P7: 15500, P8: -14600, P9: 6000
*/

void printCoeffs(bmp280_sensor *sensor)
{
  printf("T1: %u, T2: %d, T3: %d\r\n", sensor->dig_T1, sensor->dig_T2, sensor->dig_T3);
  printf("P1: %u, P2: %d, P3: %d, P4: %d, P5: %d, P6: %d, P7: %d, P8: %d, P9: %d\r\n",sensor->dig_P1,sensor->dig_P2,sensor->dig_P3,sensor->dig_P4,sensor->dig_P5,sensor->dig_P6,sensor->dig_P7,sensor->dig_P8,sensor->dig_P9);
  //printf("H1: %d, H2: %d, H3: %d, H4: %d, H5: %d, H6: %d\r\n",sensor->dig_H1,sensor->dig_H2,sensor->dig_H3,sensor->dig_H4,sensor->dig_H5,sensor->dig_H6);
}

int main(void)
{
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  cpu_init();
  usbio_init();
  sei();

  bmp280_sensor sensor0;
  bmp280_sensor sensor1;

  uint8_t sensor0_available = 0;
  uint8_t sensor1_available = 0;

  //CS Output
  PINMODE_OUTPUT(REG_CS_S0,PIN_CS_S0);
  PINMODE_OUTPUT(REG_CS_S1,PIN_CS_S1);
  //CS High
  CS_SENSOR(0,HIGHv);
  CS_SENSOR(1,HIGHv);
  //LUFA SPI
  SPI_Init(BMP280_LUFA_SPIO_OPTIONS);

  printf("USB-SPI-BMP280 starting");
  
  bmp280_result tmppres;
  float altitude;
  
  for(;;) {
    printf("\r\n== Sensor 0 ==\r\n");
    if (sensor0_available)
    {
      printCoeffs(&sensor0);
      tmppres = bmp280_readTempAndPressure(&sensor0);
      altitude = bmp280_calcAltitude(tmppres.pressure, BMP280_AVERAGE_SEA_LEVEL_PRESSURE);
      printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", tmppres.pressure, tmppres.temperature);
      printf("Altitude: %.2fm (guessing)\r\n", altitude);
      sensor0_available = bmp280_check_chipid(&sensor0);
    } else {
      printf("  not available\r\n");
      sensor0_available = bmp280_init(&sensor0, &PORTREG(REG_CS_S0), PIN_CS_S0);
    }

    printf("\r\n== Sensor 1 ==\r\n");
    if (sensor1_available)
    {  
      printCoeffs(&sensor1);
      tmppres = bmp280_readTempAndPressure(&sensor1);
      altitude = bmp280_calcAltitude(tmppres.pressure, BMP280_AVERAGE_SEA_LEVEL_PRESSURE);
      printf("\r\nPressure: %.2f Pa @ %.2f degC\r\n", tmppres.pressure, tmppres.temperature);
      printf("Altitude: %.2fm (approximate)\r\n", altitude);
      sensor1_available = bmp280_check_chipid(&sensor1);
    } else {
      printf("  not available\r\n");
      sensor1_available = bmp280_init(&sensor1, &PORTREG(REG_CS_S1), PIN_CS_S1);
    }

    usbio_task();

    _delay_ms(3000); // Pause for 3 seconds.
  }
}