Skip to content

Commit

Permalink
Fixed bug with printf and negative numbers. Added accelerometer support.
Browse files Browse the repository at this point in the history
  • Loading branch information
planetbeing committed Feb 12, 2010
1 parent ecdb6ee commit 25f1000
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 12 deletions.
2 changes: 1 addition & 1 deletion openiboot/Makefile
Expand Up @@ -3,7 +3,7 @@ CC = $(CROSS)gcc
OBJCOPY = $(CROSS)objcopy
HFS_OBJS = hfs/btree.o hfs/catalog.o hfs/extents.o hfs/fastunicodecompare.o hfs/rawfile.o hfs/utility.o hfs/volume.o hfs/bdev.o hfs/fs.o
MENU_OBJS = menu.o stb_image.o
OPENIBOOT_OBJS=entry.o openiboot.o arm.o tasks.o openiboot-asmhelpers.o mmu.o miu.o clock.o power.o uart.o interrupt.o timer.o event.o usb.o gpio.o util.o printf.o malloc.o dma.o nor.o aes.o spi.o chipid.o lcd.o i2c.o images.o sha1.o commands.o pmu.o buttons.o framebuffer.o actions.o nvram.o nand.o ftl.o
OPENIBOOT_OBJS=entry.o openiboot.o arm.o tasks.o openiboot-asmhelpers.o mmu.o miu.o clock.o power.o uart.o interrupt.o timer.o event.o usb.o gpio.o util.o printf.o malloc.o dma.o nor.o aes.o spi.o chipid.o lcd.o i2c.o images.o sha1.o commands.o pmu.o buttons.o framebuffer.o actions.o nvram.o nand.o ftl.o accel.o
ARCHFLAGS += -mlittle-endian -mfpu=vfp -mthumb -mthumb-interwork
LIBRARIES += -lgcc -nostdlib
INC += -Iincludes
Expand Down
67 changes: 67 additions & 0 deletions openiboot/accel.c
@@ -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));
}


31 changes: 31 additions & 0 deletions openiboot/commands.c
Expand Up @@ -16,8 +16,10 @@
#include "dma.h"
#include "nand.h"
#include "ftl.h"
#include "i2c.h"
#include "hfs/fs.h"
#include "aes.h"
#include "accel.h"

void cmd_reboot(int argc, char** argv) {
Reboot();
Expand Down Expand Up @@ -666,6 +668,33 @@ void cmd_time(int argc, char** argv) {
bufferPrintf("Current time: %02d:%02d:%02d, %s %02d/%02d/20%02d\r\n", pmu_get_hours(), pmu_get_minutes(), pmu_get_seconds(), pmu_get_dayofweek_str(), pmu_get_month(), pmu_get_day(), pmu_get_year());
}

void cmd_iic_read(int argc, char** argv) {
if(argc < 4) {
bufferPrintf("Usage: %s <bus> <address> <register>\n", argv[0]);
return;
}


int bus = parseNumber(argv[1]);
int address = parseNumber(argv[2]);
uint8_t registers[1];
uint8_t out[1];

registers[0] = parseNumber(argv[3]);

i2c_rx(bus, address, registers, 1, out, 1);

bufferPrintf("Result: %d\n", (int) out[0]);
}

void cmd_accel(int argc, char** argv) {
int x = accel_get_x();
int y = accel_get_y();
int z = accel_get_z();

bufferPrintf("x: %d, y: %d, z: %d\n", x, y, z);
}

void cmd_help(int argc, char** argv) {
OPIBCommand* curCommand = CommandList;
while(curCommand->name != NULL) {
Expand Down Expand Up @@ -709,6 +738,8 @@ OPIBCommand CommandList[] =
{"nor_read", "read a block of NOR into RAM", cmd_nor_read},
{"nor_write", "write RAM into NOR", cmd_nor_write},
{"nor_erase", "erase a block of NOR", cmd_nor_erase},
{"iic_read", "read a IIC register", cmd_iic_read},
{"accel", "display accelerometer data", cmd_accel},
{"images_list", "list the images available on NOR", cmd_images_list},
{"images_read", "read an image on NOR", cmd_images_read},
{"pmu_voltage", "get the battery voltage", cmd_pmu_voltage},
Expand Down
12 changes: 12 additions & 0 deletions openiboot/includes/accel.h
@@ -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
41 changes: 41 additions & 0 deletions openiboot/includes/hardware/accel.h
@@ -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
13 changes: 3 additions & 10 deletions openiboot/openiboot.c
Expand Up @@ -21,6 +21,7 @@
#include "tasks.h"
#include "images.h"
#include "nvram.h"
#include "accel.h"

#include "util.h"
#include "commands.h"
Expand Down Expand Up @@ -76,18 +77,10 @@ void OpenIBootStart() {
#endif
#endif

clock_gate_switch(6, 1);
clock_gate_switch(0, 1);
SET_REG(0x38800050, 0x09000000);
SET_REG(0x38800054, 0x09000000);
SET_REG(0x38800084, 0x0a000000);
SET_REG(0x38800090, 1);
SET_REG(0x38800094, 0);
SET_REG(0x3880009C, 1);
//*(0x38800000) = 1;

startUSB();

accel_setup();

nand_setup();
#ifndef NO_HFS
fs_setup();
Expand Down
2 changes: 1 addition & 1 deletion openiboot/printf.c
Expand Up @@ -74,7 +74,7 @@ int do_printf(const char *fmt, va_list args, fnptr_t fn, void *ptr)
unsigned flags, actual_wd, count, given_wd;
char *where, buf[PR_BUFLEN];
unsigned char state, radix;
uint64_t num;
int64_t num;

state = flags = count = given_wd = 0;
/* begin scanning format specifier list */
Expand Down

0 comments on commit 25f1000

Please sign in to comment.