aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32F3xx/I2C-LSM303DLHC/main.c
blob: 5cee22670d493532532d14950fe20905c27a6082 (plain)
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
/*
    ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"

#include "usbcfg.h"
#include "chprintf.h"
#include "lsm303dlhc.h"

/*
 * DP resistor control is not possible on the STM32F3-Discovery, using stubs
 * for the connection macros.
 */
#define usb_lld_connect_bus(usbp)
#define usb_lld_disconnect_bus(usbp)

/* Enable use of special ANSI escape sequences */
#define CHPRINTF_USE_ANSI_CODE         TRUE

static BaseSequentialStream * chp = (BaseSequentialStream*) &SDU1;

/* LSM303DLHC Driver: This object represent an LSM303DLHC instance */
static LSM303DLHCDriver LSM303DLHCD1;

static int32_t rawdata[LSM303DLHC_ACC_NUMBER_OF_AXES + LSM303DLHC_COMP_NUMBER_OF_AXES];
static float cookeddata[LSM303DLHC_ACC_NUMBER_OF_AXES + LSM303DLHC_COMP_NUMBER_OF_AXES];

static char axesID[LSM303DLHC_ACC_NUMBER_OF_AXES] = {'X', 'Y', 'Z'};
static uint32_t i;

static const I2CConfig i2ccfg = {
  STM32_TIMINGR_PRESC(15U) |
  STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) |
  STM32_TIMINGR_SCLH(15U)  | STM32_TIMINGR_SCLL(21U),
  0,
  0
};

static const LSM303DLHCAccConfig lsm303dlhcacccfg = {
  LSM303DLHC_ACC_FS_2G,
  LSM303DLHC_ACC_ODR_100Hz,
  LSM303DLHC_ACC_AE_XYZ,
  LSM303DLHC_ACC_LP_DISABLED,
  LSM303DLHC_ACC_HR_DISABLED,
  LSM303DLHC_ACC_BDU_BLOCK,
  LSM303DLHC_ACC_END_LITTLE,
  LSM303DLHC_ACC_UNIT_G
};

static const LSM303DLHCCompConfig lsm303dlhccompcfg = {
  LSM303DLHC_COMP_FS_1_3_GA,
  LSM303DLHC_COMP_ODR_30HZ,
  LSM303DLHC_COMP_MD_CONT,
};

static const LSM303DLHCConfig lsm303dlhccfg = {
  &I2CD1,
  &i2ccfg,
  &lsm303dlhcacccfg,
  &lsm303dlhccompcfg
};

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Initializes a serial-over-USB CDC driver.
   */
  sduObjectInit(&SDU1);
  sduStart(&SDU1, &serusbcfg);

  /*
   * Activates the USB driver and then the USB bus pull-up on D+.
   * Note, a delay is inserted in order to not have to disconnect the cable
   * after a reset.
   */
  usbDisconnectBus(serusbcfg.usbp);
  chThdSleepMilliseconds(1500);
  usbStart(serusbcfg.usbp, &usbcfg);
  usbConnectBus(serusbcfg.usbp);

  /*
   * LSM303DLHC Object Initialization
   */
  lsm303dlhcObjectInit(&LSM303DLHCD1);

  lsm303dlhcStart(&LSM303DLHCD1, &lsm303dlhccfg);

  while (TRUE) {
    palToggleLine(LINE_LED3_RED);
    sensorReadRaw(&LSM303DLHCD1, rawdata);
    sensorReadCooked(&LSM303DLHCD1, cookeddata);
    chprintf(chp, "ACCELEROMETER DATA\r\n");
    for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
      chprintf(chp, "RAW-%c:%d\t\t", axesID[i], rawdata[i]);
    chprintf(chp, "\r\n");
    for(i = 0; i < LSM303DLHC_ACC_NUMBER_OF_AXES; i++)
      chprintf(chp, "COOKED-%c:%.3f g\t", axesID[i], cookeddata[i]);

    chprintf(chp, "\r\nCOMPSCOPE DATA\r\n");
    for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++)
      chprintf(chp, "RAW-%c:%d\t\t", axesID[i], rawdata[i + 3]);
    chprintf(chp, "\r\n");
    for(i = 0; i < LSM303DLHC_COMP_NUMBER_OF_AXES; i++)
      chprintf(chp, "COOKED-%c:%.3f Gauss\t", axesID[i], cookeddata[i + 3]);
    chprintf(chp, "\r\n");
    chThdSleepMilliseconds(100);
#if CHPRINTF_USE_ANSI_CODE
    chprintf(chp, "\033[2J\033[1;1H");
#endif
  }
}