 /******************************************************************************
 *                              MemsToLCD example :                            *
 *                                                                             *
 * This example needs a REva3 and a STM32 daughter board. It reads             *
 * accelerations from the MEMS following X, Y and Z axis then display their    *
 * values on the LCD display.                                                  *
 *                                                                             *
 ******************************************************************************/

#include "main.h"
#include <stdio.h>
#include <stdarg.h>   

#undef TRUE
#undef FALSE
#define TRUE 1
#define FALSE 0

#define DELAY_VAL 3600

char strBuffer[200];
signed short MEMS_OutX, MEMS_OutY, MEMS_OutZ;

void MemsReadPosition()
   {
   unsigned char buf[6];

   MEMS_ReadMultByte ( MEMS_OUTXL, 6, buf );

   MEMS_OutX =  (signed)(buf[0] + ( buf[1] << 8 ));
   MEMS_OutY =  (signed)(buf[2] + ( buf[3] << 8 ));
   MEMS_OutZ =  (signed)(buf[4] + ( buf[5] << 8 ));
   }

void DisplayMemsValues ( void ) 
	{
   while(1)
      {
      //Read X-Y-Z acceleration
      MemsReadPosition();
      
      // Format a string to display
      sprintf(strBuffer, "gx = %05d      gy = %05d      gz = %05d", MEMS_OutX, MEMS_OutY, MEMS_OutZ);
      
      // Reset LCD printing positions and display the new string
      LCD_Reset();
      LCD_Display_string(strBuffer); 
      
      delay(DELAY_VAL);
      }
	}

void init_STD()
   {
      { // NVIC Configuration      
#ifdef  VECT_TAB_RAM  
      /* Set the Vector Table base location at 0x20000000 */ 
      NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
#else  /* VECT_TAB_FLASH  */
      /* Set the Vector Table base location at 0x08000000 */ 
      NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
      }

      { // RCC Configuration
    
      ErrorStatus HSEStartUpStatus;
      /* RCC system reset(for debug purpose) */
      RCC_DeInit();

      /* Enable HSE */
      RCC_HSEConfig(RCC_HSE_ON);

      /* Wait till HSE is ready */
      HSEStartUpStatus = RCC_WaitForHSEStartUp();

      if(HSEStartUpStatus == SUCCESS)
         {
         /* HCLK = SYSCLK */
         RCC_HCLKConfig(RCC_SYSCLK_Div1); 

         /* PCLK2 = HCLK */
         RCC_PCLK2Config(RCC_HCLK_Div2); 

         /* PCLK1 = HCLK/2 */
         RCC_PCLK1Config(RCC_HCLK_Div2);

         /* Flash 2 wait state */
         FLASH_SetLatency(FLASH_Latency_2);
         /* Enable Prefetch Buffer */
         FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

         /* PLLCLK = 8MHz * 9 = 72 MHz */
         RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

         /* Enable PLL */ 
         RCC_PLLCmd(ENABLE);

         /* Wait till PLL is ready */
         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
            {
            }

         /* Select PLL as system clock source */
         RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

         /* Wait till PLL is used as system clock source */
         while(RCC_GetSYSCLKSource() != 0x08)
            {
            }
         }
      /* Enable GPIOs clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
      /* Enable I2C1 clock */
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE);
      }
   }

int main()
	{
   init_STD();
	init_I2C();
	init_LCD();

   LCD_Clear();
   
   DisplayMemsValues();

	while(1) {;}
 	return 0;
 	}

// To redirect printf to the LCD display (not used at the moment)
int __io_putchar(int ch)
   {
   LCD_Display_char(ch);
   return ch;
   }
