restructured into a interface file and a driver file.
This commit is contained in:
parent
1bc91feed8
commit
b97b731bb9
|
@ -4,7 +4,12 @@
|
||||||
* Description: The implimentation file for the gy521 module, assumes TWI/I2c
|
* Description: The implimentation file for the gy521 module, assumes TWI/I2c
|
||||||
*/
|
*/
|
||||||
#include "gy521_driver.h"
|
#include "gy521_driver.h"
|
||||||
#include <stdint.h>
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* MACROS
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
/*DEBUG macro*/
|
/*DEBUG macro*/
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
|
@ -32,6 +37,12 @@ extern void mock_assert(const int result, const char* const expression,
|
||||||
|
|
||||||
#endif // UNIT_TESTING
|
#endif // UNIT_TESTING
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* Defines and consts
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
#define NUM_ACCEL_REGS 6
|
#define NUM_ACCEL_REGS 6
|
||||||
#define NUM_GYRO_REGS 6
|
#define NUM_GYRO_REGS 6
|
||||||
|
|
||||||
|
@ -52,100 +63,6 @@ extern void mock_assert(const int result, const char* const expression,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* ############################
|
|
||||||
* REGISTER MAP
|
|
||||||
* ############################
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*This thing is fairly huge, but it's got pretty much all the registers
|
|
||||||
* on it.*/
|
|
||||||
|
|
||||||
enum gy521_map {
|
|
||||||
self_test_x = 0x0D,
|
|
||||||
self_test_y,
|
|
||||||
self_test_z,
|
|
||||||
self_test_a,
|
|
||||||
smplrt_div = 0x19,
|
|
||||||
config,
|
|
||||||
gyro_config,
|
|
||||||
accel_config,
|
|
||||||
fifo_en = 0x23,
|
|
||||||
i2c_mst_ctrl,
|
|
||||||
i2c_slv0_addr,
|
|
||||||
i2c_slv0_reg,
|
|
||||||
i2c_slv0_ctrl,
|
|
||||||
i2c_slv1_addr,
|
|
||||||
i2c_slv1_reg,
|
|
||||||
i2c_slv1_ctrl,
|
|
||||||
i2c_slv2_addr,
|
|
||||||
i2c_slv2_reg,
|
|
||||||
i2c_slv2_ctrl,
|
|
||||||
i2c_slv3_addr,
|
|
||||||
i2c_slv3_reg,
|
|
||||||
i2c_slv3_ctrl,
|
|
||||||
i2c_slv4_addr,
|
|
||||||
i2c_slv4_reg,
|
|
||||||
i2c_slv4_do,
|
|
||||||
i2c_slv4_ctrl,
|
|
||||||
i2c_slv4_di,
|
|
||||||
i2c_mst_status,
|
|
||||||
int_pin_cfg,
|
|
||||||
int_enable,
|
|
||||||
int_status,
|
|
||||||
accel_xouth = 0x3B,
|
|
||||||
accel_xoutl,
|
|
||||||
accel_youth,
|
|
||||||
accel_youtl,
|
|
||||||
accel_zouth,
|
|
||||||
accel_zoutl,
|
|
||||||
temp_outh,
|
|
||||||
temp_outl,
|
|
||||||
gyro_xouth,
|
|
||||||
gyro_xoutl,
|
|
||||||
gyro_youth,
|
|
||||||
gyro_youtl,
|
|
||||||
gyro_zouth,
|
|
||||||
gyro_zoutl,
|
|
||||||
ext_sens_data_00,
|
|
||||||
ext_sens_data_01,
|
|
||||||
ext_sens_data_02,
|
|
||||||
ext_sens_data_03,
|
|
||||||
ext_sens_data_04,
|
|
||||||
ext_sens_data_05,
|
|
||||||
ext_sens_data_06,
|
|
||||||
ext_sens_data_07,
|
|
||||||
ext_sens_data_08,
|
|
||||||
ext_sens_data_09,
|
|
||||||
ext_sens_data_10,
|
|
||||||
ext_sens_data_11,
|
|
||||||
ext_sens_data_12,
|
|
||||||
ext_sens_data_13,
|
|
||||||
ext_sens_data_14,
|
|
||||||
ext_sens_data_15,
|
|
||||||
ext_sens_data_16,
|
|
||||||
ext_sens_data_17,
|
|
||||||
ext_sens_data_18,
|
|
||||||
ext_sens_data_19,
|
|
||||||
ext_sens_data_20,
|
|
||||||
ext_sens_data_21,
|
|
||||||
ext_sens_data_22,
|
|
||||||
ext_sens_data_23,
|
|
||||||
i2c_slv0_do,
|
|
||||||
i2c_slv1_do,
|
|
||||||
i2c_slv2_do,
|
|
||||||
i2c_slv3_do,
|
|
||||||
i2c_mst_delay_ctrl,
|
|
||||||
signal_path_reset,
|
|
||||||
user_ctrl,
|
|
||||||
pwr_mgmt_1,
|
|
||||||
pwr_mgmt_2,
|
|
||||||
fifo_couth = 0x72,
|
|
||||||
fifo_contl,
|
|
||||||
fifo_r_w,
|
|
||||||
who_am_i,
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ############################
|
* ############################
|
||||||
* Structures
|
* Structures
|
||||||
|
|
|
@ -6,8 +6,7 @@
|
||||||
|
|
||||||
#ifndef GY521_DRIVER_H
|
#ifndef GY521_DRIVER_H
|
||||||
#define GY521_DRIVER_H
|
#define GY521_DRIVER_H
|
||||||
|
#include "gy521_interface.h"
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -16,10 +15,6 @@
|
||||||
* ############################
|
* ############################
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define STARTUP_DELAY 30 //in ms
|
|
||||||
#define TWI_GY521_ADDR1 0x68 //ADO Logic Low
|
|
||||||
#define TWI_GY521_ADDR2 0x69 //ADO Logic High
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ############################
|
* ############################
|
||||||
* Types/Structures
|
* Types/Structures
|
||||||
|
@ -38,13 +33,101 @@ typedef struct accel_values{
|
||||||
uint16_t z;
|
uint16_t z;
|
||||||
}accel_values_struct;
|
}accel_values_struct;
|
||||||
|
|
||||||
typedef struct gy521_module gy521_module;
|
/*
|
||||||
|
* ############################
|
||||||
|
* REGISTER MAP
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*This thing is fairly huge, but it's got pretty much all the registers
|
||||||
|
* on it.*/
|
||||||
|
|
||||||
|
enum gy521_map {
|
||||||
|
self_test_x = 0x0D,
|
||||||
|
self_test_y,
|
||||||
|
self_test_z,
|
||||||
|
self_test_a,
|
||||||
|
smplrt_div = 0x19,
|
||||||
|
config,
|
||||||
|
gyro_config,
|
||||||
|
accel_config,
|
||||||
|
fifo_en = 0x23,
|
||||||
|
i2c_mst_ctrl,
|
||||||
|
i2c_slv0_addr,
|
||||||
|
i2c_slv0_reg,
|
||||||
|
i2c_slv0_ctrl,
|
||||||
|
i2c_slv1_addr,
|
||||||
|
i2c_slv1_reg,
|
||||||
|
i2c_slv1_ctrl,
|
||||||
|
i2c_slv2_addr,
|
||||||
|
i2c_slv2_reg,
|
||||||
|
i2c_slv2_ctrl,
|
||||||
|
i2c_slv3_addr,
|
||||||
|
i2c_slv3_reg,
|
||||||
|
i2c_slv3_ctrl,
|
||||||
|
i2c_slv4_addr,
|
||||||
|
i2c_slv4_reg,
|
||||||
|
i2c_slv4_do,
|
||||||
|
i2c_slv4_ctrl,
|
||||||
|
i2c_slv4_di,
|
||||||
|
i2c_mst_status,
|
||||||
|
int_pin_cfg,
|
||||||
|
int_enable,
|
||||||
|
int_status,
|
||||||
|
accel_xouth = 0x3B,
|
||||||
|
accel_xoutl,
|
||||||
|
accel_youth,
|
||||||
|
accel_youtl,
|
||||||
|
accel_zouth,
|
||||||
|
accel_zoutl,
|
||||||
|
temp_outh,
|
||||||
|
temp_outl,
|
||||||
|
gyro_xouth,
|
||||||
|
gyro_xoutl,
|
||||||
|
gyro_youth,
|
||||||
|
gyro_youtl,
|
||||||
|
gyro_zouth,
|
||||||
|
gyro_zoutl,
|
||||||
|
ext_sens_data_00,
|
||||||
|
ext_sens_data_01,
|
||||||
|
ext_sens_data_02,
|
||||||
|
ext_sens_data_03,
|
||||||
|
ext_sens_data_04,
|
||||||
|
ext_sens_data_05,
|
||||||
|
ext_sens_data_06,
|
||||||
|
ext_sens_data_07,
|
||||||
|
ext_sens_data_08,
|
||||||
|
ext_sens_data_09,
|
||||||
|
ext_sens_data_10,
|
||||||
|
ext_sens_data_11,
|
||||||
|
ext_sens_data_12,
|
||||||
|
ext_sens_data_13,
|
||||||
|
ext_sens_data_14,
|
||||||
|
ext_sens_data_15,
|
||||||
|
ext_sens_data_16,
|
||||||
|
ext_sens_data_17,
|
||||||
|
ext_sens_data_18,
|
||||||
|
ext_sens_data_19,
|
||||||
|
ext_sens_data_20,
|
||||||
|
ext_sens_data_21,
|
||||||
|
ext_sens_data_22,
|
||||||
|
ext_sens_data_23,
|
||||||
|
i2c_slv0_do,
|
||||||
|
i2c_slv1_do,
|
||||||
|
i2c_slv2_do,
|
||||||
|
i2c_slv3_do,
|
||||||
|
i2c_mst_delay_ctrl,
|
||||||
|
signal_path_reset,
|
||||||
|
user_ctrl,
|
||||||
|
pwr_mgmt_1,
|
||||||
|
pwr_mgmt_2,
|
||||||
|
fifo_couth = 0x72,
|
||||||
|
fifo_contl,
|
||||||
|
fifo_r_w,
|
||||||
|
who_am_i,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef enum{
|
|
||||||
passed = 0,
|
|
||||||
gyro_failed,
|
|
||||||
accel_failed,
|
|
||||||
}self_test_results;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ############################
|
* ############################
|
||||||
|
@ -52,25 +135,22 @@ typedef enum{
|
||||||
* ############################
|
* ############################
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
gy521_module* gy521_new(void);
|
||||||
|
|
||||||
/*Function pointers for the TX and RX fuctionality*/
|
void disable_self_test_gyro(struct gy521_module* m);
|
||||||
|
void enable_self_test_gyro(struct gy521_module* m);
|
||||||
|
struct ft_vals self_test_ft_calculation(struct gy521_module* m);
|
||||||
|
self_test_results gy521_self_test(struct gy521_module* m);
|
||||||
|
|
||||||
extern void (*gy521_twi_tx)(uint8_t, uint8_t*, uint8_t);
|
uint8_t read_register(gy521_module *m, enum gy521_map reg);
|
||||||
extern void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t);
|
void write_register(gy521_module *m, enum gy521_map reg, uint8_t data);
|
||||||
|
|
||||||
|
|
||||||
struct gy521_module* gy521_new(void);
|
|
||||||
_Bool gy521_init(struct gy521_module *m, uint8_t slave_address);
|
|
||||||
void gy521_update_gyro(struct gy521_module* m);
|
|
||||||
void gy521_update_accel(struct gy521_module* m);
|
|
||||||
void gy521_free(struct gy521_module *m);
|
|
||||||
|
|
||||||
/*These are used instead of direct access; assuming interrupt driven*/
|
/*These are used instead of direct access; assuming interrupt driven*/
|
||||||
/*updates to the gy521 then it protects the user from using volatile values*/
|
/*updates to the gy521 then it protects the user from using volatile values*/
|
||||||
struct accel_values gy521_get_accel(struct gy521_module* m);
|
struct accel_values gy521_get_accel(struct gy521_module* m);
|
||||||
struct gyro_values gy521_get_gyro(struct gy521_module* m);
|
struct gyro_values gy521_get_gyro(struct gy521_module* m);
|
||||||
|
|
||||||
self_test_results gy521_self_test(struct gy521_module* m);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* GY521_DRIVER_H */
|
#endif /* GY521_DRIVER_H */
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
* Author: Jake Goodwin
|
||||||
|
* Filename: interface.h
|
||||||
|
* Description: The file you should include for normal use.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*Function pointers for the TX and RX fuctionality*/
|
||||||
|
|
||||||
|
#ifndef _GY521_INTERFACE_H
|
||||||
|
#define _GY521_INTERFACE_H
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* DEFINES
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define STARTUP_DELAY 30 //in ms
|
||||||
|
#define TWI_GY521_ADDR1 0x68 //ADO Logic Low
|
||||||
|
#define TWI_GY521_ADDR2 0x69 //ADO Logic High
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* Structures & Typedefs
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct gy521_module gy521_module;
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
passed = 0,
|
||||||
|
gyro_failed,
|
||||||
|
accel_failed,
|
||||||
|
}self_test_results;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ############################
|
||||||
|
* Fuction Prototypes
|
||||||
|
* ############################
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern void (*gy521_twi_tx)(uint8_t, uint8_t*, uint8_t);
|
||||||
|
extern void (*gy521_twi_rx)(uint8_t, uint8_t*, uint8_t);
|
||||||
|
|
||||||
|
|
||||||
|
struct gy521_module* gy521_new(void);
|
||||||
|
_Bool gy521_init(struct gy521_module *m, uint8_t slave_address);
|
||||||
|
void gy521_update_gyro(struct gy521_module* m);
|
||||||
|
void gy521_update_accel(struct gy521_module* m);
|
||||||
|
void gy521_free(struct gy521_module *m);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue