/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright, and the information and source code
* contained herein is confidential. The software including the source code
* may not be copied and the information contained herein may not be used or
* disclosed except with the written permission of MEMSIC Inc. (C) 2019
*****************************************************************************/
/**
* @brief
* This file implement magnetic sensor driver APIs.
* Modified history:
* V1.1: Add the interrupt function on 20190109
*/
#include "MMC5983.h"
#include "Customer.h"
#include "Config.h"
typedef signed char int8_t; // signed 8-bit number (-128 to +127)
typedef unsigned char uint8_t; // unsigned 8-bit number (+0 to +255)
typedef signed short int16_t; // signed 16-bt number (-32768 to +32767)
typedef unsigned short uint16_t; // unsigned 16-bit number (+0 to +65535)
typedef signed int int32_t; // signed 32-bt number (-2,147,483,648 to +2,147,483,647)
typedef unsigned int uint32_t; // unsigned 32-bit number (+0 to +4,294,967,295)
static MMC5983_DATA MagData = {0, 0, 0, 0};
/* Function declaration */
/**
* @brief Reset the sensor by software
*/
void MMC5983_Software_Reset(void);
/**
* @brief OTP read done check
*/
int MMC5983_Check_OTP(void);
/**
* @brief Check Product ID
*/
int MMC5983_Check_ID(void);
/**
* @brief SET operation
*/
void MMC5983_SET(void);
/**
* @brief RESET operation
*/
void MMC5983_RESET(void);
/*********************************************************************************
* decription: Reset the sensor by software
*********************************************************************************/
void MMC5983_Software_Reset(void)
{
/* Write reg 0x0A, set SW_RST bit '1', cause the chip to reset, re-read OTP */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL1, MMC5983_CMD_SW_RST);
/* Delay 20ms to finish the software reset operation */
Delay_Ms(20);
return;
}
/*********************************************************************************
* decription: OTP read done check
*********************************************************************************/
int MMC5983_Check_OTP(void)
{
unsigned char reg_val = 0;
Delay_Ms(20);
/* Read reg 0x08, check OTP_Read_Done bit */
I2C_Read_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_STATUS, ®_val);
if((reg_val&MMC5983_OTP_READ_DONE) != MMC5983_OTP_READ_DONE)
return -1;
return 1;
}
/*********************************************************************************
* decription: Product ID check
*********************************************************************************/
int MMC5983_Check_ID(void)
{
unsigned char pro_id = 0;
/* Read reg 0x2F, check product ID, default value is 0x30 */
I2C_Read_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_PRODUCTID1, &pro_id);
if(pro_id != MMC5983_PRODUCT_ID)
return -1;
return 1;
}
/*********************************************************************************
* decription: SET operation
*********************************************************************************/
void MMC5983_SET(void)
{
/* Write reg 0x09, Set Set bit '1', cause the chip to do Set operation */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, MMC5983_CMD_SET);
/* Delay to finish the SET operation */
Delay_Us(500);
return;
}
/*********************************************************************************
* decription: RESET operation
*********************************************************************************/
void MMC5983_RESET(void)
{
/* Write reg 0x09, Set Reset bit '1', cause the chip to do Reset operation */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, MMC5983_CMD_RESET);
/* Delay to finish the RESET operation */
Delay_Us(500);
return;
}
/*********************************************************************************
* decription: Enable the int when a mag or temp measuremet event is finished.
*********************************************************************************/
void MMC5983_INT_Meas_Done_Enable(void)
{
/* Write register 0x09, Set INT_Meas_Done_EN high */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, MMC5983_CMD_INT_MD_EN);
return;
}
/*********************************************************************************
* decription: Clear Meas_M_Done interrupt
*********************************************************************************/
void MMC5983_INT_Meas_M_Done_Clear(void)
{
/* Write register 0x08, Set Meas_M_Done bit high, clear Meas_M_Done interrupt */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_STATUS, MMC5983_MM_DONE);
return;
}
/*********************************************************************************
* decription: Clear Meas_T_Done interrupt
*********************************************************************************/
void MMC5983_INT_Meas_T_Done_Clear(void)
{
/* Write register 0x08, Set Meas_T_Done bit high, clear Meas_T_Done interrupt */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_STATUS, MMC5983_MT_DONE);
return;
}
/*********************************************************************************
* decription: Single mode configuration
*********************************************************************************/
void MMC5983_Single_Mode_Without_Auto_SR(uint8_t bandwith)
{
/* Write reg 0x0A, Set BW<1:0> = bandwith */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL1, bandwith);
/* Write reg 0x09 */
/* Set TM_M bit '1', Causes the chip to perform a magnetic measurement */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, MMC5983_CMD_TMM);
return;
}
/*********************************************************************************
* decription: Single mode configuration with auto set and reset
*********************************************************************************/
void MMC5983_Single_Mode_With_Auto_SR(uint8_t bandwith)
{
/* Write reg 0x0A */
/* Set BW<1:0> = bandwith
BW1 BW0 Measurement Time Bandwidth
0 0 8ms 100Hz
0 1 4ms 200Hz
1 0 2ms 400Hz
1 1 0.5ms 800Hz
*/
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL1, bandwith);
/* Write reg 0x09 */
/* Set Auto_SR_en bit '1', Enable the function of automatic set/reset */
/* Set TM_M bit '1', Causes the chip to perform a magnetic measurement */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, MMC5983_CMD_AUTO_SR_EN|MMC5983_CMD_TMM);
return;
}
/*********************************************************************************
* decription: Continuous mode configuration without auto set and reset
*********************************************************************************/
void MMC5983_Continuous_Mode_Without_Auto_SR(uint8_t bandwith, uint8_t sampling_rate)
{
/* Write reg 0x09 */
/* Set Auto_SR_en bit '0', Disable the feature of automatic set/reset */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL0, 0x00);
/* Write reg 0x0A, Set BW<1:0> = bandwith */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL1, bandwith);
/* Write reg 0x0B */
/* Set Cmmm_en bit '1', Enable the continuous mode */
/* Set CM_Freq<2:0> = sampling_rate */
I2C_Write_Reg(MMC5983_7BITI2C_ADDRESS, MMC5983_REG_CTRL2, MMC5983_CMD_CMM_EN|sampling_rate);
return;
}
/*********************************************************************************
* decription: Continuous mode configuration with part time SET
*********************************************************************************/
void MMC5983_Continuous_Mode_With_Part_SET(uint8_t bandwith, uint8_t sampling
- 1
- 2
- 3
- 4
前往页