Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules 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_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 }
#define HMC5843_REG_CFGA
Definition: hmc5843.h:58
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
void hmc5843_idle_task(void)
Definition: hmc5843.c:60
transaction successfully finished by I2C driver
Definition: i2c.h:57
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c.h:256
uint8_t data_available
Definition: hmc5843.h:34
void exti9_5_irq_handler(void)
uint32_t timeout
Definition: hmc5843.h:30
#define HMC5843_ADDR
Definition: hmc5843.h:55
#define HMC5843_TIMEOUT
Definition: hmc5843.c:5
Definition: i2c.h:66
#define HMC5843_REG_CFGB
Definition: hmc5843.h:59
#define HMC5843_I2C_DEV
Definition: hmc5843.c:13
static int mag_eoc(void)
Definition: hmc5843_arch.h:28
#define HMC5843_REG_MODE
Definition: hmc5843.h:60
uint8_t sent_tx
Definition: hmc5843.h:31
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
struct i2c_transaction i2c_trans
Definition: hmc5843.h:29
void hmc5843_init(void)
Definition: hmc5843.c:16
void hmc5843_arch_init(void)
Definition: hmc5843_arch.c:24
uint8_t sent_rx
Definition: hmc5843.h:32
static void send_config(void)
Definition: hmc5843.c:27
transaction failed
Definition: i2c.h:58
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
uint8_t initialized
Definition: hmc5843.h:33
unsigned char uint8_t
Definition: types.h:14
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
union Hmc5843::@314 data
transaction is currently ongoing
Definition: i2c.h:56
transmit only transaction
Definition: i2c.h:47
void hmc5843_periodic(void)
Definition: hmc5843.c:103
arch independent LED (Light Emitting Diodes) API
void hmc5843_arch_reset(void)
Definition: hmc5843_arch.c:53
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
#define bswap_16(x)
Definition: hmc5843.c:7
receive only transaction
Definition: i2c.h:48
transaction is pending in queue
Definition: i2c.h:55
struct Hmc5843 hmc5843
Definition: hmc5843.c:9
Architecture independent I2C (Inter-Integrated Circuit Bus) API.