Permalink
Browse files

o Adding a "keyboard" firmware for the keyboard project.

o Adding a "timer" firmware for testing the fx2 timer code.
  • Loading branch information...
1 parent 25c81f8 commit ce18cf4763cedbbb765f29ef8b264ae15d581f9f @trygvis committed Aug 31, 2010
View
@@ -3,7 +3,7 @@ include ../javalibusb1/Makefile.$(shell uname -s)
CCFLAGS+=-Werror
CCFLAGS+=-std=c99
-CCFLAGS+=-I../javalibusb1
+CCFLAGS+=-I../javalibusb1/src/main/include
LDFLAGS+=-L/opt/local/lib
LDFLAGS+=-lftdi
@@ -21,11 +21,15 @@ serial_read: serial_read.c
@echo CC $<
@$(CC) $(CCFLAGS) $(CCFLAGS) -I/opt/local/include $(LDFLAGS) -o $@ $<
-test-echo: test-echo.c ../javalibusb1/usbw.o
+test-echo: test-echo.c usbw.o
@echo CC $<
- @$(CC) $(CCFLAGS) $(CCFLAGS) $(LDFLAGS) -o $@ $^
+ $(CC) $(CCFLAGS) $(CCFLAGS) $(LDFLAGS) -o $@ $^
+
+usbw.o: ../javalibusb1/src/main/c/usbw.c ../javalibusb1/src/main/include/usbw.h
+ @echo CC $<
+ $(CC) $(CCFLAGS) $(CCFLAGS) -c -o $@ $<
clean:
- rm -f $(BINS)
+ rm -f $(BINS) $(wildcard *.o)
.PHONY: clean
View
@@ -109,8 +109,8 @@ int main(int argc, char* argv[]) {
bytes_in_count += transferred_in;
transfer_count++;
-// printf("transferred_in=%d\n", transferred_in);
- /*
+ printf("transferred_in=%d\n", transferred_in);
+
for(int i = 0; i < transferred_in; i++) {
if(i == 0) {
@@ -127,7 +127,6 @@ int main(int argc, char* argv[]) {
printf("%02x ", in_data[i]);
}
printf("\n");
- */
}
done:
View
@@ -21,20 +21,29 @@ else
OPT_D=-d=$(DEVICE)
endif
-firmwares=echo
+firmwares=echo keyboard timer
+
+#
+# Targets
+#
+
+all: target $(patsubst %,target/%.rel,$(firmwares)) $(patsubst %,target/%.ihx,$(firmwares))
+keyboard.c: fx2bits.h fx2timer.h
+timer.c: fx2bits.h fx2timer.h
+fx2timer.c: fx2timer.h
#
# Dependencies
#
target/echo.ihx: target/echo-descriptor.rel target/echo-dscr.rel
+target/keyboard.ihx: target/keyboard-descriptor.rel target/keyboard-dscr.rel target/fx2timer.rel
+target/timer.ihx: target/keyboard-descriptor.rel target/keyboard-dscr.rel target/fx2timer.rel
#
-# Targets
+# Common targets
#
-all: target $(patsubst %,target/%.rel,$(firmwares)) $(patsubst %,target/%.ihx,$(firmwares))
-
clean:
@echo Cleaning...
@rm -rf target
@@ -55,7 +64,7 @@ program-%: target/%.ihx
target/%.rel: %.c
@echo CC $<
@mkdir -p target
- $(CC) $(SDCCFLAGS) -c -o $@ $<
+ @$(CC) $(SDCCFLAGS) -c -o $@ $<
target/%.rel: %.asm
@echo AS $<
@@ -67,4 +76,4 @@ target/%.rel: %.asm
target/%.ihx: target/%.rel
@echo LD $@
- $(CC) $(SDCCFLAGS) -Wl"-b INT2JT = 0x1A00" -Wl"-b DSCR_AREA=0x3e00" -o $@ $^ fx2.lib -L$(FX2LIBDIR)/lib
+ @$(CC) $(SDCCFLAGS) -Wl"-b INT2JT=0x1A00" -Wl"-b DSCR_AREA=0x3e00" -o $@ $^ fx2.lib -L$(FX2LIBDIR)/lib
View
@@ -0,0 +1,49 @@
+#ifndef FX2_BITS_H
+#define FX2_BITS_H
+
+__xdata __at 0x0088 union {
+ struct {
+ unsigned char IT0:1;
+ unsigned char IE0:1;
+ unsigned char IT1:1;
+ unsigned char IE1:1;
+ unsigned char TR0:1;
+ unsigned char TF0:1;
+ unsigned char TR1:1;
+ unsigned char TF1:1;
+ };
+} bTCON;
+
+__xdata __at 0x0089 union {
+ struct {
+ unsigned char M_0:2;
+ unsigned char C_T0:1;
+ unsigned char GATE0:1;
+ unsigned char M_1:2;
+ unsigned char C_T1:1;
+ unsigned char GATE1:1;
+ };
+} bTMOD;
+
+__xdata __at 0x008E union {
+ struct {
+ unsigned char MD:3;
+ unsigned char T0M:3;
+ unsigned char T1M:3;
+ unsigned char T2M:3;
+ };
+} bCKCON;
+
+__xdata __at 0xE600 union {
+ struct {
+ unsigned char :1;
+ unsigned char CLKOE:1;
+ unsigned char CLKINV:1;
+ unsigned char CLKSPD:2;
+ unsigned char PORTCSTB:1;
+ unsigned char :1;
+ unsigned char :1;
+ };
+} bCPUCS;
+
+#endif
View
@@ -0,0 +1,14 @@
+#ifndef FX2_EXTRA_H
+#define FX2_EXTRA_H
+
+// TODO: These (or some defines like it) should go into fx2lib
+#define EPCFG_DIRECTION_IN bmBIT6
+#define EPCFG_DIRECTION_OUT 0
+#define EPCFG_TYPE_ISO bmBIT4
+#define EPCFG_TYPE_BULK bmBIT5
+#define EPCFG_TYPE_INT bmBIT5 | bmBIT4
+#define EPCFG_BUFFER_QUAD 0
+#define EPCFG_BUFFER_DOUBLE bmBIT1
+#define EPCFG_BUFFER_TRIPLE bmBIT3 | bmBIT2
+
+#endif
View
@@ -0,0 +1,139 @@
+#include "fx2timer.h"
+#include "fx2macros.h"
+#include "fx2ints.h"
+#include "fx2bits.h"
+
+// TODO: check the doxygen syntax in the comments
+
+/**
+CKCON (SFR 0x8E) Timer Rate Control Bits:
+
+CKCON.5 T2M - Timer 2 clock select.
+When T2M = 0, Timer 2 uses CLKOUT/12 (for compatibility with standard 8051);
+when T2M = 1, Timer 2 uses CLKOUT/4. This bit has no effect when Timer 2 is
+configured for baud rate generation.
+
+CKCON.4 T1M - Timer 1 clock select.
+When T1M = 0, Timer 1 uses CLKOUT/12 (for compatibility with standard 8051);
+when T1M = 1, Timer 1 uses CLKOUT/4.
+
+CKCON.3 T0M - Timer 0 clock select.
+When T0M = 0, Timer 0 uses CLKOUT/12 (for compatibility with standard 8051);
+when T0M = 1, Timer 0 uses CLKOUT/4.
+
+ */
+
+/**
+ * Sets up timer0 to call timer0_callback() at the specified interval. It
+ * looks at the currently configured CPU frequency and adjust accordingly.
+ *
+ * Highest values:
+ * \verbatim
+ *
+ * CPU Freq Interval
+ * 12MHz 1023
+ * 24MHz 511
+ * 48MHz 255
+ * \endverbatim
+ *
+ * It it possible to improve the size of intervals supported with more
+ * accounting work.
+ *
+ * Do not give to small values here as the precision will significantly
+ * degrade because of the amount of time the CPU and C code spends in
+ * handling interrupts.
+ *
+ * To properly use this remember to call fx2_timer0_isr() in your ISR for timer 0.
+ *
+ * Example code:
+ * \code
+ * void timer0_isr() __interrupt TF0_ISR {
+ * fx2_timer0_isr();
+ * other stuff..
+ * }
+ * \endcode
+ *
+ * \param us Number of micro seconds between each callback.
+ */
+#define bmTMOD_TIMER0_MODE (bmBIT1 | bmBIT0)
+#define bmTMOD_TIMER0_COUNTER bmBIT2
+#define bmTMOD_TIMER0_GATE bmBIT3
+
+#define bmTMOD_TIMER1_MODE (bmBIT5 | bmBIT4)
+#define bmTMOD_TIMER1_COUNTER bmBIT6
+#define bmTMOD_TIMER1_GATE bmBIT7
+
+// Mode 0: 13-bit counter
+#define TMOD_TIMER0_MODE_0 0x00
+// Mode 1: 16-bit counter
+#define TMOD_TIMER0_MODE_1 0x01
+// Mode 2: 8-Bit Counter with Auto-Reload
+#define TMOD_TIMER0_MODE_2 0x02
+// Mode 3: Two 8-bit counters
+#define TMOD_TIMER0_MODE_3 0x03
+
+#define bmCKCON_T0 bmBIT3
+
+static WORD timer0us;
+static BYTE timer0us_tl;
+static BYTE timer0us_th;
+
+void fx2_setup_timer0(WORD us) {
+
+/*
+Duty cycle:
+ 12MHz = 83.33ns, 12MHz / 4 = 333.3ns, 12MHz / 12 = 1us
+ 48MHz = 20.83ns, 48MHz / 4 = 83.33ns, 48MHz / 12 = 250ns
+
+Freq | Divisor | 1us in cycles | 8bit in us | 16bit in us
+48MHz | 4 | 12 | 21.3 ~= 21 | 5461.3 ~= 5461
+48MHz | 12 | 4 | 64 | 16384
+12MHz | 4 | 3 | 85 | 21845.3 ~= 21845
+12MHz | 12 | 1 | 256 | 65536
+
+ */
+
+ timer0us = us;
+ // Clear old flags
+ TMOD = TMOD & ~(bmTMOD_TIMER0_MODE | bmTMOD_TIMER0_COUNTER | bmTMOD_TIMER0_GATE);
+
+ switch(CPUFREQ) {
+ case CLK_12M:
+ if(us >= 256) {
+ TMOD |= TMOD_TIMER0_MODE_1;
+ us = 65536 - us;
+ timer0us_tl = LSB(us);
+ timer0us_th = MSB(us);
+
+ TL0 = timer0us_tl;
+ TH0 = timer0us_th;
+ CKCON &= ~bmCKCON_T0; // div = 12
+ }
+ else {
+ TMOD |= TMOD_TIMER0_MODE_2;
+ TL0 = 0;
+ if(us >= 86) {
+ TH0 = 256 - us;
+ CKCON &= ~bmCKCON_T0; // div = 12
+ }
+ else {
+ TH0 = 256 - (us * 3);
+ CKCON |= bmCKCON_T0; // div = 4
+ }
+ }
+ TR0 = 1;
+ ENABLE_TIMER0();
+ break;
+ }
+}
+
+void fx2_timer0_isr() {
+ switch(CPUFREQ) {
+ case CLK_12M:
+ if(timer0us >= 256) {
+ TL0 = timer0us_tl;
+ TH0 = timer0us_th;
+ }
+ }
+ timer0_callback();
+}
View
@@ -0,0 +1,11 @@
+#ifndef FX2_TIMER_H
+#define FX2_TIMER_H
+
+#include "fx2types.h"
+
+void fx2_timer0_isr();
+void fx2_setup_timer0(WORD us);
+
+extern void timer0_callback();
+
+#endif
Oops, something went wrong. Retry.

0 comments on commit ce18cf4

Please sign in to comment.