forked from ivyknob/bno055_stm32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbno055_stm32.h
109 lines (95 loc) · 3.17 KB
/
bno055_stm32.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef BNO055_STM32_H_
#define BNO055_STM32_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "i2c.h"
#ifdef FREERTOS_ENABLED
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#endif
#include "bno055.h"
I2C_HandleTypeDef *_bno055_i2c_port;
void bno055_assignI2C(I2C_HandleTypeDef *hi2c_device) {
_bno055_i2c_port = hi2c_device;
}
void bno055_delay(int time) {
#ifdef FREERTOS_ENABLED
osDelay(time);
#else
HAL_Delay(time);
#endif
}
void bno055_writeData(uint8_t reg, uint8_t data) {
uint8_t txdata[2] = {reg, data};
uint8_t status;
status = HAL_I2C_Master_Transmit(_bno055_i2c_port, BNO055_I2C_ADDR << 1,
txdata, sizeof(txdata), 10);
if (status == HAL_OK) {
return;
}
if (status == HAL_ERROR) {
printf("HAL_I2C_Master_Transmit HAL_ERROR\r\n");
} else if (status == HAL_TIMEOUT) {
printf("HAL_I2C_Master_Transmit HAL_TIMEOUT\r\n");
} else if (status == HAL_BUSY) {
printf("HAL_I2C_Master_Transmit HAL_BUSY\r\n");
} else {
printf("Unknown status data %d", status);
}
uint32_t error = HAL_I2C_GetError(_bno055_i2c_port);
if (error == HAL_I2C_ERROR_NONE) {
return;
} else if (error == HAL_I2C_ERROR_BERR) {
printf("HAL_I2C_ERROR_BERR\r\n");
} else if (error == HAL_I2C_ERROR_ARLO) {
printf("HAL_I2C_ERROR_ARLO\r\n");
} else if (error == HAL_I2C_ERROR_AF) {
printf("HAL_I2C_ERROR_AF\r\n");
} else if (error == HAL_I2C_ERROR_OVR) {
printf("HAL_I2C_ERROR_OVR\r\n");
} else if (error == HAL_I2C_ERROR_DMA) {
printf("HAL_I2C_ERROR_DMA\r\n");
} else if (error == HAL_I2C_ERROR_TIMEOUT) {
printf("HAL_I2C_ERROR_TIMEOUT\r\n");
}
HAL_I2C_StateTypeDef state = HAL_I2C_GetState(_bno055_i2c_port);
if (state == HAL_I2C_STATE_RESET) {
printf("HAL_I2C_STATE_RESET\r\n");
} else if (state == HAL_I2C_STATE_READY) {
printf("HAL_I2C_STATE_RESET\r\n");
} else if (state == HAL_I2C_STATE_BUSY) {
printf("HAL_I2C_STATE_BUSY\r\n");
} else if (state == HAL_I2C_STATE_BUSY_TX) {
printf("HAL_I2C_STATE_BUSY_TX\r\n");
} else if (state == HAL_I2C_STATE_BUSY_RX) {
printf("HAL_I2C_STATE_BUSY_RX\r\n");
} else if (state == HAL_I2C_STATE_LISTEN) {
printf("HAL_I2C_STATE_LISTEN\r\n");
} else if (state == HAL_I2C_STATE_BUSY_TX_LISTEN) {
printf("HAL_I2C_STATE_BUSY_TX_LISTEN\r\n");
} else if (state == HAL_I2C_STATE_BUSY_RX_LISTEN) {
printf("HAL_I2C_STATE_BUSY_RX_LISTEN\r\n");
} else if (state == HAL_I2C_STATE_ABORT) {
printf("HAL_I2C_STATE_ABORT\r\n");
} else if (state == HAL_I2C_STATE_TIMEOUT) {
printf("HAL_I2C_STATE_TIMEOUT\r\n");
} else if (state == HAL_I2C_STATE_ERROR) {
printf("HAL_I2C_STATE_ERROR\r\n");
}
// while (HAL_I2C_GetState(_bno055_i2c_port) != HAL_I2C_STATE_READY) {}
// return;
}
void bno055_readData(uint8_t reg, uint8_t *data, uint8_t len) {
HAL_I2C_Master_Transmit(_bno055_i2c_port, BNO055_I2C_ADDR << 1, ®, 1,
100);
HAL_I2C_Master_Receive(_bno055_i2c_port, BNO055_I2C_ADDR << 1, data, len,
100);
// HAL_I2C_Mem_Read(_bno055_i2c_port, BNO055_I2C_ADDR_LO<<1, reg,
// I2C_MEMADD_SIZE_8BIT, data, len, 100);
}
#ifdef __cplusplus
}
#endif
#endif // BNO055_STM32_H_