Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
11
12#ifndef HMC5843_I2C_DEV
13#define HMC5843_I2C_DEV i2c2
14#endif
15
16void hmc5843_init(void)
17{
18 hmc5843.i2c_trans.status = I2CTransSuccess;
19 hmc5843.i2c_trans.slave_addr = HMC5843_ADDR;
20
21#ifndef HMC5843_NO_IRQ
23#endif
24}
25
26// blocking, only intended to be called for initialization
27static void send_config(void)
28{
29 hmc5843.i2c_trans.type = I2CTransTx;
30 hmc5843.i2c_trans.buf[0] = HMC5843_REG_CFGA; // set to rate to 50Hz
31 hmc5843.i2c_trans.buf[1] = 0x00 | (0x06 << 2);
32 hmc5843.i2c_trans.len_w = 2;
34 while (hmc5843.i2c_trans.status == I2CTransPending);
35
36 hmc5843.i2c_trans.type = I2CTransTx;
37 hmc5843.i2c_trans.buf[0] = HMC5843_REG_CFGB; // set to gain to 1 Gauss
38 hmc5843.i2c_trans.buf[1] = 0x01 << 5;
39 hmc5843.i2c_trans.len_w = 2;
41 while (hmc5843.i2c_trans.status == I2CTransPending);
42
43 hmc5843.i2c_trans.type = I2CTransTx;
44 hmc5843.i2c_trans.buf[0] = HMC5843_REG_MODE; // set to continuous mode
45 hmc5843.i2c_trans.buf[1] = 0x00;
46 hmc5843.i2c_trans.len_w = 2;
48 while (hmc5843.i2c_trans.status == I2CTransPending);
49
50}
51
52#ifdef HMC5843_NO_IRQ
53volatile uint8_t fake_mag_eoc = 0;
54static uint8_t mag_eoc(void)
55{
56 return fake_mag_eoc;
57}
58#endif
59
61{
62 if (hmc5843.i2c_trans.status == I2CTransFailed) {
63 hmc5843.sent_tx = 0;
64 hmc5843.sent_rx = 0;
65 }
66
67 if (hmc5843.i2c_trans.status == I2CTransRunning || hmc5843.i2c_trans.status == I2CTransPending) { return; }
68
69 if (hmc5843.initialized && mag_eoc() && !hmc5843.sent_tx && !hmc5843.sent_rx) {
71 hmc5843.i2c_trans.type = I2CTransTx;
72 hmc5843.i2c_trans.len_w = 1;
73 hmc5843.i2c_trans.buf[0] = 0x3;
75 hmc5843.sent_tx = 1;
76 return;
77 }
78 }
79
80 if (hmc5843.sent_tx) {
81 hmc5843.i2c_trans.type = I2CTransRx;
82 hmc5843.i2c_trans.len_r = 6;
83 hmc5843.i2c_trans.len_w = 1;
84 hmc5843.i2c_trans.buf[0] = 0x3;
86 hmc5843.sent_rx = 1;
87 hmc5843.sent_tx = 0;
88 return;
89 }
90
91 if (hmc5843.sent_rx && hmc5843.i2c_trans.status == I2CTransSuccess) {
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;
108 } else if (hmc5843.timeout++ > HMC5843_TIMEOUT && HMC5843_I2C_DEV.status == I2CIdle && i2c_idle(&HMC5843_I2C_DEV)) {
109#ifdef USE_HMC59843_ARCH_RESET
111#endif
112 hmc5843.i2c_trans.type = I2CTransTx;
113 hmc5843.i2c_trans.len_w = 1;
114 hmc5843.i2c_trans.buf[0] = 0x3;
115 i2c_submit(&HMC5843_I2C_DEV, &hmc5843.i2c_trans);
116 while (hmc5843.i2c_trans.status == I2CTransPending || hmc5843.i2c_trans.status == I2CTransRunning);
117
118 hmc5843.i2c_trans.type = I2CTransRx;
119 hmc5843.i2c_trans.len_r = 6;
120 i2c_submit(&HMC5843_I2C_DEV, &hmc5843.i2c_trans);
121 while (hmc5843.i2c_trans.status == I2CTransPending || hmc5843.i2c_trans.status == I2CTransRunning);
122 hmc5843.timeout = 0;
123 }
124
125#ifdef HMC5843_NO_IRQ
126 // < 50Hz
127 fake_mag_eoc = 1;
128#endif
129
130}
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition i2c.h:271
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition i2c.h:261
@ I2CIdle
Definition i2c.h:67
@ I2CTransRunning
transaction is currently ongoing
Definition i2c.h:57
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
@ I2CTransFailed
transaction failed
Definition i2c.h:59
@ I2CTransPending
transaction is pending in queue
Definition i2c.h:56
@ I2CTransRx
receive only transaction
Definition i2c.h:49
@ I2CTransTx
transmit only transaction
Definition i2c.h:48
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
#define HMC5843_REG_MODE
Definition hmc5843.h:60
#define HMC5843_REG_CFGA
Definition hmc5843.h:58
#define HMC5843_REG_CFGB
Definition hmc5843.h:59
void hmc5843_arch_reset(void)
#define HMC5843_ADDR
Definition hmc5843.h:55
void hmc5843_arch_init(void)
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
arch independent LED (Light Emitting Diodes) API
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.