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_itg3200.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2010 The Paparazzi Team
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  * gyro-drdy PC14
26  *
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/uart.h"
40 #include "mcu_periph/i2c.h"
41 #include "mcu_periph/sys_time.h"
43 #include "std.h"
44 #include "math/pprz_algebra_int.h"
45 
46 #include "peripherals/itg3200.h"
47 #include "my_debug_servo.h"
48 #include "led.h"
49 
50 static inline void main_init( void );
51 static inline void main_periodic_task( void );
52 static inline void main_event_task( void );
53 
54 static inline void main_init_hw(void);
55 
56 static struct i2c_transaction i2c_trans;
57 #define INITIALIZED 6
58 static uint8_t gyro_state = 0;
59 static volatile uint8_t gyro_ready_for_read = FALSE;
61 
62 void exti15_10_irq_handler(void);
63 
64 int main(void) {
65  main_init();
66 
67  while(1) {
71  }
72 
73  return 0;
74 }
75 
76 static inline void main_init( void ) {
77  mcu_init();
79  main_init_hw();
80 }
81 
82 static inline void main_periodic_task( void ) {
83  // LED_TOGGLE(6);
84  RunOnceEvery(10,
85  {
86  DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);
87  LED_PERIODIC();
88  });
89  RunOnceEvery(256, {
90  uint16_t i2c2_ack_fail_cnt = i2c2.errors->ack_fail_cnt;
91  uint16_t i2c2_miss_start_stop_cnt = i2c2.errors->miss_start_stop_cnt;
92  uint16_t i2c2_arb_lost_cnt = i2c2.errors->arb_lost_cnt;
93  uint16_t i2c2_over_under_cnt = i2c2.errors->over_under_cnt;
94  uint16_t i2c2_pec_recep_cnt = i2c2.errors->pec_recep_cnt;
95  uint16_t i2c2_timeout_tlow_cnt = i2c2.errors->timeout_tlow_cnt;
96  uint16_t i2c2_smbus_alert_cnt = i2c2.errors->smbus_alert_cnt;
97  uint16_t i2c2_unexpected_event_cnt = i2c2.errors->unexpected_event_cnt;
98  uint32_t i2c2_last_unexpected_event = i2c2.errors->last_unexpected_event;
99  const uint8_t _bus2 = 2;
100  DOWNLINK_SEND_I2C_ERRORS(DefaultChannel, DefaultDevice,
101  &i2c2_ack_fail_cnt,
102  &i2c2_miss_start_stop_cnt,
103  &i2c2_arb_lost_cnt,
104  &i2c2_over_under_cnt,
105  &i2c2_pec_recep_cnt,
106  &i2c2_timeout_tlow_cnt,
107  &i2c2_smbus_alert_cnt,
108  &i2c2_unexpected_event_cnt,
109  &i2c2_last_unexpected_event,
110  &_bus2);
111  });
112 
113  switch (gyro_state) {
114 
115  case 1:
116  /* dummy one byte write for testing */
120  i2c_trans.len_w = 1;
121  i2c_submit(&i2c2,&i2c_trans);
122  break;
123  case 2:
124  /* set gyro range to 2000deg/s and low pass at 256Hz */
128  i2c_trans.buf[1] = (0x03<<3);
129  i2c_trans.len_w = 2;
130  i2c_submit(&i2c2,&i2c_trans);
131  break;
132  case 3:
133  /* set sample rate to 533Hz */
137  i2c_trans.buf[1] = 0x0E;
138  i2c_trans.len_w = 2;
139  i2c_submit(&i2c2,&i2c_trans);
140  break;
141  case 4:
142  /* switch to gyroX clock */
146  i2c_trans.buf[1] = 0x01;
147  i2c_trans.len_w = 2;
148  i2c_submit(&i2c2,&i2c_trans);
149  break;
150  case 5:
151  /* enable interrupt on data ready, idle hight */
155  i2c_trans.buf[1] = (0x01 | 0x01<<7);
156  i2c_trans.len_w = 2;
157  i2c_submit(&i2c2,&i2c_trans);
158  break;
159  case INITIALIZED:
160  /* reads 8 bytes from address 0x1b */
161  // i2c2.buf[0] = ITG3200_REG_TEMP_OUT_H;
162  // i2c2_transceive(ITG3200_ADDR,1, 8, &i2c_done);
163  // reading_gyro = TRUE;
164  default:
165  break;
166  }
167 
168  // if (gyro_state == 1) gyro_state = 0;
170 
171 }
172 
173 
174 #if 0
175 
176 #endif
177 
178 static inline void main_event_task( void ) {
179 
182  /* reads 8 bytes from address 0x1b */
185  i2c_trans.len_w = 1;
186  i2c_trans.len_r = 8;
187  i2c_submit(&i2c2,&i2c_trans);
188  // i2c2.buf[0] = ITG3200_REG_GYRO_XOUT_H;
189  // i2c2_transceive(ITG3200_ADDR,1, 6, &i2c_done);
191  reading_gyro = TRUE;
192  }
193 
194  if (reading_gyro &&
196  // DEBUG_S5_ON();
198  int16_t tgp, tgq, tgr;
199 
200  int16_t ttemp = i2c_trans.buf[0]<<8 | i2c_trans.buf[1];
201 #if 1
202  tgp = i2c_trans.buf[2]<<8 | i2c_trans.buf[3];
203  tgq = i2c_trans.buf[4]<<8 | i2c_trans.buf[5];
204  tgr = i2c_trans.buf[6]<<8 | i2c_trans.buf[7];
205 #endif
206 #if 0
207  tgp = __REVSH(*(int16_t*)(i2c_trans.buf+2));
208  tgq = __REVSH(*(int16_t*)(i2c_trans.buf+4));
209  tgr = __REVSH(*(int16_t*)(i2c_trans.buf+6));
210 #endif
211 #if 0
212  MyByteSwap16(*(int16_t*)(i2c_trans.buf+2), tgp);
213  MyByteSwap16(*(int16_t*)(i2c_trans.buf+4), tgq);
214  MyByteSwap16(*(int16_t*)(i2c_trans.buf+6), tgr);
215 #endif
216  struct Int32Rates g;
217  RATES_ASSIGN(g, tgp, tgq, tgr);
218  RunOnceEvery(10,
219  {
220  DOWNLINK_SEND_IMU_GYRO_RAW(DefaultChannel, DefaultDevice, &g.p, &g.q, &g.r);
221 
222  uint8_t tmp[8];
223  memcpy(tmp, i2c_trans.buf, 8);
224  DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, 8, tmp);
225 
226 
227  });
228  // DEBUG_S5_OFF();
229  }
230 }
231 
232 static inline void main_init_hw( void ) {
233  /* set mag ss as floating input (on PC12) = shorted to sda ------------------------------*/
234  /* set mag reset as floating input (on PC13) = shorted to scl ------------------------------*/
235  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
236  GPIO_InitTypeDef GPIO_InitStructure;
237  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;
238  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
239  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
240  GPIO_Init(GPIOC, &GPIO_InitStructure);
241 
242 
243  /* set "eeprom ss" as floating input (on PC14) = gyro int ------------------------------*/
244  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
245  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
246  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
247  GPIO_Init(GPIOC, &GPIO_InitStructure);
248 
249  /* configure external interrupt exti15_10 on PC14( gyro int ) */
250  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
251  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
252  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
253  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
254  GPIO_Init(GPIOC, &GPIO_InitStructure);
255 
256  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource14);
257  EXTI_InitTypeDef EXTI_InitStructure;
258  EXTI_InitStructure.EXTI_Line = EXTI_Line14;
259  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
260  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
261  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
262  EXTI_Init(&EXTI_InitStructure);
263 
264  NVIC_InitTypeDef NVIC_InitStructure;
265  NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
266  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
267  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
268  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
269  NVIC_Init(&NVIC_InitStructure);
270 
273 
274 }
275 
276 
278 
279  // DEBUG_S4_ON();
280 
281  /* clear EXTI */
282  if(EXTI_GetITStatus(EXTI_Line14) != RESET)
283  EXTI_ClearITPendingBit(EXTI_Line14);
284 
285  // DEBUG_S4_TOGGLE();
286 
288 
289  // DEBUG_S4_OFF();
290 
291 }
#define ITG3200_REG_INT_CFG
Definition: itg3200.h:12
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define ITG3200_REG_SMPLRT_DIV
Definition: itg3200.h:10
int32_t p
in rad/s^2 with INT32_RATE_FRAC
static void main_init_hw(void)
#define ITG3200_ADDR
Definition: itg3200.h:5
static struct i2c_transaction i2c_trans
static uint8_t gyro_state
uint8_t slave_addr
Definition: i2c.h:43
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
#define FALSE
Definition: imu_chimu.h:141
void exti15_10_irq_handler(void)
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:321
static void main_init(void)
#define DEBUG_SERVO1_INIT()
Architecture independent timing functions.
unsigned long uint32_t
Definition: types.h:18
#define ITG3200_REG_TEMP_OUT_H
Definition: itg3200.h:14
int main(void)
signed short int16_t
Definition: types.h:17
static void main_periodic_task(void)
#define INITIALIZED
#define DEBUG_SERVO2_INIT()
enum I2CTransactionStatus status
Definition: i2c.h:47
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:35
angular rates
static bool_t sys_time_check_and_ack_timer(tid_t id)
Definition: sys_time.h:90
#define TRUE
Definition: imu_chimu.h:144
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
static void main_event_task(void)
uint8_t len_w
Definition: i2c.h:45
uint16_t len_r
Definition: i2c.h:44
#define LED_PERIODIC()
Definition: led_hw.h:8
arch independent mcu ( Micro Controller Unit ) utilities
unsigned char uint8_t
Definition: types.h:14
#define MyByteSwap16(in, out)
Definition: mcu_arch.h:34
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:296
static volatile uint8_t gyro_ready_for_read
enum I2CTransactionType type
Definition: i2c.h:42
int32_t q
in rad/s^2 with INT32_RATE_FRAC
arch independent LED (Light Emitting Diodes) API
int32_t r
in rad/s^2 with INT32_RATE_FRAC
#define ITG3200_REG_DLPF_FS
Definition: itg3200.h:11
static uint8_t reading_gyro
#define RESET
Definition: humid_sht.h:40
Definition: i2c.h:9
void mcu_init(void)
Definition: mcu.c:57
Paparazzi fixed point algebra.
#define ITG3200_REG_PWR_MGM
Definition: itg3200.h:22