Paparazzi UAS  v6.2_unstable
Paparazzi is a free software Unmanned Aircraft System.
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_DEV
13 #define HMC5843_I2C_DEV 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 
70  if (HMC5843_I2C_DEV.status == I2CIdle && i2c_idle(&HMC5843_I2C_DEV)) {
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;
95  hmc5843.data_available = true;
96  memcpy(hmc5843.data.buf, (const void *) hmc5843.i2c_trans.buf, 6);
97  for (int i = 0; i < 3; i++) {
98  hmc5843.data.value[i] = bswap_16(hmc5843.data.value[i]);
99  }
100  }
101 }
102 
104 {
105  if (!hmc5843.initialized) {
106  send_config();
107  hmc5843.initialized = true;
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 }
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
hmc5843_idle_task
void hmc5843_idle_task(void)
Definition: hmc5843.c:60
exti9_5_irq_handler
void exti9_5_irq_handler(void)
Hmc5843::timeout
uint32_t timeout
Definition: hmc5843.h:30
mag_eoc
static int mag_eoc(void)
Definition: hmc5843_arch.h:28
HMC5843_ADDR
#define HMC5843_ADDR
Definition: hmc5843.h:55
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
Hmc5843::data_available
uint8_t data_available
Definition: hmc5843.h:34
HMC5843_REG_CFGA
#define HMC5843_REG_CFGA
Definition: hmc5843.h:58
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
Hmc5843::sent_tx
uint8_t sent_tx
Definition: hmc5843.h:31
Hmc5843::i2c_trans
struct i2c_transaction i2c_trans
Definition: hmc5843.h:29
i2c_idle
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c.h:256
HMC5843_TIMEOUT
#define HMC5843_TIMEOUT
Definition: hmc5843.c:5
hmc5843_init
void hmc5843_init(void)
Definition: hmc5843.c:16
hmc5843_arch_init
void hmc5843_arch_init(void)
Definition: hmc5843_arch.c:24
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
HMC5843_REG_MODE
#define HMC5843_REG_MODE
Definition: hmc5843.h:60
HMC5843_REG_CFGB
#define HMC5843_REG_CFGB
Definition: hmc5843.h:59
HMC5843_I2C_DEV
#define HMC5843_I2C_DEV
Definition: hmc5843.c:13
I2CTransRx
@ I2CTransRx
receive only transaction
Definition: i2c.h:48
Hmc5843
Definition: hmc5843.h:28
I2CTransRunning
@ I2CTransRunning
transaction is currently ongoing
Definition: i2c.h:56
send_config
static void send_config(void)
Definition: hmc5843.c:27
Hmc5843::sent_rx
uint8_t sent_rx
Definition: hmc5843.h:32
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
led.h
arch independent LED (Light Emitting Diodes) API
I2CIdle
@ I2CIdle
Definition: i2c.h:66
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
hmc5843.h
Hmc5843::data
union Hmc5843::@307 data
hmc5843_periodic
void hmc5843_periodic(void)
Definition: hmc5843.c:103
Hmc5843::initialized
uint8_t initialized
Definition: hmc5843.h:33
I2CTransPending
@ I2CTransPending
transaction is pending in queue
Definition: i2c.h:55
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
bswap_16
#define bswap_16(x)
Definition: hmc5843.c:7
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
i2c.h
hmc5843
struct Hmc5843 hmc5843
Definition: hmc5843.c:9
hmc5843_arch_reset
void hmc5843_arch_reset(void)
Definition: hmc5843_arch.c:53