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