28 #define WRCTRL_REG1 0x20
29 #define RDCTRL_REG1 0xA0
31 #define DUMMY_BYTE 0xA5
56 void MEMS_WakeUp(
void )
63 MEMS_ChipSelect( ACTIVE );
66 MEMS_SendByte( RDCTRL_REG1 );
68 reg_val = MEMS_SendByte( DUMMY_BYTE );
71 MEMS_ChipSelect( STDBY );
75 reg_val = reg_val | 0xC0 | 0x30;
78 MEMS_ChipSelect( ACTIVE );
81 MEMS_SendByte( WRCTRL_REG1 );
82 MEMS_SendByte( reg_val );
85 MEMS_ChipSelect( STDBY );
101 u32 MEMS_ReadOutXY(
void )
112 MEMS_ChipSelect( ACTIVE );
115 MEMS_SendByte( RDOUTXL );
118 OutXL = MEMS_SendByte( DUMMY_BYTE );
121 OutXH = MEMS_SendByte( DUMMY_BYTE );
124 OutYL = MEMS_SendByte( DUMMY_BYTE );
127 OutYH = MEMS_SendByte( DUMMY_BYTE );
130 OutZL = MEMS_SendByte( DUMMY_BYTE );
133 OutZH = MEMS_SendByte( DUMMY_BYTE );
135 MEMS_Info.OutX = OutXL + ( OutXH << 8 );
136 MEMS_Info.OutY = OutYL + ( OutYH << 8 );
137 MEMS_Info.OutZ = OutZL + ( OutZH << 8 );
140 MEMS_ChipSelect( STDBY );
142 MEMS_Info.OutX_F4 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F4 >> 2 ) );
143 MEMS_Info.OutY_F4 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F4 >> 2 ) );
144 MEMS_Info.OutZ_F4 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F4 >> 2 ) );
146 MEMS_Info.OutX_F16 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F16 >> 4 ) );
147 MEMS_Info.OutY_F16 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F16 >> 4 ) );
148 MEMS_Info.OutZ_F16 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F16 >> 4 ) );
150 MEMS_Info.OutX_F64 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F64 >> 6 ) );
151 MEMS_Info.OutY_F64 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F64 >> 6 ) );
152 MEMS_Info.OutZ_F64 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F64 >> 6 ) );
154 MEMS_Info.OutX_F256 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F256 >> 8 ) );
155 MEMS_Info.OutY_F256 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F256 >> 8 ) );
156 MEMS_Info.OutZ_F256 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F256 >> 8 ) );
158 return ( MEMS_Info.OutX + ( MEMS_Info.OutY << 16 ) );
173 void MEMS_ChipSelect( u8 State )
176 GPIO_WriteBit( GPIOX_MEMS_CS, GPIO_PIN_MEMS_CS, ( BitAction )State );
194 u8 MEMS_SendByte( u8 byte )
197 while ( SPI_I2S_GetFlagStatus( SPIX_MEMS, SPI_I2S_FLAG_TXE ) == RESET );
200 SPI_I2S_SendData( SPIX_MEMS, byte );
203 while ( SPI_I2S_GetFlagStatus( SPIX_MEMS, SPI_I2S_FLAG_RXNE ) == RESET );
206 return SPI_I2S_ReceiveData( SPIX_MEMS );
224 NODEBUG2
void MEMS_Init(
void )
226 SPI_InitTypeDef SPI_InitStructure;
227 GPIO_InitTypeDef GPIO_InitStructure;
230 RCC_APBxPERIPH_MEMS_CLOCK_CMD( RCC_APBxPERIPH_SPIX_MEMS, ENABLE );
231 RCC_PERIPH_GPIO_CLOCK_CMD( RCC_APBxPERIPH_GPIOX_MEMS_CTRL, ENABLE );
232 RCC_PERIPH_GPIO_CLOCK_CMD( RCC_APBxPERIPH_GPIOX_MEMS_CS, ENABLE );
237 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_SCK | GPIO_PIN_MEMS_MISO | GPIO_PIN_MEMS_MOSI;
238 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
239 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
240 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
241 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
242 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
245 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_SCK_SOURCE, MEMS_SPI_SCK_AF);
246 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_MISO_SOURCE, MEMS_SPI_MISO_AF);
247 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_MOSI_SOURCE, MEMS_SPI_MOSI_AF);
254 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_SCK | GPIO_PIN_MEMS_MISO | GPIO_PIN_MEMS_MOSI;
255 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
256 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
257 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
258 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
259 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
262 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_SCK_SOURCE, MEMS_SPI_SCK_AF);
263 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_MISO_SOURCE, MEMS_SPI_MISO_AF);
264 GPIO_PinAFConfig(GPIOX_MEMS_CTRL, MEMS_SPI_MOSI_SOURCE, MEMS_SPI_MOSI_AF);
269 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_SCK | GPIO_PIN_MEMS_MOSI;
270 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
271 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
272 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
275 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_MISO;
276 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
277 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
278 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
284 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_CS;
286 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
287 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
288 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
289 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
292 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
293 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
294 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
295 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
297 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
298 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
301 GPIO_Init( GPIOX_MEMS_CS, &GPIO_InitStructure );
304 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
305 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
306 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
307 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
308 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
309 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
310 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
311 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
312 SPI_InitStructure.SPI_CRCPolynomial = 7;
314 SPI_Init( SPIX_MEMS, &SPI_InitStructure );
317 SPI_Cmd( SPIX_MEMS, ENABLE );
319 if ( MEMS_ReadID() != 0x3A )
324 for ( i = 0 ; i < 17 ; i++ )
327 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_SCK | GPIO_PIN_MEMS_MOSI;
329 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
330 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
331 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
332 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
335 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
336 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
337 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
338 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
340 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
341 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
342 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
345 GPIO_WriteBit( GPIOX_MEMS_CTRL, GPIO_PIN_MEMS_MOSI, HIGH );
346 MEMS_ChipSelect( ACTIVE );
348 GPIO_WriteBit( GPIOX_MEMS_CTRL, GPIO_PIN_MEMS_SCK, LOW );
349 GPIO_WriteBit( GPIOX_MEMS_CTRL, GPIO_PIN_MEMS_SCK, HIGH );
350 MEMS_ChipSelect( HIGH );
353 GPIO_InitStructure.GPIO_Pin = GPIO_PIN_MEMS_SCK | GPIO_PIN_MEMS_MOSI;
355 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
356 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
357 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
358 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
361 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
362 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
363 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
364 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
366 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
367 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
368 GPIO_Init( GPIOX_MEMS_CTRL, &GPIO_InitStructure );
371 if ( MEMS_ReadID() == 0x3A )
386 MEMS_Info.OutX_F4 = MEMS_Info.OutX_F16 = MEMS_Info.OutX_F64 = MEMS_Info.OutX_F256 = MEMS_Info.OutX;
387 MEMS_Info.OutY_F4 = MEMS_Info.OutY_F16 = MEMS_Info.OutY_F64 = MEMS_Info.OutY_F256 = MEMS_Info.OutY;
388 MEMS_Info.OutZ_F4 = MEMS_Info.OutZ_F16 = MEMS_Info.OutZ_F64 = MEMS_Info.OutZ_F256 = MEMS_Info.OutZ;
410 u8 MEMS_ReadID(
void )
415 MEMS_ChipSelect( ACTIVE );
418 MEMS_SendByte( RDID );
421 Temp = MEMS_SendByte( DUMMY_BYTE );
424 MEMS_ChipSelect( STDBY );