Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
lisa_test_adxl345.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
24 /* lisa/L lisa/M
25  * ACC_DRDY PD2 PB2
26  * ACC_SS PB12 PB12
27  *
28  */
29 
30 
31 #include <stm32/gpio.h>
32 #include <stm32/flash.h>
33 #include <stm32/misc.h>
34 #include <stm32/exti.h>
35 #include <stm32/spi.h>
36 
37 #include BOARD_CONFIG
38 #include "mcu.h"
39 #include "mcu_periph/sys_time.h"
41 
42 #include "peripherals/adxl345.h"
43 #include "my_debug_servo.h"
44 #include "led.h"
45 
46 static inline void main_init( void );
47 static inline void main_periodic_task( void );
48 static inline void main_event_task( void );
49 
50 static inline void main_init_hw(void);
51 
52 void exti2_irq_handler(void);
53 
54 int main(void) {
55  main_init();
56 
57  while(1) {
61  }
62 
63  return 0;
64 }
65 
66 static inline void main_init( void ) {
67  mcu_init();
69  main_init_hw();
70 
71 }
72 
73 static void write_to_reg(uint8_t addr, uint8_t val);
74 static uint8_t read_fom_reg(uint8_t addr);
75 #define CONFIGURED 6
77 static volatile uint8_t acc_ready_for_read = FALSE;
78 static uint8_t values[6];
79 
80 #define AccUnselect() GPIOB->BSRR = GPIO_Pin_12
81 #define AccSelect() GPIOB->BRR = GPIO_Pin_12
82 #define AccToggleSelect() GPIOB->ODR ^= GPIO_Pin_12
83 
84 static void write_to_reg(uint8_t addr, uint8_t val) {
85 
86  AccSelect();
87  SPI_I2S_SendData(SPI2, addr);
88  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
89  SPI_I2S_SendData(SPI2, val);
90  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
91  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
92  AccUnselect();
93 
94 }
95 
97  AccSelect();
98  SPI_I2S_SendData(SPI2, (1<<7|addr));
99  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
100  SPI_I2S_SendData(SPI2, 0x00);
101  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
102  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
103  uint8_t ret = SPI_I2S_ReceiveData(SPI2);
104  AccUnselect();
105  return ret;
106 }
107 
108 static void read_data(void) {
109  AccSelect();
110  SPI_I2S_SendData(SPI2, (1<<7|1<<6|ADXL345_REG_DATA_X0));
111  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
112  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
113  uint8_t __attribute__ ((unused)) foo = SPI_I2S_ReceiveData(SPI2);
114 
115  SPI_I2S_SendData(SPI2, 0x00);
116  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
117  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
118  values[0] = SPI_I2S_ReceiveData(SPI2);
119  SPI_I2S_SendData(SPI2, 0x00);
120  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
121  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
122  values[1] = SPI_I2S_ReceiveData(SPI2);
123 
124  SPI_I2S_SendData(SPI2, 0x00);
125  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
126  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
127  values[2] = SPI_I2S_ReceiveData(SPI2);
128  SPI_I2S_SendData(SPI2, 0x00);
129  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
130  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
131  values[3] = SPI_I2S_ReceiveData(SPI2);
132 
133  SPI_I2S_SendData(SPI2, 0x00);
134  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
135  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
136  values[4] = SPI_I2S_ReceiveData(SPI2);
137  SPI_I2S_SendData(SPI2, 0x00);
138  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
139  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
140  values[5] = SPI_I2S_ReceiveData(SPI2);
141 
142  AccUnselect();
143 
144 }
145 
146 
147 static inline void main_periodic_task( void ) {
148 
149 
150  RunOnceEvery(10,
151  {
152  DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);
153  LED_PERIODIC();
154  });
155 
156  switch (acc_status) {
157  case 1:
158  {
159  /* read data rate */
160  // uint8_t bar = read_fom_reg(ADXL345_REG_BW_RATE);
161  }
162  /* set data rate to 800Hz */
164  break;
165  case 2:
166  /* switch to measurememnt mode */
168  break;
169  case 3:
170  /* enable data ready interrupt */
172  break;
173  case 4:
174  /* Enable full res and interrupt active low */
176  break;
177  case 5:
178  /* reads data once to bring interrupt line up */
179  read_data();
180  break;
181  case CONFIGURED:
182  // read_data();
183  break;
184  default:
185  break;
186  }
187 
189 
190 }
191 
192 
193 static inline void main_event_task( void ) {
194 
196  read_data();
198  int32_t iax = *((int16_t*)&values[0]);
199  int32_t iay = *((int16_t*)&values[2]);
200  int32_t iaz = *((int16_t*)&values[4]);
201  RunOnceEvery(10, {DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &iax, &iay, &iaz);});
202  }
203 
204 }
205 
206 static inline void main_init_hw( void ) {
207 
208  /* configure acc slave select */
209  /* set acc slave select as output and assert it ( on PB12) */
210  AccUnselect();
211  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
212  GPIO_InitTypeDef GPIO_InitStructure;
213  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
214  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
215  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
216  GPIO_Init(GPIOB, &GPIO_InitStructure);
217 
218  /* configure external interrupt exti2 on P(B/D)2( accel int ) */
219  RCC_APB2PeriphClockCmd(IMU_ACC_DRDY_RCC_GPIO | RCC_APB2Periph_AFIO, ENABLE);
220  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
221  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
222  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
223  GPIO_Init(IMU_ACC_DRDY_GPIO, &GPIO_InitStructure);
224  EXTI_InitTypeDef EXTI_InitStructure;
225  GPIO_EXTILineConfig(IMU_ACC_DRDY_GPIO_PORTSOURCE, GPIO_PinSource2);
226  EXTI_InitStructure.EXTI_Line = EXTI_Line2;
227  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
228  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
229  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
230  EXTI_Init(&EXTI_InitStructure);
231  NVIC_InitTypeDef NVIC_InitStructure;
232  NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
233  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
234  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
235  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
236  NVIC_Init(&NVIC_InitStructure);
237 
238 
239  /* Enable SPI2 Periph clock -------------------------------------------------*/
240  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
241 
242  /* Configure GPIOs: SCK, MISO and MOSI --------------------------------*/
243  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
244  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
245  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
246  GPIO_Init(GPIOB, &GPIO_InitStructure);
247 
248  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
249  SPI_Cmd(SPI2, ENABLE);
250 
251  /* configure SPI */
252  SPI_InitTypeDef SPI_InitStructure;
253  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
254  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
255  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
256  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
257  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
258  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
259  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
260  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
261  SPI_InitStructure.SPI_CRCPolynomial = 7;
262  SPI_Init(SPI2, &SPI_InitStructure);
263 
265 
266 }
267 
268 
269 void exti2_irq_handler(void) {
270 
271  /* clear EXTI */
272  if(EXTI_GetITStatus(EXTI_Line2) != RESET)
273  EXTI_ClearITPendingBit(EXTI_Line2);
274 
275  DEBUG_S4_TOGGLE();
276 
278 
279 
280 }
281 
static void write_to_reg(uint8_t addr, uint8_t val)
#define ADXL345_REG_DATA_FORMAT
Definition: adxl345.h:12
static uint8_t values[6]
#define ADXL345_REG_POWER_CTL
Definition: adxl345.h:10
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
#define FALSE
Definition: imu_chimu.h:141
static void read_data(void)
#define IMU_ACC_DRDY_GPIO
Definition: lisa_l_1.0.h:24
int main(void)
#define AccSelect()
Architecture independent timing functions.
uint16_t val[TCOUPLE_NB]
signed short int16_t
Definition: types.h:17
#define IMU_ACC_DRDY_RCC_GPIO
Definition: lisa_l_1.0.h:23
uint16_t foo
Definition: main_demo5.c:54
#define DEBUG_S4_TOGGLE()
#define DEBUG_SERVO2_INIT()
#define ADXL345_REG_BW_RATE
Definition: adxl345.h:9
#define AccUnselect()
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:35
static void main_event_task(void)
static bool_t sys_time_check_and_ack_timer(tid_t id)
Definition: sys_time.h:90
signed long int32_t
Definition: types.h:19
#define TRUE
Definition: imu_chimu.h:144
static void main_init_hw(void)
static void main_periodic_task(void)
static uint8_t read_fom_reg(uint8_t addr)
#define LED_PERIODIC()
Definition: led_hw.h:8
arch independent mcu ( Micro Controller Unit ) utilities
unsigned char uint8_t
Definition: types.h:14
void exti2_irq_handler(void)
#define ADXL345_REG_DATA_X0
Definition: adxl345.h:13
static volatile uint8_t acc_ready_for_read
static uint8_t acc_status
arch independent LED (Light Emitting Diodes) API
#define IMU_ACC_DRDY_GPIO_PORTSOURCE
Definition: lisa_l_1.0.h:25
#define ADXL345_REG_INT_ENABLE
Definition: adxl345.h:11
static void main_init(void)
#define RESET
Definition: humid_sht.h:40
#define CONFIGURED
__attribute__((always_inline))
Definition: i2c_arch.c:35
void mcu_init(void)
Definition: mcu.c:57