driver for gyro

Dependencies:   COG4050_ADT7420

Fork of COG4050_adxl355_adxl357-ver2 by ADI_CAC

Committer:
vtoffoli
Date:
Tue Aug 14 11:33:30 2018 +0000
Revision:
7:5aaa09c40283
Parent:
6:45d2393ef468
Child:
9:1afd906c5ed2
update 14.08.2018;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vtoffoli 6:45d2393ef468 1 #include <stdint.h>
vtoffoli 6:45d2393ef468 2 #include "mbed.h"
vtoffoli 6:45d2393ef468 3 #include "ADXRS290.h"
vtoffoli 6:45d2393ef468 4
vtoffoli 7:5aaa09c40283 5 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 6 /** SPI - MAX 5MHZ */
vtoffoli 7:5aaa09c40283 7 /** ----------------------------------- */
vtoffoli 6:45d2393ef468 8 ADXRS290::ADXRS290(PinName cs_pin, PinName MOSI, PinName MISO, PinName SCK): adxrs290(MOSI, MISO, SCK), cs(cs_pin)
vtoffoli 6:45d2393ef468 9 {
vtoffoli 6:45d2393ef468 10 cs = 1;
vtoffoli 6:45d2393ef468 11 adxrs290.format(8,_SPI_MODE);
vtoffoli 6:45d2393ef468 12 adxrs290.lock();
vtoffoli 6:45d2393ef468 13 gyro_sens = 5e-3;
vtoffoli 6:45d2393ef468 14 t_sens = 0.1;
vtoffoli 6:45d2393ef468 15 }
vtoffoli 6:45d2393ef468 16 void ADXRS290::frequency(int hz)
vtoffoli 6:45d2393ef468 17 {
vtoffoli 6:45d2393ef468 18 adxrs290.frequency(hz);
vtoffoli 6:45d2393ef468 19 }
vtoffoli 6:45d2393ef468 20
vtoffoli 6:45d2393ef468 21 /** ----------------------------------- */
vtoffoli 6:45d2393ef468 22 /** Writes the reg register with data */
vtoffoli 6:45d2393ef468 23 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 24 void ADXRS290::write_reg(ADXRS290_register_t reg, uint8_t data){
vtoffoli 7:5aaa09c40283 25 adxrs290.format(8, _SPI_MODE);
vtoffoli 7:5aaa09c40283 26 cs = false;
vtoffoli 7:5aaa09c40283 27 adxrs290.write(static_cast<uint8_t>(reg) | _WRITE_REG_CMD);
vtoffoli 7:5aaa09c40283 28 adxrs290.write(data);
vtoffoli 7:5aaa09c40283 29 cs = true;
vtoffoli 7:5aaa09c40283 30 }
vtoffoli 7:5aaa09c40283 31 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 32 /** Reads the reg register */
vtoffoli 7:5aaa09c40283 33 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 34 uint8_t ADXRS290::read_reg(ADXRS290_register_t reg){
vtoffoli 7:5aaa09c40283 35 uint8_t ret_val;
vtoffoli 7:5aaa09c40283 36 adxrs290.format(8, _SPI_MODE);
vtoffoli 7:5aaa09c40283 37 cs = false;
vtoffoli 7:5aaa09c40283 38 adxrs290.write(static_cast<uint8_t>(reg) | _READ_REG_CMD);
vtoffoli 7:5aaa09c40283 39 ret_val = adxrs290.write(_DUMMY_BYTE);
vtoffoli 7:5aaa09c40283 40 cs = true;
vtoffoli 7:5aaa09c40283 41 return ret_val;
vtoffoli 7:5aaa09c40283 42 }
vtoffoli 7:5aaa09c40283 43 uint16_t ADXRS290::read_reg_u16(ADXRS290_register_t reg){
vtoffoli 7:5aaa09c40283 44 uint16_t ret_val = 0;
vtoffoli 7:5aaa09c40283 45 adxrs290.format(8, _SPI_MODE);
vtoffoli 7:5aaa09c40283 46 cs = false;
vtoffoli 7:5aaa09c40283 47 adxrs290.write(static_cast<uint8_t>(reg) | _READ_REG_CMD);
vtoffoli 7:5aaa09c40283 48 ret_val = adxrs290.write(_DUMMY_BYTE);
vtoffoli 7:5aaa09c40283 49 ret_val = (ret_val) | (adxrs290.write(_DUMMY_BYTE)<<8);
vtoffoli 7:5aaa09c40283 50 cs = true;
vtoffoli 7:5aaa09c40283 51 return ret_val;
vtoffoli 7:5aaa09c40283 52 }
vtoffoli 7:5aaa09c40283 53 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 54 /** Sets the CTL registers */
vtoffoli 7:5aaa09c40283 55 /** ----------------------------------- */
vtoffoli 7:5aaa09c40283 56 void ADXRS290::set_power_ctl_reg(uint8_t data){
vtoffoli 7:5aaa09c40283 57 write_reg(POWER_CTL, data);
vtoffoli 7:5aaa09c40283 58 wait(0.1);
vtoffoli 7:5aaa09c40283 59 }
vtoffoli 7:5aaa09c40283 60 void ADXRS290::set_filter_ctl_reg(ADXRS290_filter_ctl_t hpf, ADXRS290_filter_ctl_t lpf){
vtoffoli 7:5aaa09c40283 61 write_reg(FILTER, hpf|lpf);
vtoffoli 7:5aaa09c40283 62 }
vtoffoli 6:45d2393ef468 63 void ADXRS290::set_sync(ADXRS290_dataready_ctl_t data){}
vtoffoli 6:45d2393ef468 64
vtoffoli 7:5aaa09c40283 65 uint16_t ADXRS290::scanx(){
vtoffoli 7:5aaa09c40283 66 return read_reg_u16(DATAX0);
vtoffoli 7:5aaa09c40283 67 }
vtoffoli 7:5aaa09c40283 68 uint16_t ADXRS290::scany(){
vtoffoli 7:5aaa09c40283 69 return read_reg_u16(DATAY0);
vtoffoli 7:5aaa09c40283 70 }
vtoffoli 7:5aaa09c40283 71 uint16_t ADXRS290::scant(){
vtoffoli 7:5aaa09c40283 72 return read_reg_u16(TEMP0);
vtoffoli 7:5aaa09c40283 73 }