|
| 1 | +/* |
| 2 | + * This file is part of the libopencm3 project. |
| 3 | + * |
| 4 | + * Copyright (C) 2015 Marco Russi <[email protected]> |
| 5 | + * |
| 6 | + * This library is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +/* ---------------- Inclusions ----------------- */ |
| 22 | +#include <libopencm3/stm32/rcc.h> |
| 23 | +#include <libopencm3/stm32/gpio.h> |
| 24 | +#include <libopencm3/stm32/spi.h> |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +/* ---------------- Local Defines ----------------- */ |
| 30 | + |
| 31 | +/* LIS3DSH registers addresses */ |
| 32 | +#define ADD_REG_WHO_AM_I 0x0F |
| 33 | +#define ADD_REG_CTRL_4 0x20 |
| 34 | +#define ADD_REG_OUT_X_L 0x28 |
| 35 | +#define ADD_REG_OUT_X_H 0x29 |
| 36 | +#define ADD_REG_OUT_Y_L 0x2A |
| 37 | +#define ADD_REG_OUT_Y_H 0x2B |
| 38 | +#define ADD_REG_OUT_Z_L 0x2C |
| 39 | +#define ADD_REG_OUT_Z_H 0x2D |
| 40 | + |
| 41 | +/* WHO AM I register default value */ |
| 42 | +#define UC_WHO_AM_I_DEFAULT_VALUE 0x3F |
| 43 | + |
| 44 | +/* ADD_REG_CTRL_4 register configuration value: |
| 45 | + * X,Y,Z axis enabled and 400Hz of output data rate */ |
| 46 | +#define UC_ADD_REG_CTRL_4_CFG_VALUE 0x77 |
| 47 | + |
| 48 | +/* Sensitivity for 2G range [mg/digit] */ |
| 49 | +#define SENS_2G_RANGE_MG_PER_DIGIT ((float)0.06) |
| 50 | + |
| 51 | +/* LED threshold value in mg */ |
| 52 | +#define LED_TH_MG (1000) /* 1000mg (1G) */ |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +/* ---------------- Local Macros ----------------- */ |
| 58 | + |
| 59 | +/* set read single command. Attention: command must be 0x3F at most */ |
| 60 | +#define SET_READ_SINGLE_CMD(x) (x | 0x80) |
| 61 | +/* set read multiple command. Attention: command must be 0x3F at most */ |
| 62 | +#define SET_READ_MULTI_CMD(x) (x | 0xC0) |
| 63 | +/* set write single command. Attention: command must be 0x3F at most */ |
| 64 | +#define SET_WRITE_SINGLE_CMD(x) (x & (~(0xC0))) |
| 65 | +/* set write multiple command. Attention: command must be 0x3F at most */ |
| 66 | +#define SET_WRITE_MULTI_CMD(x) (x & (~(0x80)) \ |
| 67 | + x |= 0x40) |
| 68 | + |
| 69 | +/* Macros for turning LEDs ON */ |
| 70 | +#define LED_GREEN_ON() (gpio_set(GPIOD, GPIO12)) |
| 71 | +#define LED_ORANGE_ON() (gpio_set(GPIOD, GPIO13)) |
| 72 | +#define LED_RED_ON() (gpio_set(GPIOD, GPIO14)) |
| 73 | +#define LED_BLUE_ON() (gpio_set(GPIOD, GPIO15)) |
| 74 | + |
| 75 | +/* Macros for turning LEDs OFF */ |
| 76 | +#define LED_GREEN_OFF() (gpio_clear(GPIOD, GPIO12)) |
| 77 | +#define LED_ORANGE_OFF() (gpio_clear(GPIOD, GPIO13)) |
| 78 | +#define LED_RED_OFF() (gpio_clear(GPIOD, GPIO14)) |
| 79 | +#define LED_BLUE_OFF() (gpio_clear(GPIOD, GPIO15)) |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +/* ------------- Local functions prototypes --------------- */ |
| 85 | + |
| 86 | +static void gpio_setup(void); |
| 87 | +static void spi_setup(void); |
| 88 | +static void lis3dsh_init(void); |
| 89 | +static void lis3dsh_write_reg(int, int); |
| 90 | +static int lis3dsh_read_reg(int); |
| 91 | +static inline int two_compl_to_int16(int); |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +/* ------------ Local functions implementation ----------- */ |
| 97 | + |
| 98 | +/* Function to setup all used GPIOs */ |
| 99 | +static void gpio_setup(void) |
| 100 | +{ |
| 101 | + /* Enable GPIOD clock. */ |
| 102 | + rcc_periph_clock_enable(RCC_GPIOD); |
| 103 | + |
| 104 | + /* Set GPIO12/13/14/15 (in GPIO port D) to 'output push-pull'. */ |
| 105 | + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, |
| 106 | + GPIO12 | GPIO13 | GPIO14 | GPIO15); |
| 107 | + |
| 108 | + /* LIS3DSH pins map: |
| 109 | + PA5 - SPI1_SCK |
| 110 | + PA6 - SPI1_MISO |
| 111 | + PA7 - SPI1_MOSI |
| 112 | + PE3 - CS_SPI |
| 113 | + */ |
| 114 | + |
| 115 | + /* Enable GPIOA clock. */ |
| 116 | + rcc_periph_clock_enable(RCC_GPIOA); |
| 117 | + /* set SPI pins as CLK, MOSI, MISO */ |
| 118 | + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, |
| 119 | + GPIO5 | GPIO6 | GPIO7); |
| 120 | + /* Push Pull, Speed 100 MHz */ |
| 121 | + gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, |
| 122 | + GPIO5 | GPIO6 | GPIO7); |
| 123 | + /* Alternate Function: SPI1 */ |
| 124 | + gpio_set_af(GPIOA, GPIO_AF5, GPIO5 | GPIO6 | GPIO7); |
| 125 | + |
| 126 | + /* Enable GPIOE clock. */ |
| 127 | + rcc_periph_clock_enable(RCC_GPIOE); |
| 128 | + /* set CS as OUTPUT */ |
| 129 | + gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3); |
| 130 | + /* Push Pull, Speed 100 MHz */ |
| 131 | + gpio_set_output_options(GPIOE, GPIO_OTYPE_PP, |
| 132 | + GPIO_OSPEED_100MHZ, GPIO3); |
| 133 | + /* set CS high */ |
| 134 | + gpio_set(GPIOE, GPIO3); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +/* Function to setup the SPI1 */ |
| 139 | +static void spi_setup(void) |
| 140 | +{ |
| 141 | + /* Enable SPI1 clock. */ |
| 142 | + rcc_periph_clock_enable(RCC_SPI1); |
| 143 | + |
| 144 | + /* reset SPI1 */ |
| 145 | + spi_reset(SPI1); |
| 146 | + /* init SPI1 master */ |
| 147 | + spi_init_master(SPI1, |
| 148 | + SPI_CR1_BAUDRATE_FPCLK_DIV_64, |
| 149 | + SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, |
| 150 | + SPI_CR1_CPHA_CLK_TRANSITION_1, |
| 151 | + SPI_CR1_DFF_8BIT, |
| 152 | + SPI_CR1_MSBFIRST); |
| 153 | + /* enable SPI1 first */ |
| 154 | + spi_enable(SPI1); |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +/* Function to initialise the LIS3DSH */ |
| 159 | +static void lis3dsh_init(void) |
| 160 | +{ |
| 161 | + int int_reg_value; |
| 162 | + |
| 163 | + /* init SPI1 */ |
| 164 | + spi_setup(); |
| 165 | + |
| 166 | + /* get WHO AM I value */ |
| 167 | + int_reg_value = lis3dsh_read_reg(ADD_REG_WHO_AM_I); |
| 168 | + |
| 169 | + /* if WHO AM I value is the expected one */ |
| 170 | + if (int_reg_value == UC_WHO_AM_I_DEFAULT_VALUE) { |
| 171 | + /* set output data rate to 400 Hz and enable X,Y,Z axis */ |
| 172 | + lis3dsh_write_reg(ADD_REG_CTRL_4, UC_ADD_REG_CTRL_4_CFG_VALUE); |
| 173 | + /* verify written value */ |
| 174 | + int_reg_value = lis3dsh_read_reg(ADD_REG_CTRL_4); |
| 175 | + /* if written value is different */ |
| 176 | + if (int_reg_value != UC_ADD_REG_CTRL_4_CFG_VALUE) { |
| 177 | + /* ERROR: stay here... */ |
| 178 | + while (1); |
| 179 | + } |
| 180 | + } else { |
| 181 | + /* ERROR: stay here... */ |
| 182 | + while (1); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +/* Function to write a register to LIS3DSH through SPI */ |
| 188 | +static void lis3dsh_write_reg(int reg, int data) |
| 189 | +{ |
| 190 | + /* set CS low */ |
| 191 | + gpio_clear(GPIOE, GPIO3); |
| 192 | + /* discard returned value */ |
| 193 | + spi_xfer(SPI1, SET_WRITE_SINGLE_CMD(reg)); |
| 194 | + spi_xfer(SPI1, data); |
| 195 | + /* set CS high */ |
| 196 | + gpio_set(GPIOE, GPIO3); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +/* Function to read a register from LIS3DSH through SPI */ |
| 201 | +static int lis3dsh_read_reg(int reg) |
| 202 | +{ |
| 203 | + int reg_value; |
| 204 | + /* set CS low */ |
| 205 | + gpio_clear(GPIOE, GPIO3); |
| 206 | + reg_value = spi_xfer(SPI1, SET_READ_SINGLE_CMD(reg)); |
| 207 | + reg_value = spi_xfer(SPI1, 0xFF); |
| 208 | + /* set CS high */ |
| 209 | + gpio_set(GPIOE, GPIO3); |
| 210 | + |
| 211 | + return reg_value; |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | +/* Transform a two's complement value to 16-bit int value */ |
| 216 | +static inline int two_compl_to_int16(int two_compl_value) |
| 217 | +{ |
| 218 | + int int16_value = 0; |
| 219 | + |
| 220 | + /* conversion */ |
| 221 | + if (two_compl_value > 32768) { |
| 222 | + int16_value = -(((~two_compl_value) & 0xFFFF) + 1); |
| 223 | + } else { |
| 224 | + int16_value = two_compl_value; |
| 225 | + } |
| 226 | + |
| 227 | + return int16_value; |
| 228 | +} |
| 229 | + |
| 230 | + |
| 231 | +/* Main function */ |
| 232 | +int main(void) |
| 233 | +{ |
| 234 | + int value_mg_x, value_mg_y, value_mg_z; |
| 235 | + |
| 236 | + /* setup all GPIOs */ |
| 237 | + gpio_setup(); |
| 238 | + |
| 239 | + /* initialise LIS3DSH */ |
| 240 | + lis3dsh_init(); |
| 241 | + |
| 242 | + /* infinite loop */ |
| 243 | + while (1) { |
| 244 | + /* get X, Y, Z values */ |
| 245 | + value_mg_x = ((lis3dsh_read_reg(ADD_REG_OUT_X_H) << 8) | |
| 246 | + lis3dsh_read_reg(ADD_REG_OUT_X_L)); |
| 247 | + value_mg_y = ((lis3dsh_read_reg(ADD_REG_OUT_Y_H) << 8) | |
| 248 | + lis3dsh_read_reg(ADD_REG_OUT_Y_L)); |
| 249 | + value_mg_z = ((lis3dsh_read_reg(ADD_REG_OUT_Z_H) << 8) | |
| 250 | + lis3dsh_read_reg(ADD_REG_OUT_Z_L)); |
| 251 | + |
| 252 | + /* transform X value from two's complement to 16-bit int */ |
| 253 | + value_mg_x = two_compl_to_int16(value_mg_x); |
| 254 | + /* convert X absolute value to mg value */ |
| 255 | + value_mg_x = value_mg_x * SENS_2G_RANGE_MG_PER_DIGIT; |
| 256 | + |
| 257 | + /* transform Y value from two's complement to 16-bit int */ |
| 258 | + value_mg_y = two_compl_to_int16(value_mg_y); |
| 259 | + /* convert Y absolute value to mg value */ |
| 260 | + value_mg_y = value_mg_y * SENS_2G_RANGE_MG_PER_DIGIT; |
| 261 | + |
| 262 | + /* transform Z value from two's complement to 16-bit int */ |
| 263 | + value_mg_z = two_compl_to_int16(value_mg_z); |
| 264 | + /* convert Z absolute value to mg value */ |
| 265 | + value_mg_z = value_mg_z * SENS_2G_RANGE_MG_PER_DIGIT; |
| 266 | + |
| 267 | + /* set X related LEDs according to specified threshold */ |
| 268 | + if (value_mg_x >= LED_TH_MG) { |
| 269 | + LED_BLUE_OFF(); |
| 270 | + LED_ORANGE_OFF(); |
| 271 | + LED_GREEN_OFF(); |
| 272 | + LED_RED_ON(); |
| 273 | + } else if (value_mg_x <= -LED_TH_MG) { |
| 274 | + LED_BLUE_OFF(); |
| 275 | + LED_ORANGE_OFF(); |
| 276 | + LED_RED_OFF(); |
| 277 | + LED_GREEN_ON(); |
| 278 | + } |
| 279 | + |
| 280 | + /* set Y related LEDs according to specified threshold */ |
| 281 | + if (value_mg_y >= LED_TH_MG) { |
| 282 | + LED_BLUE_OFF(); |
| 283 | + LED_RED_OFF(); |
| 284 | + LED_GREEN_OFF(); |
| 285 | + LED_ORANGE_ON(); |
| 286 | + } else if (value_mg_y <= -LED_TH_MG) { |
| 287 | + LED_RED_OFF(); |
| 288 | + LED_GREEN_OFF(); |
| 289 | + LED_ORANGE_OFF(); |
| 290 | + LED_BLUE_ON(); |
| 291 | + } |
| 292 | + |
| 293 | + /* set Z related LEDs according to specified threshold */ |
| 294 | + if (value_mg_z >= LED_TH_MG) { |
| 295 | + LED_BLUE_ON(); |
| 296 | + LED_ORANGE_ON(); |
| 297 | + LED_RED_ON(); |
| 298 | + LED_GREEN_ON(); |
| 299 | + } else if (value_mg_z <= -LED_TH_MG) { |
| 300 | + LED_BLUE_OFF(); |
| 301 | + LED_ORANGE_OFF(); |
| 302 | + LED_RED_OFF(); |
| 303 | + LED_GREEN_OFF(); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + return 0; |
| 308 | +} |
| 309 | + |
| 310 | + |
| 311 | +/* End of file */ |
0 commit comments