-
Notifications
You must be signed in to change notification settings - Fork 6
/
MXR6202.c
204 lines (156 loc) · 5.5 KB
/
MXR6202.c
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Includes */
#include "MXR6202.h"
#include "HAL_MXR6202.h"
#include "OSConfig.h"
#include "stm32f10x.h"
/**
* @defgroup MXR
* @{
*/
/** @defgroup MXR_I2C_Function
* @{
*/
/**
* @brief Initializes the I2C peripheral used to drive the MXR
* @param None
* @retval None
*/
void MXR_I2C_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable I2C and GPIO clocks */
RCC_APB1PeriphClockCmd(MXR_I2C_RCC_Periph, ENABLE);
RCC_APB2PeriphClockCmd(MXR_I2C_RCC_Port, ENABLE);
/* Configure I2C pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = MXR_I2C_SCL_Pin | MXR_I2C_SDA_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(MXR_I2C_Port, &GPIO_InitStructure);
/* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = MXR_I2C_Speed;
/* Apply I2C configuration after enabling it */
I2C_Init(MXR_I2C, &I2C_InitStructure);
/* I2C Peripheral Enable */
I2C_Cmd(MXR_I2C, ENABLE);
}
/**
* @brief Writes one byte to the MXR.
* @param slAddr : slave address MXR_A_I2C_ADDRESS or MXR_M_I2C_ADDRESS
* @param pBuffer : pointer to the buffer containing the data to be written to the MXR.
* @param WriteAddr : address of the register in which the data will be written
* @retval None
*/
void MXR_I2C_ByteWrite(u8 slAddr, u8* pBuffer, u8 WriteAddr)
{
/* Send START condition */
I2C_GenerateSTART(MXR_I2C, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MXR_Magn address for write */
I2C_Send7bitAddress(MXR_I2C, slAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send the MXR_Magn's internal address to write to */
I2C_SendData(MXR_I2C, WriteAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the byte to be written */
I2C_SendData(MXR_I2C, *pBuffer);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STOP condition */
I2C_GenerateSTOP(MXR_I2C, ENABLE);
}
/**
* @brief Reads a block of data from the MXR.
* @param slAddr : slave address MXR_A_I2C_ADDRESS or MXR_M_I2C_ADDRESS
* @param pBuffer : pointer to the buffer that receives the data read from the MXR.
* @param ReadAddr : MXR's internal address to read from.
* @param NumByteToRead : number of bytes to read from the MXR ( NumByteToRead >1 only for the Mgnetometer readinf).
* @retval None
*/
void MXR_I2C_BufferRead(u8 slAddr, u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
/* While the bus is busy */
while(I2C_GetFlagStatus(MXR_I2C, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(MXR_I2C, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MXR_Magn address for write */
I2C_Send7bitAddress(MXR_I2C, slAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(MXR_I2C, ENABLE);
/* Send the MXR_Magn's internal address to write to */
I2C_SendData(MXR_I2C, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(MXR_I2C, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_MODE_SELECT));
/* Send MXR address for read */
I2C_Send7bitAddress(MXR_I2C, slAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(MXR_I2C, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(MXR_I2C, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(MXR_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the MXR */
*pBuffer = I2C_ReceiveData(MXR_I2C);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(MXR_I2C, ENABLE);
}
void MXR_Init(MXR_ConfigTypeDef *MXR_Config_Struct)
{
u8 wake_up_byte=0;
MXR_I2C_ByteWrite(MXR_I2C_ADDRESS, &wake_up_byte, MXR_ONLY_REG_ADDR);
//75ms should be inserted here
}
u8 MXR_Read_RawData(u16* out)
{
u8 buffer[5]={0};
MXR_I2C_BufferRead(MXR_I2C_ADDRESS, buffer, MXR_ONLY_REG_ADDR, 5);
if(buffer[2]+buffer[4] == 0)
return 0;
out[0] = buffer[1] & 0x00ff;
out[0] <<= 8;
out[0] |= buffer[2];
out[1] = buffer[3] & 0x00ff;
out[1] <<= 8;
out[1] |= buffer[4];
return 1;
}
/**
* @brief Read MXR output register, and calculate the acceleration ACC=SENSITIVITY* (out_h*256+out_l)/16 (12 bit rappresentation)
* @param out : buffer to store data
* @retval None
*/
void MXR_Read_Acc(float* out)
{
}
/******************* (C) COPYRIGHT 2013 Skyworks Embedded System *****END OF FILE****/