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_hmc5843.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 #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 /*
31  * lisa/L lisa/M
32  * mag drdy PB5 PB5
33  *
34  */
35 
36 #include BOARD_CONFIG
37 #include "mcu.h"
38 #include "mcu_periph/sys_time.h"
40 #include "std.h"
41 #include "led.h"
42 
43 #include "mcu_periph/uart.h"
44 #include "mcu_periph/i2c.h"
45 #include "peripherals/hmc5843.h"
46 #include "my_debug_servo.h"
47 #include "math/pprz_algebra_int.h"
48 
49 static inline void main_init( void );
50 static inline void main_periodic_task( void );
51 static inline void main_event_task( void );
52 
53 static inline void main_init_hw(void);
54 static void send_config(void);
55 
56 static struct i2c_transaction i2c_trans;
59 
60 #define INITIALIZED 6
61 static uint8_t mag_state = 0;
62 static volatile uint8_t mag_ready_for_read = FALSE;
64 extern void exti9_5_irq_handler(void);
65 
66 
67 int main(void) {
68  main_init();
69 
70  while(1) {
74  }
75 
76  return 0;
77 }
78 
79 static inline void main_init( void ) {
80  mcu_init();
82  main_init_hw();
83 }
84 
85 static inline void main_periodic_task( void ) {
86  // LED_TOGGLE(6);
87  RunOnceEvery(10,
88  {
89  DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);
90  LED_PERIODIC();
91  });
92  RunOnceEvery(256,
93  {
94  uint16_t i2c2_ack_fail_cnt = i2c2.errors->ack_fail_cnt;
95  uint16_t i2c2_miss_start_stop_cnt = i2c2.errors->miss_start_stop_cnt;
96  uint16_t i2c2_arb_lost_cnt = i2c2.errors->arb_lost_cnt;
97  uint16_t i2c2_over_under_cnt = i2c2.errors->over_under_cnt;
98  uint16_t i2c2_pec_recep_cnt = i2c2.errors->pec_recep_cnt;
99  uint16_t i2c2_timeout_tlow_cnt = i2c2.errors->timeout_tlow_cnt;
100  uint16_t i2c2_smbus_alert_cnt = i2c2.errors->smbus_alert_cnt;
101  uint16_t i2c2_unexpected_event_cnt = i2c2.errors->unexpected_event_cnt;
102  uint32_t i2c2_last_unexpected_event = i2c2.errors->last_unexpected_event;
103  const uint8_t _bus2 = 2;
104  DOWNLINK_SEND_I2C_ERRORS(DefaultChannel, DefaultDevice,
105  &i2c2_ack_fail_cnt,
106  &i2c2_miss_start_stop_cnt,
107  &i2c2_arb_lost_cnt,
108  &i2c2_over_under_cnt,
109  &i2c2_pec_recep_cnt,
110  &i2c2_timeout_tlow_cnt,
111  &i2c2_smbus_alert_cnt,
112  &i2c2_unexpected_event_cnt,
113  &i2c2_last_unexpected_event,
114  &_bus2);
115  });
116  if (mag_state == 2) send_config();
117 
118 #if 0
119  switch (mag_state) {
120  case 2:
123  i2c_trans.buf[0] = HMC5843_REG_CFGA; // set to rate to 50Hz
124  i2c_trans.buf[1] = 0x00 | (0x06 << 2);
125  i2c_trans.len_w = 2;
126  i2c_submit(&i2c2,&i2c_trans);
127  break;
128  case 3:
131  i2c_trans.buf[0] = HMC5843_REG_CFGB; // set to gain to 1 Gauss
132  i2c_trans.buf[1] = 0x01<<5;
133  i2c_trans.len_w = 2;
134  i2c_submit(&i2c2,&i2c_trans);
135  break;
136  case 4:
139  i2c_trans.buf[0] = HMC5843_REG_MODE; // set to continuous mode
140  i2c_trans.buf[1] = 0x00;
141  i2c_trans.len_w = 2;
142  i2c_submit(&i2c2,&i2c_trans);
143  break;
144  case 5:
145  break;
146  case INITIALIZED:
147  // i2c2_receive(HMC5843_ADDR, 7, &i2c_done);
148  // reading_mag = TRUE;
149  break;
150  default:
151  break;
152  }
153 #endif
154  // if (mag_state == 4) mag_state=1;
155 
157 
158 }
159 
160 
161 static inline void main_event_task( void ) {
162 
164  /* read mag */
167  i2c_trans.len_r = 7;
168  i2c_submit(&i2c2,&i2c_trans);
169  reading_mag = TRUE;
171  }
172 
174  RunOnceEvery(10,
175  {
176  int16_t mx = i2c_trans.buf[0]<<8 | i2c_trans.buf[1];
177  int16_t my = i2c_trans.buf[2]<<8 | i2c_trans.buf[3];
178  int16_t mz = i2c_trans.buf[4]<<8 | i2c_trans.buf[5];
179  struct Int32Vect3 m;
180  VECT3_ASSIGN(m, mx, my, mz);
181  DOWNLINK_SEND_IMU_MAG_RAW(DefaultChannel, DefaultDevice, &m.x, &m.y, &m.z);
182  // uint8_t tmp[8];
183  // memcpy(tmp, i2c2.buf, 8);
184  // DOWNLINK_SEND_DEBUG(DefaultChannel, DefaultDevice, 8, tmp);
185  }
186  );
187  reading_mag = FALSE;
188  }
189 
190 }
191 
192 
193 
194 static void send_config(void) {
195 
196  t1.type = I2CTransTx;
198  t1.buf[0] = HMC5843_REG_CFGA; // set to rate to 50Hz
199  t1.buf[1] = 0x00 | (0x06 << 2);
200  t1.len_w = 2;
201  i2c_submit(&i2c2,&t1);
202 
203  t2.type = I2CTransTx;
205  t2.buf[0] = HMC5843_REG_CFGB; // set to gain to 1 Gauss
206  t2.buf[1] = 0x01<<5;
207  t2.len_w = 2;
208  i2c_submit(&i2c2,&t2);
209 
212  i2c_trans.buf[0] = HMC5843_REG_MODE; // set to continuous mode
213  i2c_trans.buf[1] = 0x00;
214  i2c_trans.len_w = 2;
215  i2c_submit(&i2c2,&i2c_trans);
216 
217 }
218 
219 
220 
221 
222 
223 
224 
225 
226 static inline void main_init_hw( void ) {
227  /* set mag ss as floating input (on PC12) = shorted to I2C2 sda ----------*/
228  /* set mag reset as floating input (on PC13) = shorted to I2C2 scl ----------*/
229  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
230  GPIO_InitTypeDef GPIO_InitStructure;
231  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;
232  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
233  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
234  GPIO_Init(GPIOC, &GPIO_InitStructure);
235 
236  /* configure external interrupt exti5 on PB5( mag int ) */
237  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
238  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
239  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
240  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
241  GPIO_Init(GPIOB, &GPIO_InitStructure);
242 
243  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5);
244  EXTI_InitTypeDef EXTI_InitStructure;
245  EXTI_InitStructure.EXTI_Line = EXTI_Line5;
246  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
247  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
248  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
249  EXTI_Init(&EXTI_InitStructure);
250 
251  NVIC_InitTypeDef NVIC_InitStructure;
252  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
253  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
254  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
255  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
256  NVIC_Init(&NVIC_InitStructure);
257 
260 
261 }
262 
263 
264 
265 
267  /* clear EXTI */
268  if(EXTI_GetITStatus(EXTI_Line5) != RESET)
269  EXTI_ClearITPendingBit(EXTI_Line5);
270 
272 }
unsigned short uint16_t
Definition: types.h:16
#define HMC5843_REG_CFGA
Definition: hmc5843.h:60
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
static uint8_t reading_mag
static uint8_t mag_state
static void main_periodic_task(void)
#define HMC5843_ADDR
Definition: hmc5843.h:57
#define INITIALIZED
#define HMC5843_REG_CFGB
Definition: hmc5843.h:61
uint8_t slave_addr
Definition: i2c.h:43
#define HMC5843_REG_MODE
Definition: hmc5843.h:62
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
#define FALSE
Definition: imu_chimu.h:141
static volatile uint8_t mag_ready_for_read
int main(void)
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:321
#define DEBUG_SERVO1_INIT()
Architecture independent timing functions.
void exti9_5_irq_handler(void)
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
#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
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 VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:99
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
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
static void main_init(void)
unsigned char uint8_t
Definition: types.h:14
static struct i2c_transaction i2c_trans
enum I2CTransactionType type
Definition: i2c.h:42
arch independent LED (Light Emitting Diodes) API
Definition: i2c.h:10
static void main_event_task(void)
struct i2c_transaction t2
static void send_config(void)
#define RESET
Definition: humid_sht.h:40
Definition: i2c.h:9
static void main_init_hw(void)
struct i2c_transaction t1
void mcu_init(void)
Definition: mcu.c:57
Paparazzi fixed point algebra.