From 257d17da6d64d2e265df3c80192a01f47e1dd2b7 Mon Sep 17 00:00:00 2001 From: Linus Nielsen Feltzing Date: Sat, 20 Apr 2002 23:18:14 +0000 Subject: [PATCH] First version git-svn-id: svn://svn.rockbox.org/rockbox/trunk@158 a1c6a512-1295-4272-9138-f99709370657 --- firmware/mas.c | 164 +++++++++++++++++++++++++++++++++++++ firmware/mas.h | 54 ++++++++++++ firmware/test/i2c/Makefile | 44 ++++++++++ firmware/test/i2c/app.lds | 23 ++++++ firmware/test/i2c/crt0.S | 49 +++++++++++ firmware/test/i2c/main.c | 128 +++++++++++++++++++++++++++++ 6 files changed, 462 insertions(+) create mode 100644 firmware/mas.c create mode 100644 firmware/mas.h create mode 100644 firmware/test/i2c/Makefile create mode 100644 firmware/test/i2c/app.lds create mode 100644 firmware/test/i2c/crt0.S create mode 100644 firmware/test/i2c/main.c diff --git a/firmware/mas.c b/firmware/mas.c new file mode 100644 index 0000000000..decfff612b --- /dev/null +++ b/firmware/mas.c @@ -0,0 +1,164 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "i2c.h" +#include "debug.h" +#include "mas.h" + +/* note: 'len' is number of 32-bit words, not number of bytes! */ +int mas_readmem(int bank, int addr, unsigned long* dest, int len) +{ + int i; + unsigned char buf[16]; + + i=0; + buf[i++] = MAS_DATA_WRITE; + buf[i++] = bank?0xf0:0xe0; + buf[i++] = 0x00; + buf[i++] = (len & 0xff00) >> 8; + buf[i++] = len & 0xff; + buf[i++] = (addr & 0xff00) >> 8; + buf[i++] = addr & 0xff; + + /* send read command */ + if (i2c_write(MAS_DEV_WRITE,buf,i)) + { + return -1; + } + + return mas_devread(dest, len); +} + +/* note: 'len' is number of 32-bit words, not number of bytes! */ +int mas_writemem(int bank, int addr, unsigned long* src, int len) +{ + int i, j; + unsigned char buf[60]; + unsigned char* ptr = (unsigned char*)src; + + i=0; + buf[i++] = MAS_DATA_WRITE; + buf[i++] = bank; + buf[i++] = 0x00; + buf[i++] = (len & 0xff00) >> 8; + buf[i++] = len & 0xff; + buf[i++] = (addr & 0xff00) >> 8; + buf[i++] = addr & 0xff; + + j = 0; + while(len--) { + buf[i++] = ptr[j*4+1]; + buf[i++] = ptr[j*4+0]; + buf[i++] = 0; + buf[i++] = ptr[j*4+2]; + j += 4; + } + + /* send write command */ + if (i2c_write(MAS_DEV_WRITE,buf,i)) + { + return -1; + } + + return 0; +} + +int mas_readreg(int reg) +{ + int i; + unsigned char buf[16]; + + i=0; + buf[i++] = MAS_DATA_WRITE; + buf[i++] = 0xd0 | reg >> 4; + buf[i++] = (reg & 0x0f) << 4; + + /* send read command */ + if (i2c_write(MAS_DEV_WRITE,buf,i)) + { + return -1; + } + + if(mas_devread((unsigned long *)buf, 1)) + { + return -2; + } + + return buf[0] | buf[1] << 8 | buf[3] << 16; +} + +int mas_writereg(int reg, unsigned short val) +{ + int i; + unsigned char buf[16]; + + i=0; + buf[i++] = MAS_DATA_WRITE; + buf[i++] = 0x90 | reg >> 4; + buf[i++] = ((reg & 0x0f) << 4) | (val & 0x0f); + buf[i++] = (val >> 12) & 0xff; + buf[i++] = (val >> 4) & 0xff; + + /* send write command */ + if (i2c_write(MAS_DEV_WRITE,buf,i)) + { + return -1; + } + return 0; +} + +/* note: 'len' is number of 32-bit words, not number of bytes! */ +int mas_devread(unsigned long *dest, int len) +{ + unsigned char* ptr = (unsigned char*)dest; + int ret = 0; + int i; + + /* handle read-back */ + i2c_start(); + i2c_outb(MAS_DEV_WRITE); + if (i2c_getack()) { + i2c_outb(MAS_DATA_READ); + if (i2c_getack()) { + i2c_start(); + i2c_outb(MAS_DEV_READ); + if (i2c_getack()) { + for (i=0;len;i++) { + len--; + ptr[i*4+1] = i2c_inb(0); + ptr[i*4+0] = i2c_inb(0); + ptr[i*4+3] = i2c_inb(0); + if(len) + ptr[i*4+2] = i2c_inb(0); + else + ptr[i*4+2] = i2c_inb(1); /* NAK the last byte */ + } + } + else + ret = -3; + } + else + ret = -2; + } + else + ret = -1; + + i2c_stop(); + + return ret; +} diff --git a/firmware/mas.h b/firmware/mas.h new file mode 100644 index 0000000000..65e23f1498 --- /dev/null +++ b/firmware/mas.h @@ -0,0 +1,54 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef _MAS_H_ +#define _MAS_H_ + +#define MAS_BANK_D0 0 +#define MAS_BANK_D1 1 + +/* + MAS I2C defs +*/ +#define MAS_ADR 0x3a +#define MAS_DEV_WRITE (MAS_ADR | 0x00) +#define MAS_DEV_READ (MAS_ADR | 0x01) + +/* registers..*/ +#define MAS_DATA_WRITE 0x68 +#define MAS_DATA_READ 0x69 +#define MAS_CONTROL 0x6a + +/* + * MAS register + */ +#define MAS_REG_DCCF 0x8e +#define MAS_REG_MUTE 0xaa +#define MAS_REG_PIODATA 0xc8 +#define MAS_REG_StartUpConfig 0xe6 +#define MAS_REG_KPRESCALE 0xe7 +#define MAS_REG_KBASS 0x6b +#define MAS_REG_KTREBLE 0x6f + +int mas_readmem(int bank, int addr, unsigned long* dest, int len); +int mas_writemem(int bank, int addr, unsigned long* src, int len); +int mas_devread(unsigned long *buf, int len); +int mas_readreg(int reg); +int mas_writereg(int reg, unsigned short val); + +#endif diff --git a/firmware/test/i2c/Makefile b/firmware/test/i2c/Makefile new file mode 100644 index 0000000000..831299f787 --- /dev/null +++ b/firmware/test/i2c/Makefile @@ -0,0 +1,44 @@ +CC = sh-elf-gcc +LD = sh-elf-ld +AR = sh-elf-ar +AS = sh-elf-as +OC = sh-elf-objcopy + +INCLUDES=-I../../ + +CFLAGS = -g -Wall -m1 -nostdlib -Wstrict-prototypes -fomit-frame-pointer -fschedule-insns -fno-builtin $(INCLUDES) +AFLAGS += -small -relax + +OBJS= crt0.o main.o ../../lcd.o ../../i2c.o ../../mas.o ../../debug.o + +%.o: %.S + $(CC) -o $@ $(CFLAGS) $(INCLUDES) $(DEFS) -c $< + + +all : archos.mod + +main.o: main.c + +archos.elf : $(OBJS) app.lds + $(CC) -nostartfiles -o archos.elf $(OBJS) -lgcc -Tapp.lds -Wl,-Map,archos.map + +archos.bin : archos.elf + $(OC) -O binary archos.elf archos.bin + +archos.asm: archos.bin + sh2d -sh1 archos.bin > archos.asm + +archos.mod : archos.bin + scramble archos.bin archos.mod + +archos.mod.gz : archos.mod + gzip -f archos.mod + +dist: + tar czvf dist.tar.gz Makefile main.c start.s app.lds + +clean: + -rm -f *.x *.i *.o *.elf *.bin *.map *.mod *.bak *~ + +install: + mount /mnt/archos; cp archos.mod /mnt/archos; umount /mnt/archos diff --git a/firmware/test/i2c/app.lds b/firmware/test/i2c/app.lds new file mode 100644 index 0000000000..4b7e5f4c6f --- /dev/null +++ b/firmware/test/i2c/app.lds @@ -0,0 +1,23 @@ +ENTRY(start) +OUTPUT_FORMAT(elf32-sh) +SECTIONS +{ + .text 0x09018000 : + { + *(.rodata) + *(.text) + } + + .data : + { + *(.data) + } + + .bss : + { + *(.bss) + _end = . + 0x8000; + _stack = . + 0x9000; + _edata = .; + } +} diff --git a/firmware/test/i2c/crt0.S b/firmware/test/i2c/crt0.S new file mode 100644 index 0000000000..5f0ef2d64e --- /dev/null +++ b/firmware/test/i2c/crt0.S @@ -0,0 +1,49 @@ + .section .text + .global start +start: + mov.l stack_k,r15 + + ! zero out bss + mov.l edata_k,r0 + mov.l end_k,r1 + mov #0,r2 +start_l: + mov.l r2,@r0 + add #4,r0 + cmp/ge r0,r1 + bt start_l + nop + +#if defined (__SH3E__) || defined(__SH4_SINGLE__) || defined(__SH4__) || defined(__SH4_SINGLE_ONLY) + mov.l set_fpscr_k, r1 + jsr @r1 + mov #0,r4 + lds r3,fpscr +#endif /* defined (__SH3E__) || defined(__SH4_SINGLE__) || defined(__SH4__) || defined(__SH4_SINGLE_ONLY__) */ + + ! call the mainline + mov.l main_k,r0 + jsr @r0 + nop +.hoo: + bra .hoo + + .align 2 +#if defined (__SH3E__) || defined(__SH4_SINGLE__) || defined(__SH4__) || defined(__SH4_SINGLE_ONLY__) +set_fpscr_k: + .long ___set_fpscr +#endif /* defined (__SH3E__) || defined(__SH4_SINGLE__) || defined(__SH4__) || defined(SH4_SINGLE_ONLY) */ +stack_k: + .long _stack +edata_k: + .long _edata +end_k: + .long _end +main_k: + .long _main + +#ifdef __ELF__ + .section .stack,"aw" +#else + .section .stack +#endif diff --git a/firmware/test/i2c/main.c b/firmware/test/i2c/main.c new file mode 100644 index 0000000000..588236aff1 --- /dev/null +++ b/firmware/test/i2c/main.c @@ -0,0 +1,128 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 by Linus Nielsen Feltzing + * + * All files in this archive are subject to the GNU General Public License. + * See the file COPYING in the source tree root for full license agreement. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "i2c.h" +#include "mas.h" +#include "sh7034.h" +#include "debug.h" + +int strlen(unsigned char* str) +{ + int i=0; + while (*str++) + i++; + return i; +} + +int main(void) +{ + char buf[40]; + char str[32]; + int i=0; + + /* Clear it all! */ + SSR1 &= ~(SCI_RDRF | SCI_ORER | SCI_PER | SCI_FER); + + /* This enables the serial Rx interrupt, to be able to exit into the + debugger when you hit CTRL-C */ + SCR1 |= 0x40; + SCR1 &= ~0x80; + asm ("ldc\t%0,sr" : : "r"(0<<4)); + + debugf("Olle: %d\n", 7); + + i2c_init(); + debug("I2C Init done\n"); + i=mas_readmem(MAS_BANK_D1,0xff6,(unsigned long*)buf,2); + if (i) { + debugf("Error - mas_readmem() returned %d\n", i); + while(1); + } + + i = buf[0] | buf[1] << 8; + debugf("MAS version: %x\n", i); + i = buf[4] | buf[5] << 8; + debugf("MAS revision: %x\n", i); + + i=mas_readmem(MAS_BANK_D1,0xff9,(unsigned long*)buf,7); + if (i) { + debugf("Error - mas_readmem() returned %d\n", i); + while(1); + } + + for(i = 0;i < 7;i++) + { + str[i*2+1] = buf[i*4]; + str[i*2] = buf[i*4+1]; + } + str[i*2] = 0; + debugf("Description: %s\n", str); + + i=mas_readreg(0xe6); + if (i < 0) { + debugf("Error - mas_readreg() returned %d\n", i); + while(1); + } + + debugf("Register 0xe6: %x\n", i); + + + debugf("Writing register 0xaa\n"); + + i=mas_writereg(0xaa, 0x1); + if (i < 0) { + debugf("Error - mas_writereg() returned %d\n", i); + while(1); + } + + i=mas_readreg(0xaa); + if (i < 0) { + debugf("Error - mas_readreg() returned %d\n", i); + while(1); + } + + debugf("Register 0xaa: %x\n", i); + + debugf("Writing register 0xaa again\n"); + + i=mas_writereg(0xaa, 0); + if (i < 0) { + debugf("Error - mas_writereg() returned %d\n", i); + while(1); + } + + i=mas_readreg(0xaa); + if (i < 0) { + debugf("Error - mas_readreg() returned %d\n", i); + while(1); + } + + debugf("Register 0xaa: %x\n", i); + + while(1); +} + +extern const void stack(void); + +const void* vectors[] __attribute__ ((section (".vectors"))) = +{ + main, /* Power-on reset */ + stack, /* Power-on reset (stack pointer) */ + main, /* Manual reset */ + stack /* Manual reset (stack pointer) */ +};