Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug with printf and negative numbers. Added accelerometer support.
- Loading branch information
1 parent
ecdb6ee
commit 25f1000
Showing
7 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "openiboot.h" | ||
#include "util.h" | ||
#include "accel.h" | ||
#include "i2c.h" | ||
#include "hardware/accel.h" | ||
|
||
int accel_get_reg(int reg) { | ||
uint8_t registers[1]; | ||
uint8_t out[1]; | ||
|
||
registers[0] = reg; | ||
|
||
i2c_rx(ACCEL_I2C_BUS, ACCEL_GETADDR, registers, 1, out, 1); | ||
return out[0]; | ||
} | ||
|
||
int accel_write_reg(int reg, int data, int verify) { | ||
uint8_t command[2]; | ||
|
||
command[0] = reg; | ||
command[1] = data; | ||
|
||
i2c_tx(ACCEL_I2C_BUS, ACCEL_SETADDR, command, sizeof(command)); | ||
|
||
if(!verify) | ||
return 0; | ||
|
||
uint8_t accelReg = reg; | ||
uint8_t buffer = 0; | ||
i2c_rx(ACCEL_I2C_BUS, ACCEL_GETADDR, &accelReg, 1, &buffer, 1); | ||
|
||
if(buffer == data) | ||
return 0; | ||
else | ||
return -1; | ||
} | ||
|
||
int accel_setup() | ||
{ | ||
int whoami = accel_get_reg(ACCEL_WHOAMI); | ||
if(whoami != ACCEL_WHOAMI_VALUE) | ||
{ | ||
bufferPrintf("accel: incorrect whoami value\n"); | ||
return -1; | ||
} | ||
|
||
accel_write_reg(ACCEL_CTRL_REG2, ACCEL_CTRL_REG2_BOOT, FALSE); | ||
accel_write_reg(ACCEL_CTRL_REG1, ACCEL_CTRL_REG1_PD | ACCEL_CTRL_REG1_XEN | ACCEL_CTRL_REG1_YEN | ACCEL_CTRL_REG1_ZEN, FALSE); | ||
return 0; | ||
} | ||
|
||
int accel_get_x() | ||
{ | ||
return (signed char)(accel_get_reg(ACCEL_OUTX)); | ||
} | ||
|
||
int accel_get_y() | ||
{ | ||
return (signed char)(accel_get_reg(ACCEL_OUTY)); | ||
} | ||
|
||
int accel_get_z() | ||
{ | ||
return (signed char)(accel_get_reg(ACCEL_OUTZ)); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef ACCEL_H | ||
#define ACCEL_H | ||
|
||
#include "openiboot.h" | ||
|
||
int accel_setup(); | ||
|
||
int accel_get_x(); | ||
int accel_get_y(); | ||
int accel_get_z(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef HW_ACCEL_H | ||
#define HW_ACCEL_H | ||
|
||
#include "hardware/s5l8900.h" | ||
|
||
// See http://www.st.com/stonline/products/literature/ds/12726.pdf | ||
|
||
#define ACCEL_I2C_BUS 0 | ||
|
||
// could be 0x72 and 0x73 as well | ||
#define ACCEL_SETADDR 0x3A | ||
#define ACCEL_GETADDR 0x3B | ||
|
||
#define ACCEL_WHOAMI 0x0F | ||
#define ACCEL_CTRL_REG1 0x20 | ||
#define ACCEL_CTRL_REG2 0x21 | ||
#define ACCEL_STATUS 0x27 | ||
#define ACCEL_OUTX 0x29 | ||
#define ACCEL_OUTY 0x2B | ||
#define ACCEL_OUTZ 0x2D | ||
|
||
#define ACCEL_WHOAMI_VALUE 0x3B | ||
|
||
#define ACCEL_CTRL_REG1_DR (1 << 7) | ||
#define ACCEL_CTRL_REG1_PD (1 << 6) | ||
#define ACCEL_CTRL_REG1_FS (1 << 5) | ||
#define ACCEL_CTRL_REG1_STP (1 << 4) | ||
#define ACCEL_CTRL_REG1_STM (1 << 3) | ||
#define ACCEL_CTRL_REG1_ZEN (1 << 2) | ||
#define ACCEL_CTRL_REG1_YEN (1 << 1) | ||
#define ACCEL_CTRL_REG1_XEN (1 << 0) | ||
|
||
#define ACCEL_CTRL_REG2_SIM (1 << 7) | ||
#define ACCEL_CTRL_REG2_BOOT (1 << 6) | ||
#define ACCEL_CTRL_REG2_FDS (1 << 4) | ||
#define ACCEL_CTRL_REG2_HPEN2 (1 << 3) | ||
#define ACCEL_CTRL_REG2_HPEN1 (1 << 2) | ||
#define ACCEL_CTRL_REG2_HP2 (1 << 1) | ||
#define ACCEL_CTRL_REG2_HP1 (1 << 0) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters