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