1 |
582 |
jeremybenn |
/********************* (C) COPYRIGHT 2007 RAISONANCE S.A.S. *******************/
|
2 |
|
|
/**
|
3 |
|
|
*
|
4 |
|
|
* @file mems.c
|
5 |
|
|
* @brief Mems Initialization and management
|
6 |
|
|
* @author FL
|
7 |
|
|
* @date 07/2007
|
8 |
|
|
* @version 1.1
|
9 |
|
|
* @date 10/2007
|
10 |
|
|
* @version 1.5 various corrections reported by Ron Miller
|
11 |
|
|
*
|
12 |
|
|
**/
|
13 |
|
|
/******************************************************************************/
|
14 |
|
|
|
15 |
|
|
/* Includes ------------------------------------------------------------------*/
|
16 |
|
|
#include "circle.h"
|
17 |
|
|
|
18 |
|
|
/// @cond Internal
|
19 |
|
|
|
20 |
|
|
/* Private define ------------------------------------------------------------*/
|
21 |
|
|
#define RDOUTXL 0xE8 /*!< Multiple Read from OUTXL */
|
22 |
|
|
#define WRCTRL_REG1 0x20 /*!< Single Write CTRL_REG */
|
23 |
|
|
#define RDCTRL_REG1 0xA0 /*!< Single Read CTRL_REG */
|
24 |
|
|
#define RDID 0x8F /*!< Single Read WHO_AM_I */
|
25 |
|
|
#define LOW 0x00 /*!< ChipSelect line low */
|
26 |
|
|
#define HIGH 0x01 /*!< ChipSelect line high */
|
27 |
|
|
#define DUMMY_BYTE 0xA5
|
28 |
|
|
#define MEMS_DIVIDER 1
|
29 |
|
|
#define MEMS_TESTING_DIVIDER 101
|
30 |
|
|
#define MARGIN 500
|
31 |
|
|
#define DELAY_REACT 20
|
32 |
|
|
#define MIN_REACT 15
|
33 |
|
|
#define DIV_REACT 10
|
34 |
|
|
#define GRAD_SHOCK 200000
|
35 |
|
|
|
36 |
|
|
/* Private variables ---------------------------------------------------------*/
|
37 |
|
|
tMEMS_Info MEMS_Info = {0}; // structure definition in circle.h
|
38 |
|
|
int TestingActive = 0;
|
39 |
|
|
int StartingFromResetOrShockCounter = 1000;
|
40 |
|
|
int TimeCounterForDoubleClick = 0;
|
41 |
|
|
int TimeLastShock = 0;
|
42 |
|
|
static int divider = 0;
|
43 |
|
|
static Rotate_H12_V_Match_TypeDef previous_Screen_Orientation;
|
44 |
|
|
u32 Gradient2;
|
45 |
|
|
|
46 |
|
|
//Filtering
|
47 |
|
|
unsigned N_filtering = 0;
|
48 |
|
|
|
49 |
|
|
//Gradient
|
50 |
|
|
s16 GradX = 0;
|
51 |
|
|
s16 GradY = 0;
|
52 |
|
|
s16 GradZ = 0;
|
53 |
|
|
|
54 |
|
|
// Pointer move:
|
55 |
|
|
// each coordinate (X, Y and Z) is described by 3 variables where suffix means:
|
56 |
|
|
// f = flag to indicate that a move has been done. Cleared by the Ptr Manager when acknowledged.
|
57 |
|
|
// i = amplitude of the move (Grad / 10)
|
58 |
|
|
// t = delay to accept the counter reaction
|
59 |
|
|
int fMovePtrX;
|
60 |
|
|
int iMovePtrX;
|
61 |
|
|
int tMovePtrX;
|
62 |
|
|
int fMovePtrY;
|
63 |
|
|
int iMovePtrY;
|
64 |
|
|
int tMovePtrY;
|
65 |
|
|
int fMovePtrZ;
|
66 |
|
|
int iMovePtrZ;
|
67 |
|
|
int tMovePtrZ;
|
68 |
|
|
|
69 |
|
|
s16 XInit = 0;
|
70 |
|
|
s16 YInit = 0;
|
71 |
|
|
s16 ZInit = 0;
|
72 |
|
|
|
73 |
|
|
/* Private function prototypes -----------------------------------------------*/
|
74 |
|
|
static void MEMS_ChipSelect( u8 State );
|
75 |
|
|
static u8 MEMS_SendByte( u8 byte );
|
76 |
|
|
static void MEMS_WriteEnable( void );
|
77 |
|
|
static u32 MEMS_ReadOutXY( void );
|
78 |
|
|
static void MEMS_WakeUp( void );
|
79 |
|
|
|
80 |
|
|
/* Private functions ---------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
/*******************************************************************************
|
83 |
|
|
*
|
84 |
|
|
* MEMS_WakeUp
|
85 |
|
|
*
|
86 |
|
|
*******************************************************************************/
|
87 |
|
|
/**
|
88 |
|
|
* Wake Up Mems.
|
89 |
|
|
*
|
90 |
|
|
**/
|
91 |
|
|
/******************************************************************************/
|
92 |
|
|
static void MEMS_WakeUp( void )
|
93 |
|
|
{
|
94 |
|
|
u8 reg_val;
|
95 |
|
|
|
96 |
|
|
/* read RDCTRL_REG1 */
|
97 |
|
|
|
98 |
|
|
/* Chip Select low */
|
99 |
|
|
MEMS_ChipSelect( LOW );
|
100 |
|
|
|
101 |
|
|
/* Send "RDCTRL_REG1" instruction */
|
102 |
|
|
MEMS_SendByte( RDCTRL_REG1 );
|
103 |
|
|
|
104 |
|
|
reg_val = MEMS_SendByte( DUMMY_BYTE );
|
105 |
|
|
|
106 |
|
|
/* Chip Select high */
|
107 |
|
|
MEMS_ChipSelect( HIGH );
|
108 |
|
|
|
109 |
|
|
/* SET P0:P1 to '11' */
|
110 |
|
|
/* 0xC0 to wake up and 0x30 for full speed frequency (640 Hz). */
|
111 |
|
|
reg_val = reg_val | 0xC0 | 0x30;
|
112 |
|
|
|
113 |
|
|
/* Chip Select low */
|
114 |
|
|
MEMS_ChipSelect( LOW );
|
115 |
|
|
|
116 |
|
|
/* Send "WRCTRL_REG1" instruction */
|
117 |
|
|
MEMS_SendByte( WRCTRL_REG1 );
|
118 |
|
|
MEMS_SendByte( reg_val );
|
119 |
|
|
|
120 |
|
|
/* Chip Select high */
|
121 |
|
|
MEMS_ChipSelect( HIGH );
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/*******************************************************************************
|
125 |
|
|
*
|
126 |
|
|
* MEMS_ReadOutXY
|
127 |
|
|
*
|
128 |
|
|
*******************************************************************************/
|
129 |
|
|
/**
|
130 |
|
|
* Reads X and Y Out.
|
131 |
|
|
*
|
132 |
|
|
* @return An unsigned 32 bit word with the highest 16 bits containing the Y
|
133 |
|
|
* and the lowest 16 bits the X.
|
134 |
|
|
*
|
135 |
|
|
**/
|
136 |
|
|
/******************************************************************************/
|
137 |
|
|
static u32 MEMS_ReadOutXY( void )
|
138 |
|
|
{
|
139 |
|
|
u8 OutXL;
|
140 |
|
|
u8 OutXH;
|
141 |
|
|
u8 OutYL;
|
142 |
|
|
u8 OutYH;
|
143 |
|
|
u8 OutZL;
|
144 |
|
|
u8 OutZH;
|
145 |
|
|
|
146 |
|
|
/* Chip Select low */
|
147 |
|
|
MEMS_ChipSelect( LOW );
|
148 |
|
|
|
149 |
|
|
/* Send "RDOUTXL" instruction */
|
150 |
|
|
MEMS_SendByte( RDOUTXL );
|
151 |
|
|
|
152 |
|
|
/* Read a byte */
|
153 |
|
|
OutXL = MEMS_SendByte( DUMMY_BYTE );
|
154 |
|
|
|
155 |
|
|
/* Read a byte */
|
156 |
|
|
OutXH = MEMS_SendByte( DUMMY_BYTE );
|
157 |
|
|
|
158 |
|
|
/* Read a byte */
|
159 |
|
|
OutYL = MEMS_SendByte( DUMMY_BYTE );
|
160 |
|
|
|
161 |
|
|
/* Read a byte */
|
162 |
|
|
OutYH = MEMS_SendByte( DUMMY_BYTE );
|
163 |
|
|
|
164 |
|
|
/* Read a byte */
|
165 |
|
|
OutZL = MEMS_SendByte( DUMMY_BYTE );
|
166 |
|
|
|
167 |
|
|
/* Read a byte */
|
168 |
|
|
OutZH = MEMS_SendByte( DUMMY_BYTE );
|
169 |
|
|
|
170 |
|
|
MEMS_Info.OutX = OutXL + ( OutXH << 8 );
|
171 |
|
|
MEMS_Info.OutY = OutYL + ( OutYH << 8 );
|
172 |
|
|
MEMS_Info.OutZ = OutZL + ( OutZH << 8 );
|
173 |
|
|
|
174 |
|
|
/* Chip Select high */
|
175 |
|
|
MEMS_ChipSelect( HIGH );
|
176 |
|
|
|
177 |
|
|
MEMS_Info.OutX_F4 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F4 >> 2 ) ); // Filter on 4 values.
|
178 |
|
|
MEMS_Info.OutY_F4 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F4 >> 2 ) ); // Filter on 4 values.
|
179 |
|
|
MEMS_Info.OutZ_F4 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F4 >> 2 ) ); // Filter on 4 values.
|
180 |
|
|
|
181 |
|
|
MEMS_Info.OutX_F16 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F16 >> 4 ) ); // Filter on 16 values.
|
182 |
|
|
MEMS_Info.OutY_F16 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F16 >> 4 ) ); // Filter on 16 values.
|
183 |
|
|
MEMS_Info.OutZ_F16 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F16 >> 4 ) ); // Filter on 16 values.
|
184 |
|
|
|
185 |
|
|
MEMS_Info.OutX_F64 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F64 >> 6 ) ); // Filter on 64 values.
|
186 |
|
|
MEMS_Info.OutY_F64 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F64 >> 6 ) ); // Filter on 64 values.
|
187 |
|
|
MEMS_Info.OutZ_F64 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F64 >> 6 ) ); // Filter on 64 values.
|
188 |
|
|
|
189 |
|
|
MEMS_Info.OutX_F256 += ( MEMS_Info.OutX - ( MEMS_Info.OutX_F256 >> 8) ); // Filter on 256 values.
|
190 |
|
|
MEMS_Info.OutY_F256 += ( MEMS_Info.OutY - ( MEMS_Info.OutY_F256 >> 8) ); // Filter on 256 values.
|
191 |
|
|
MEMS_Info.OutZ_F256 += ( MEMS_Info.OutZ - ( MEMS_Info.OutZ_F256 >> 8) ); // Filter on 256 values.
|
192 |
|
|
|
193 |
|
|
if( N_filtering < 256 )
|
194 |
|
|
{
|
195 |
|
|
// Just to validate the calculated average values.
|
196 |
|
|
N_filtering++;
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
return ( MEMS_Info.OutX + ( MEMS_Info.OutY << 16 ) );
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/*******************************************************************************
|
203 |
|
|
*
|
204 |
|
|
* MEMS_ChipSelect
|
205 |
|
|
*
|
206 |
|
|
*******************************************************************************/
|
207 |
|
|
/**
|
208 |
|
|
* Selects or deselects the MEMS device.
|
209 |
|
|
*
|
210 |
|
|
* @param[in] State Level to be applied on ChipSelect pin.
|
211 |
|
|
*
|
212 |
|
|
**/
|
213 |
|
|
/******************************************************************************/
|
214 |
|
|
static void MEMS_ChipSelect( u8 State )
|
215 |
|
|
{
|
216 |
|
|
/* Set High or low the chip select line on PA.4 pin */
|
217 |
|
|
GPIO_WriteBit( GPIOD, GPIO_Pin_2, (BitAction)State );
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
/*******************************************************************************
|
221 |
|
|
*
|
222 |
|
|
* MEMS_SendByte
|
223 |
|
|
*
|
224 |
|
|
*******************************************************************************/
|
225 |
|
|
/**
|
226 |
|
|
* Sends a byte through the SPI interface and return the byte received from
|
227 |
|
|
* the SPI bus.
|
228 |
|
|
*
|
229 |
|
|
* @param[in] byte The byte to send to the SPI interface.
|
230 |
|
|
*
|
231 |
|
|
* @return The byte returned by the SPI bus.
|
232 |
|
|
*
|
233 |
|
|
**/
|
234 |
|
|
/******************************************************************************/
|
235 |
|
|
static u8 MEMS_SendByte( u8 byte )
|
236 |
|
|
{
|
237 |
|
|
/* Loop while DR register in not emplty */
|
238 |
|
|
while( SPI_I2S_GetFlagStatus( SPI2, SPI_I2S_FLAG_TXE ) == RESET );
|
239 |
|
|
|
240 |
|
|
/* Send byte through the SPI2 peripheral */
|
241 |
|
|
SPI_I2S_SendData( SPI2, byte );
|
242 |
|
|
|
243 |
|
|
/* Wait to receive a byte */
|
244 |
|
|
while( SPI_I2S_GetFlagStatus( SPI2, SPI_I2S_FLAG_RXNE ) == RESET );
|
245 |
|
|
|
246 |
|
|
/* Return the byte read from the SPI bus */
|
247 |
|
|
return SPI_I2S_ReceiveData( SPI2 );
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/* Public functions for CircleOS ---------------------------------------------*/
|
251 |
|
|
|
252 |
|
|
/*******************************************************************************
|
253 |
|
|
*
|
254 |
|
|
* MEMS_Init
|
255 |
|
|
*
|
256 |
|
|
*******************************************************************************/
|
257 |
|
|
/**
|
258 |
|
|
*
|
259 |
|
|
* Initializes the peripherals used by the SPI MEMS driver.
|
260 |
|
|
*
|
261 |
|
|
* @attention This function must <b>NOT</b> be called by the user.
|
262 |
|
|
*
|
263 |
|
|
**/
|
264 |
|
|
/******************************************************************************/
|
265 |
|
|
void MEMS_Init(void)
|
266 |
|
|
{
|
267 |
|
|
SPI_InitTypeDef SPI_InitStructure;
|
268 |
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
269 |
|
|
|
270 |
|
|
/* Configure PC6 and PC7 as Output push-pull For MEMS*/
|
271 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
272 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
273 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
274 |
|
|
|
275 |
|
|
GPIO_Init( GPIOC, &GPIO_InitStructure );
|
276 |
|
|
|
277 |
|
|
/* Enable SPI2 and GPIOA clocks */
|
278 |
|
|
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
|
279 |
|
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
|
280 |
|
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE );
|
281 |
|
|
|
282 |
|
|
/* Configure SPI2 pins: SCK, MISO and MOSI */
|
283 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
284 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
285 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
286 |
|
|
|
287 |
|
|
GPIO_Init( GPIOB, &GPIO_InitStructure );
|
288 |
|
|
|
289 |
|
|
/* Configure PD2 as Output push-pull, used as MEMS Chip select */
|
290 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
291 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
292 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
293 |
|
|
|
294 |
|
|
GPIO_Init( GPIOD, &GPIO_InitStructure );
|
295 |
|
|
|
296 |
|
|
/* SPI2 configuration */
|
297 |
|
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
298 |
|
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
299 |
|
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
300 |
|
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
301 |
|
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
302 |
|
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
303 |
|
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
304 |
|
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
305 |
|
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
306 |
|
|
|
307 |
|
|
SPI_Init( SPI2, &SPI_InitStructure );
|
308 |
|
|
|
309 |
|
|
/* Enable SPI2 */
|
310 |
|
|
SPI_Cmd( SPI2, ENABLE );
|
311 |
|
|
|
312 |
|
|
if( MEMS_ReadID() != 0x3A )
|
313 |
|
|
{
|
314 |
|
|
int i;
|
315 |
|
|
|
316 |
|
|
// Try to resynchronize
|
317 |
|
|
for( i = 0 ; i < 17 ; i++ )
|
318 |
|
|
{
|
319 |
|
|
/* Configure SPI2 pins: SCK, MISO and MOSI */
|
320 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
|
321 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
322 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
323 |
|
|
|
324 |
|
|
GPIO_Init( GPIOB, &GPIO_InitStructure );
|
325 |
|
|
GPIO_WriteBit( GPIOB, GPIO_Pin_15, HIGH );
|
326 |
|
|
MEMS_ChipSelect( LOW );
|
327 |
|
|
|
328 |
|
|
GPIO_WriteBit( GPIOB, GPIO_Pin_13, LOW );
|
329 |
|
|
GPIO_WriteBit( GPIOB, GPIO_Pin_13, HIGH );
|
330 |
|
|
MEMS_ChipSelect( HIGH );
|
331 |
|
|
|
332 |
|
|
/* Configure again PB. SCK as SPI2 pin */
|
333 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
|
334 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
335 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
336 |
|
|
|
337 |
|
|
GPIO_Init( GPIOB, &GPIO_InitStructure );
|
338 |
|
|
if ( MEMS_ReadID() == 0x3A )
|
339 |
|
|
{
|
340 |
|
|
break;
|
341 |
|
|
}
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
if( i == 17 )
|
345 |
|
|
{
|
346 |
|
|
DRAW_DisplayString( 1, 50, "Test MEM ID Failed", 17 );
|
347 |
|
|
}
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
/* Read for the first time */
|
351 |
|
|
N_filtering = 0;
|
352 |
|
|
|
353 |
|
|
MEMS_ReadOutXY();
|
354 |
|
|
|
355 |
|
|
MEMS_Info.OutX_F4 = MEMS_Info.OutX_F16 = MEMS_Info.OutX_F64 = MEMS_Info.OutX_F256 = MEMS_Info.OutX;
|
356 |
|
|
MEMS_Info.OutY_F4 = MEMS_Info.OutY_F16 = MEMS_Info.OutY_F64 = MEMS_Info.OutY_F256 = MEMS_Info.OutY;
|
357 |
|
|
MEMS_Info.OutZ_F4 = MEMS_Info.OutZ_F16 = MEMS_Info.OutZ_F64 = MEMS_Info.OutZ_F256 = MEMS_Info.OutZ;
|
358 |
|
|
|
359 |
|
|
/* Init X and Y*/
|
360 |
|
|
MEMS_GetPosition( &XInit, &YInit );
|
361 |
|
|
|
362 |
|
|
/* Wake Up Mems*/
|
363 |
|
|
MEMS_WakeUp();
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
/*******************************************************************************
|
367 |
|
|
*
|
368 |
|
|
* MEMS_Handler
|
369 |
|
|
*
|
370 |
|
|
*******************************************************************************/
|
371 |
|
|
/**
|
372 |
|
|
*
|
373 |
|
|
* Called by the CircleOS scheduler to manage the MEMS. The Circle beeps if the
|
374 |
|
|
* MEMS is shocked.
|
375 |
|
|
*
|
376 |
|
|
* @attention This function must <b>NOT</b> be called by the user.
|
377 |
|
|
*
|
378 |
|
|
**/
|
379 |
|
|
/******************************************************************************/
|
380 |
|
|
void MEMS_Handler( void )
|
381 |
|
|
{
|
382 |
|
|
char buffer [20];
|
383 |
|
|
int i;
|
384 |
|
|
int ofs_disp = 0;
|
385 |
|
|
|
386 |
|
|
if( StartingFromResetOrShockCounter )
|
387 |
|
|
{
|
388 |
|
|
StartingFromResetOrShockCounter--;
|
389 |
|
|
}
|
390 |
|
|
TimeCounterForDoubleClick++;
|
391 |
|
|
|
392 |
|
|
MEMS_ReadOutXY();
|
393 |
|
|
|
394 |
|
|
// Evaluate gradients
|
395 |
|
|
GradX = ( MEMS_Info.OutX_F4 >> 2 ) - MEMS_Info.OutX;
|
396 |
|
|
GradY = ( MEMS_Info.OutY_F4 >> 2 ) - MEMS_Info.OutY;
|
397 |
|
|
GradZ = ( MEMS_Info.OutZ_F4 >> 2 ) - MEMS_Info.OutZ;
|
398 |
|
|
|
399 |
|
|
// Decide whether a direction is selected
|
400 |
|
|
if( tMovePtrX == 0 )
|
401 |
|
|
{
|
402 |
|
|
if( ( GradX > MIN_REACT ) || ( GradX < -MIN_REACT ) )
|
403 |
|
|
{
|
404 |
|
|
iMovePtrX = GradX / DIV_REACT;
|
405 |
|
|
tMovePtrX = DELAY_REACT;
|
406 |
|
|
fMovePtrX = 1;
|
407 |
|
|
}
|
408 |
|
|
}
|
409 |
|
|
else
|
410 |
|
|
{
|
411 |
|
|
tMovePtrX--;
|
412 |
|
|
}
|
413 |
|
|
|
414 |
|
|
if( tMovePtrY == 0 )
|
415 |
|
|
{
|
416 |
|
|
if( ( GradY > MIN_REACT ) || ( GradY < -MIN_REACT ) )
|
417 |
|
|
{
|
418 |
|
|
iMovePtrY = GradY / DIV_REACT; //FL071012 rrm fix
|
419 |
|
|
tMovePtrY = DELAY_REACT;
|
420 |
|
|
fMovePtrY = 1;
|
421 |
|
|
}
|
422 |
|
|
}
|
423 |
|
|
else
|
424 |
|
|
{
|
425 |
|
|
tMovePtrY--;
|
426 |
|
|
}
|
427 |
|
|
|
428 |
|
|
if( tMovePtrZ==0 )
|
429 |
|
|
{
|
430 |
|
|
if( ( GradZ > MIN_REACT ) || ( GradY < -MIN_REACT ) )
|
431 |
|
|
{
|
432 |
|
|
iMovePtrZ = GradZ / DIV_REACT;
|
433 |
|
|
tMovePtrZ = DELAY_REACT;
|
434 |
|
|
fMovePtrZ = 1;
|
435 |
|
|
}
|
436 |
|
|
}
|
437 |
|
|
else
|
438 |
|
|
{
|
439 |
|
|
tMovePtrZ--;
|
440 |
|
|
}
|
441 |
|
|
|
442 |
|
|
Gradient2 = (s32)GradX * (s32)GradX + (s32)GradY * (s32)GradY + (s32)GradZ * (s32)GradZ;
|
443 |
|
|
|
444 |
|
|
// MEMS is shocked, let's beep!
|
445 |
|
|
if( ( Gradient2 > GRAD_SHOCK ) && ( BUZZER_GetMode() == BUZZER_OFF ) && ( StartingFromResetOrShockCounter == 0 ) )
|
446 |
|
|
{
|
447 |
|
|
MEMS_Info.Shocked++;
|
448 |
|
|
/*FL071007 = 1;
|
449 |
|
|
Suggested by Bob Seabrook: a further posiblity is to increment Shocked rather than just setting it
|
450 |
|
|
So it can still be tested for non zero as before but one can get more
|
451 |
|
|
info from the int without extra cost. */
|
452 |
|
|
|
453 |
|
|
#define DELAY_BETWEEN_TWO_SHOCK 20
|
454 |
|
|
#define MAX_DELAY_FOR_DOUBLECLICK 150
|
455 |
|
|
StartingFromResetOrShockCounter = DELAY_BETWEEN_TWO_SHOCK; //< filter: short delay before detecting the next shock
|
456 |
|
|
if ( (TimeCounterForDoubleClick - TimeLastShock) < MAX_DELAY_FOR_DOUBLECLICK )
|
457 |
|
|
{
|
458 |
|
|
MEMS_Info.DoubleClick++;
|
459 |
|
|
TimeLastShock = 0;
|
460 |
|
|
}
|
461 |
|
|
else
|
462 |
|
|
{
|
463 |
|
|
TimeLastShock = TimeCounterForDoubleClick;
|
464 |
|
|
}
|
465 |
|
|
BUZZER_SetMode( BUZZER_SHORTBEEP );
|
466 |
|
|
}
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
/*******************************************************************************
|
470 |
|
|
*
|
471 |
|
|
* MEMS_ReadID
|
472 |
|
|
*
|
473 |
|
|
*******************************************************************************/
|
474 |
|
|
/**
|
475 |
|
|
* Reads SPI chip identification.
|
476 |
|
|
*
|
477 |
|
|
* @return The SPI chip identification.
|
478 |
|
|
*
|
479 |
|
|
**/
|
480 |
|
|
/******************************************************************************/
|
481 |
|
|
u8 MEMS_ReadID( void )
|
482 |
|
|
{
|
483 |
|
|
u8 Temp = 0;
|
484 |
|
|
|
485 |
|
|
/* Chip Select low */
|
486 |
|
|
MEMS_ChipSelect( LOW );
|
487 |
|
|
|
488 |
|
|
/* Send "RDID" instruction */
|
489 |
|
|
MEMS_SendByte( RDID );
|
490 |
|
|
|
491 |
|
|
/* Read a byte from the MEMS */
|
492 |
|
|
Temp = MEMS_SendByte( DUMMY_BYTE );
|
493 |
|
|
|
494 |
|
|
/* Chip Select low */
|
495 |
|
|
MEMS_ChipSelect( HIGH );
|
496 |
|
|
|
497 |
|
|
return Temp;
|
498 |
|
|
}
|
499 |
|
|
|
500 |
|
|
/// @endcond
|
501 |
|
|
|
502 |
|
|
/* Public functions ----------------------------------------------------------*/
|
503 |
|
|
|
504 |
|
|
/*******************************************************************************
|
505 |
|
|
*
|
506 |
|
|
* MEMS_GetPosition
|
507 |
|
|
*
|
508 |
|
|
*******************************************************************************/
|
509 |
|
|
/**
|
510 |
|
|
*
|
511 |
|
|
* Returns the current (relative) position of the Primer.
|
512 |
|
|
* Only X-Y axis are considered here.
|
513 |
|
|
*
|
514 |
|
|
* @param[out] pX Current horizontal coordinate.
|
515 |
|
|
* @param[out] pY Current vertical coordinate.
|
516 |
|
|
*
|
517 |
|
|
* @warning The (0x0) point in on the low left corner.
|
518 |
|
|
* @note For absolute position information use MEMS_GetInfo()
|
519 |
|
|
*
|
520 |
|
|
**/
|
521 |
|
|
/******************************************************************************/
|
522 |
|
|
void MEMS_GetPosition( s16* pX, s16* pY )
|
523 |
|
|
{
|
524 |
|
|
*pX = MEMS_Info.OutX - XInit;
|
525 |
|
|
*pY = MEMS_Info.OutY - YInit;
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
/*******************************************************************************
|
529 |
|
|
*
|
530 |
|
|
* MEMS_GetRotation
|
531 |
|
|
*
|
532 |
|
|
*******************************************************************************/
|
533 |
|
|
/**
|
534 |
|
|
*
|
535 |
|
|
* Returns current screen orientation.
|
536 |
|
|
*
|
537 |
|
|
* @param[out] pH12 Current screen orientation.
|
538 |
|
|
*
|
539 |
|
|
**/
|
540 |
|
|
/******************************************************************************/
|
541 |
|
|
void MEMS_GetRotation( Rotate_H12_V_Match_TypeDef* pH12 )
|
542 |
|
|
{
|
543 |
|
|
s16 sX = MEMS_Info.OutX;
|
544 |
|
|
s16 sY = MEMS_Info.OutY;
|
545 |
|
|
|
546 |
|
|
if( ( ( sX <= -MARGIN ) && ( sY <= 0 ) && (sX<=sY ) ) ||
|
547 |
|
|
( ( sX <=- MARGIN ) && ( sY > 0) && (sX <= (-sY ) ) ) )
|
548 |
|
|
{
|
549 |
|
|
// 1st case: x<0, |x|>y => H12 = V9
|
550 |
|
|
*pH12 = V9;
|
551 |
|
|
}
|
552 |
|
|
else if( ( ( sY <= -MARGIN ) && ( sX <= 0 ) && ( sY <= sX ) ) ||
|
553 |
|
|
( ( sY <= -MARGIN ) && ( sX > 0 ) && ( sY <= (-sX ) ) ) )
|
554 |
|
|
{
|
555 |
|
|
// 2nd case: y<0, |y|>x => H12 = V12
|
556 |
|
|
*pH12 = V12;
|
557 |
|
|
}
|
558 |
|
|
else if( ( ( sX >= MARGIN ) && ( sY <= 0 ) && ( sX >= (-sY) ) ) ||
|
559 |
|
|
( ( sX >= MARGIN ) && ( sY > 0 ) && ( sX >= sY ) ) )
|
560 |
|
|
{
|
561 |
|
|
// 3rd case: x>0, |x|>y => H12=V3
|
562 |
|
|
*pH12 = V3;
|
563 |
|
|
}
|
564 |
|
|
else if( ( ( sY >= MARGIN ) && ( sX <= 0 ) && ( sY >= (-sX ) ) ) ||
|
565 |
|
|
( ( sY >= MARGIN ) && ( sX > 0 ) && ( sY >= sX ) ) )
|
566 |
|
|
{
|
567 |
|
|
// 4th case: y>0, |y|>x => H12=V6
|
568 |
|
|
*pH12 = V6;
|
569 |
|
|
}
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
/*******************************************************************************
|
573 |
|
|
*
|
574 |
|
|
* MEMS_SetNeutral
|
575 |
|
|
*
|
576 |
|
|
*******************************************************************************/
|
577 |
|
|
/**
|
578 |
|
|
*
|
579 |
|
|
* Set current position as "neutral position".
|
580 |
|
|
*
|
581 |
|
|
**/
|
582 |
|
|
/******************************************************************************/
|
583 |
|
|
void MEMS_SetNeutral( void )
|
584 |
|
|
{
|
585 |
|
|
// Set Neutral position.
|
586 |
|
|
MEMS_GetPosition( &XInit, &YInit );
|
587 |
|
|
}
|
588 |
|
|
|
589 |
|
|
/*******************************************************************************
|
590 |
|
|
*
|
591 |
|
|
* MEMS_GetInfo
|
592 |
|
|
*
|
593 |
|
|
*******************************************************************************/
|
594 |
|
|
/**
|
595 |
|
|
*
|
596 |
|
|
* Return the current MEMS information (state, absolute position...).
|
597 |
|
|
*
|
598 |
|
|
* @return a pointer to tMEMS_Info
|
599 |
|
|
*
|
600 |
|
|
**/
|
601 |
|
|
/******************************************************************************/
|
602 |
|
|
tMEMS_Info* MEMS_GetInfo( void )
|
603 |
|
|
{
|
604 |
|
|
return &MEMS_Info;
|
605 |
|
|
}
|