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_aspirin.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 #include <stm32/gpio.h>
25 #include <stm32/flash.h>
26 #include <stm32/misc.h>
27 #include <stm32/exti.h>
28 #include <stm32/spi.h>
29 
30 #include BOARD_CONFIG
31 #include "mcu.h"
32 #include "mcu_periph/sys_time.h"
34 #include "led.h"
35 
36 #include "peripherals/itg3200.h"
37 #include "peripherals/hmc5843.h"
38 
39 static inline void main_init( void );
40 static inline void main_periodic_task( void );
41 static inline void main_event_task( void );
42 
43 static inline void main_init_hw(void);
44 
45 static volatile uint8_t gyro_ready_for_read;
46 
47 extern void exti2_irq_handler(void);
48 extern void exti3_irq_handler(void);
49 extern void exti4_irq_handler(void);
50 
51 int main(void) {
52  main_init();
53 
54  while(1) {
58  }
59 
60  return 0;
61 }
62 
63 static inline void main_init( void ) {
64  mcu_init();
66  main_init_hw();
67 
69 
70 }
71 
72 static uint8_t foo=0;
73 
74 
75 #define AccUnselect() GPIOB->BSRR = GPIO_Pin_12
76 #define AccSelect() GPIOB->BRR = GPIO_Pin_12
77 #define AccToggleSelect() GPIOB->ODR ^= GPIO_Pin_12
78 
79 
80 static inline void main_periodic_task( void ) {
81  // LED_TOGGLE(6);
82  RunOnceEvery(10,
83  {
84  DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);
85  LED_PERIODIC();
86  });
87 
88 
89  switch (foo) {
90  case 2:
91  /* set gyro range to 2000deg/s and low pass at 256Hz */
92  i2c2.buf[0] = ITG3200_REG_DLPF_FS;
93  i2c2.buf[1] = 0x03;
94  i2c2_transmit(ITG3200_ADDR, 2, &i2c_done);
95  break;
96  case 3:
97  /* switch to gyroX clock */
98  i2c2.buf[0] = ITG3200_REG_PWR_MGM;
99  i2c2.buf[1] = 0x01;
100  i2c2_transmit(ITG3200_ADDR, 2, &i2c_done);
101  break;
102  case 4:
103  /* enable interrupt on data ready, idle hight */
104  i2c2.buf[0] = ITG3200_REG_INT_CFG;
105  i2c2.buf[1] = (0x01 | 0x01<<7);
106  i2c2_transmit(ITG3200_ADDR, 2, &i2c_done);
107  break;
108  case 5:
109  /* set mag rate to 50Hz */
110  i2c2.buf[0] = HMC5843_REG_CFGA;
111  i2c2.buf[1] = 0x00 | (0x06 << 2);
112  i2c2_transmit(HMC5843_ADDR, 2, &i2c_done);
113  break;
114  case 6:
115  /* Set mag gain to 1 Gauss */
116  i2c2.buf[0] = HMC5843_REG_CFGB;
117  i2c2.buf[1] = 0x01<<5;
118  i2c2_transmit(HMC5843_ADDR, 2, &i2c_done);
119  break;
120  case 7:
121  /* set mag to continuous measurements */
122  i2c2.buf[0] = HMC5843_REG_MODE;
123  i2c2.buf[1] = 0x00;
124  i2c2_transmit(HMC5843_ADDR, 2, &i2c_done);
125  break;
126  case 8:
127  /* reads 8 bytes from address 0x1b */
128  // i2c2.buf[0] = ITG3200_REG_TEMP_OUT_H;
129  // i2c2_transceive(ITG3200_ADDR,1, 8, &i2c_done);
130  break;
131  default:
132  break;
133  }
134 
135  if (foo< 8) foo++;
136 
137 
138  // AccToggleSelect();
139 
140 }
141 
142 
143 static inline void main_event_task( void ) {
144 
145 
146 }
147 
148 static inline void main_init_hw( void ) {
149 
150  /* set mag ss as output and assert it (on PC12) = sda ------------------------------*/
151  /* mag drdy (on PB5) = mag int */
152  /* set mag reset as output and assert it (on PC13) = scl ------------------------------*/
153  /* set eeprom ss as output and assert it (on PC14) = gyro int ------------------------------*/
154  GPIOC->BSRR = GPIO_Pin_12;
155  GPIOC->BSRR = GPIO_Pin_13;
156  GPIOC->BSRR = GPIO_Pin_14;
157 
158  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
159  GPIO_InitTypeDef GPIO_InitStructure;
160  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14;
161  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
162  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
163  GPIO_Init(GPIOC, &GPIO_InitStructure);
164 
165  /* configure acc slave select */
166  /* set acc slave select as output and assert it ( on PB12) */
167  AccUnselect();
168  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
169  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
170  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
171  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
172  GPIO_Init(GPIOB, &GPIO_InitStructure);
173 
174  /* configure external interrupt exti4 on PD2( accel int ) */
175  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
176  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
177  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
178  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
179  GPIO_Init(GPIOD, &GPIO_InitStructure);
180  EXTI_InitTypeDef EXTI_InitStructure;
181  GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2);
182  EXTI_InitStructure.EXTI_Line = EXTI_Line4;
183  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
184  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
185  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
186  EXTI_Init(&EXTI_InitStructure);
187  NVIC_InitTypeDef NVIC_InitStructure;
188  NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
189  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
190  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
191  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
192 
193 
194 
195  /* configure external interrupt exti2 on PC14( gyro int ) */
196  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
197  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
198  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
199  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
200  GPIO_Init(GPIOC, &GPIO_InitStructure);
201 
202  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource14);
203  EXTI_InitStructure.EXTI_Line = EXTI_Line2;
204  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
205  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
206  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
207  EXTI_Init(&EXTI_InitStructure);
208 
209  NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
210  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
211  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
212  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
213 
214  NVIC_Init(&NVIC_InitStructure);
215 
216 
217 
218  /* configure external interrupt exti3 on PB5( mag int ) */
219  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
220  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
221  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
222  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
223  GPIO_Init(GPIOB, &GPIO_InitStructure);
224 
225  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
226  EXTI_InitStructure.EXTI_Line = EXTI_Line3;
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 
232  NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
233  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
234  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
235  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
236 
237  NVIC_Init(&NVIC_InitStructure);
238 
239 
240  /* Enable SPI2 Periph clock -------------------------------------------------*/
241  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
242 
243  /* Configure GPIOs: SCK, MISO and MOSI --------------------------------*/
244  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
245  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
246  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
247  GPIO_Init(GPIOB, &GPIO_InitStructure);
248 
249  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
250  SPI_Cmd(SPI2, ENABLE);
251 
252  /* configure SPI */
253  SPI_InitTypeDef SPI_InitStructure;
254  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
255  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
256  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
257  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
258  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
259  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
260  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
261  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
262  SPI_InitStructure.SPI_CRCPolynomial = 7;
263  SPI_Init(SPI2, &SPI_InitStructure);
264 
265 
266 }
267 
268 void exti2_irq_handler(void) {
269  /* clear EXTI */
270  if(EXTI_GetITStatus(EXTI_Line2) != RESET)
271  EXTI_ClearITPendingBit(EXTI_Line2);
272 
273  AccToggleSelect();
274 #if 0
276 
277  if (gyro_ready_for_read && i2c_done && foo>=5) {
278  /* read gyros */
279  /* reads 8 bytes from address 0x1b */
280  i2c2.buf[0] = ITG3200_REG_TEMP_OUT_H;
281  i2c2_transceive(ITG3200_ADDR,1, 8, &i2c_done);
283  }
284 #endif
285 }
286 
287 void exti3_irq_handler(void) {
288  /* clear EXTI */
289  if(EXTI_GetITStatus(EXTI_Line3) != RESET)
290  EXTI_ClearITPendingBit(EXTI_Line3);
291 
292  // AccToggleSelect();
293 
294 
295 }
296 
297 
298 void exti4_irq_handler(void) {
299  /* clear EXTI */
300  if(EXTI_GetITStatus(EXTI_Line4) != RESET)
301  EXTI_ClearITPendingBit(EXTI_Line4);
302 
303  // AccToggleSelect();
304 
305 
306 }
307 
#define ITG3200_REG_INT_CFG
Definition: itg3200.h:12
#define HMC5843_REG_CFGA
Definition: hmc5843.h:60
int main(void)
static void main_init(void)
#define AccToggleSelect()
#define HMC5843_ADDR
Definition: hmc5843.h:57
#define ITG3200_ADDR
Definition: itg3200.h:5
void exti4_irq_handler(void)
static void main_event_task(void)
#define HMC5843_REG_CFGB
Definition: hmc5843.h:61
#define HMC5843_REG_MODE
Definition: hmc5843.h:62
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
#define FALSE
Definition: imu_chimu.h:141
static uint8_t foo
#define AccUnselect()
Architecture independent timing functions.
#define ITG3200_REG_TEMP_OUT_H
Definition: itg3200.h:14
void exti3_irq_handler(void)
static void main_init_hw(void)
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:35
static bool_t sys_time_check_and_ack_timer(tid_t id)
Definition: sys_time.h:90
#define TRUE
Definition: imu_chimu.h:144
#define LED_PERIODIC()
Definition: led_hw.h:8
arch independent mcu ( Micro Controller Unit ) utilities
unsigned char uint8_t
Definition: types.h:14
static volatile uint8_t gyro_ready_for_read
static uint8_t i2c_done
arch independent LED (Light Emitting Diodes) API
#define ITG3200_REG_DLPF_FS
Definition: itg3200.h:11
#define RESET
Definition: humid_sht.h:40
void mcu_init(void)
Definition: mcu.c:57
#define ITG3200_REG_PWR_MGM
Definition: itg3200.h:22
static void main_periodic_task(void)
void exti2_irq_handler(void)