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
hmc5843.c
Go to the documentation of this file.
1 #include "peripherals/hmc5843.h"
2 #include "mcu_periph/i2c.h"
3 #include "led.h"
4 
5 #define HMC5843_TIMEOUT 100
6 
7 #define bswap_16(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
8 
9 struct Hmc5843 hmc5843;
10 void exti9_5_irq_handler(void);
11 
12 #ifndef HMC5843_I2C_DEVICE
13 #define HMC5843_I2C_DEVICE i2c2
14 #endif
15 
16 void hmc5843_init(void)
17 {
20 
21 #ifndef HMC5843_NO_IRQ
23 #endif
24 }
25 
26 // blocking, only intended to be called for initialization
27 static void send_config(void)
28 {
30  hmc5843.i2c_trans.buf[0] = HMC5843_REG_CFGA; // set to rate to 50Hz
31  hmc5843.i2c_trans.buf[1] = 0x00 | (0x06 << 2);
35 
37  hmc5843.i2c_trans.buf[0] = HMC5843_REG_CFGB; // set to gain to 1 Gauss
38  hmc5843.i2c_trans.buf[1] = 0x01<<5;
42 
44  hmc5843.i2c_trans.buf[0] = HMC5843_REG_MODE; // set to continuous mode
45  hmc5843.i2c_trans.buf[1] = 0x00;
49 
50 }
51 
52 #ifdef HMC5843_NO_IRQ
53 volatile uint8_t fake_mag_eoc = 0;
54 static uint8_t mag_eoc(void)
55 {
56  return fake_mag_eoc;
57 }
58 #endif
59 
61 {
63  hmc5843.sent_tx = 0;
64  hmc5843.sent_rx = 0;
65  }
66 
68 
73  hmc5843.i2c_trans.buf[0] = 0x3;
75  hmc5843.sent_tx = 1;
76  return;
77  }
78  }
79 
80  if (hmc5843.sent_tx) {
84  hmc5843.i2c_trans.buf[0] = 0x3;
86  hmc5843.sent_rx = 1;
87  hmc5843.sent_tx = 0;
88  return;
89  }
90 
92  hmc5843.sent_rx = 0;
93  hmc5843.sent_tx = 0;
94  hmc5843.timeout = 0;
96  memcpy(hmc5843.data.buf, (const void*) hmc5843.i2c_trans.buf, 6);
97  for (int i = 0; i < 3; i++) {
99  }
100  }
101 }
102 
104 {
105  if (!hmc5843.initialized) {
106  send_config();
109 #ifdef USE_HMC59843_ARCH_RESET
111 #endif
113  hmc5843.i2c_trans.len_w = 1;
114  hmc5843.i2c_trans.buf[0] = 0x3;
117 
119  hmc5843.i2c_trans.len_r = 6;
122  hmc5843.timeout = 0;
123  }
124 
125 #ifdef HMC5843_NO_IRQ
126  // < 50Hz
127  fake_mag_eoc = 1;
128 #endif
129 
130 }
#define HMC5843_REG_CFGA
Definition: hmc5843.h:60
uint32_t timeout
Definition: hmc5843.h:32
uint8_t initialized
Definition: hmc5843.h:35
void hmc5843_idle_task(void)
Definition: hmc5843.c:60
void exti9_5_irq_handler(void)
Definition: hmc5843_arch.c:66
#define HMC5843_ADDR
Definition: hmc5843.h:57
uint8_t sent_tx
Definition: hmc5843.h:33
#define HMC5843_TIMEOUT
Definition: hmc5843.c:5
#define HMC5843_REG_CFGB
Definition: hmc5843.h:61
bool_t i2c_idle(struct i2c_periph *p)
Definition: i2c_arch.c:317
#define HMC5843_I2C_DEVICE
Definition: hmc5843.c:13
static int mag_eoc(void)
Definition: hmc5843_arch.h:30
uint8_t slave_addr
Definition: i2c.h:43
#define HMC5843_REG_MODE
Definition: hmc5843.h:62
Definition: i2c.h:23
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:321
void hmc5843_init(void)
Definition: hmc5843.c:16
void hmc5843_arch_init(void)
Definition: hmc5843_arch.c:24
struct i2c_transaction i2c_trans
Definition: hmc5843.h:31
uint8_t sent_rx
Definition: hmc5843.h:34
static void send_config(void)
Definition: hmc5843.c:27
enum I2CTransactionStatus status
Definition: i2c.h:47
uint8_t data_available
Definition: hmc5843.h:36
#define TRUE
Definition: imu_chimu.h:144
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
int16_t value[3]
Definition: hmc5843.h:39
unsigned char uint8_t
Definition: types.h:14
enum I2CTransactionType type
Definition: i2c.h:42
void hmc5843_periodic(void)
Definition: hmc5843.c:103
arch independent LED (Light Emitting Diodes) API
Definition: i2c.h:10
uint8_t buf[7]
Definition: hmc5843.h:38
void hmc5843_arch_reset(void)
Definition: hmc5843_arch.c:61
#define bswap_16(x)
Definition: hmc5843.c:7
Definition: i2c.h:9
union Hmc5843::@15 data
struct Hmc5843 hmc5843
Definition: hmc5843.c:9