/*
===============================================================================
Name : main.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
===============================================================================
*/
#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif
#include <stdlib.h>
#include <string.h>
#include <cr_section_macros.h>
// TODO: insert other include files here
#define I2C_INTERFACE I2C2
#define I2C_CLOCK100000
#define ACC_I2C_ADDR (0x1D)
#define ACC_ADDR_XOUTL 0x00
#define ACC_ADDR_XOUTH 0x01
#define ACC_ADDR_YOUTL 0x02
#define ACC_ADDR_YOUTX 0x03
#define ACC_ADDR_ZOUTL 0x04
#define ACC_ADDR_ZOUTH 0x05
#define ACC_ADDR_XOUT8 0x06
#define ACC_ADDR_YOUT8 0x07
#define ACC_ADDR_ZOUT8 0x08
#define ACC_ADDR_STATUS 0x09
#define ACC_ADDR_DETSRC 0x0A
#define ACC_ADDR_TOUT 0x0B
#define ACC_ADDR_I2CAD 0x0D
#define ACC_ADDR_USRINF 0x0E
#define ACC_ADDR_WHOAMI 0x0F
#define ACC_ADDR_XOFFL 0x10
#define ACC_ADDR_XOFFH 0x11
#define ACC_ADDR_YOFFL 0x12
#define ACC_ADDR_YOFFH 0x13
#define ACC_ADDR_ZOFFL 0x14
#define ACC_ADDR_ZOFFH 0x15
#define ACC_ADDR_MCTL 0x16
#define ACC_ADDR_INTRST 0x17
#define ACC_ADDR_CTL1 0x18
#define ACC_ADDR_CTL2 0x19
#define ACC_ADDR_LDTH 0x1A
#define ACC_ADDR_PDTH 0x1B
#define ACC_ADDR_PW 0x1C
#define ACC_ADDR_LT 0x1D
#define ACC_ADDR_TW 0x1E
#define ACC_MCTL_MODE(m) ((m) << 0)
#define ACC_MCTL_GLVL(g) ((g) << 2)
#define ACC_STATUS_DRDY 0x01
#define ACC_STATUS_DOVR 0x02
#define ACC_STATUS_PERR 0x04
typedef enum
{
ACC_MODE_STANDBY,
ACC_MODE_MEASURE,
ACC_MODE_LEVEL, /* level detection */
ACC_MODE_PULSE, /* pulse detection */
} acc_mode_t;
typedef enum
{
ACC_RANGE_8G,
ACC_RANGE_2G,
ACC_RANGE_4G,
} acc_range_t;
// TODO: insert other definitions and declarations here
/* State machine handler*/
static void i2c_state_handling(I2C_ID_T id)
{
Chip_I2C_MasterStateHandler(id);
}
// ------------------------------------------------------------------------//
// -------------------------- Interrup handlers ---------------------------//
// ------------------------------------------------------------------------//
/**
* @briefI2C2 Interrupt handler
* @returnNone
*/
void I2C2_IRQHandler(void)
{
//while(1);
i2c_state_handling(I2C2);
}
// ------------------------------------------------------------------------//
// -------------------------- I2c related stuff ---------------------------//
// ------------------------------------------------------------------------//
static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len)
{
if (Chip_I2C_MasterRead (I2C_INTERFACE, addr, buf, len) > 0){
return (0);
} else {
return (-1);
}
}
static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len)
{
if (Chip_I2C_MasterSend(I2C_INTERFACE, addr, buf, len) >0 )
{
return (0);
} else
{
return (-1);
}
}
static uint8_t getStatus(void)
{
uint8_t buf[1];
buf[0] = ACC_ADDR_STATUS;
I2CWrite(ACC_I2C_ADDR, buf, 1);
I2CRead(ACC_I2C_ADDR, buf, 1);
return buf[0];
}
static uint8_t getModeControl(void)
{
uint8_t buf[1];
buf[0] = ACC_ADDR_MCTL;
I2CWrite(ACC_I2C_ADDR, buf, 1);
I2CRead(ACC_I2C_ADDR, buf, 1);
return buf[0];
}
static void setModeControl(uint8_t mctl)
{
uint8_t buf[2];
buf[0] = ACC_ADDR_MCTL;
buf[1] = mctl;
I2CWrite(ACC_I2C_ADDR, buf, 2);
}
void acc_init (void)
{
/* set to measurement mode by default */
setModeControl( (ACC_MCTL_MODE(ACC_MODE_MEASURE)
| ACC_MCTL_GLVL(ACC_RANGE_2G) ));
}
int main(void) {
#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
// Read clock settings and update SystemCoreClock variable
SystemCoreClockUpdate();
// Set up and initialize all required blocks and
// functions related to the board hardware
Board_Init();
// Set the LED to the state of "On"
Board_LED_Set(0, true);
#endif
#endif
// TODO: insert code here
Board_I2C_Init(I2C_INTERFACE);
Chip_I2C_Init (I2C_INTERFACE);
Chip_I2C_SetClockRate (I2C_INTERFACE, I2C_CLOCK);
Chip_I2C_SetMasterEventHandler(I2C_INTERFACE, Chip_I2C_EventHandler);
NVIC_EnableIRQ(I2C2_IRQn);
acc_init();
// Force the counter to be placed into memory
volatile static int i = 0 ;
// Enter an infinite loop, just incrementing a counter
while(1) {
i++ ;
Board_LED_Set(0, false);
}
return 0 ;
}
|