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