From 98cba32ca9d62f6e4b7e735d177dba699b8d0698 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 6 Jun 2015 12:38:36 +0100 Subject: inititalcommit --- .gitignore | 6 + .gitmodules | 3 + Makefile.include | 48 +++++++ Makefile.rules | 251 ++++++++++++++++++++++++++++++++++ app/.Makefile.swo | Bin 0 -> 12288 bytes app/.Makefile.swp | Bin 0 -> 4096 bytes app/.main.c.swp | Bin 0 -> 4096 bytes app/.usart.c.swn | Bin 0 -> 4096 bytes app/.usart.c.swo | Bin 0 -> 16384 bytes app/Makefile | 61 +++++++++ app/atkbd.c | 325 +++++++++++++++++++++++++++++++++++++++++++++ app/consumer.c | 93 +++++++++++++ app/dfu.c | 80 +++++++++++ app/gdb.script | 2 + app/keyboard.c | 117 ++++++++++++++++ app/pins.h | 49 +++++++ app/project.h | 26 ++++ app/prototypes.h | 57 ++++++++ app/ring.c | 75 +++++++++++ app/ring.h | 14 ++ app/stdio.c | 83 ++++++++++++ app/ticker.c | 76 +++++++++++ app/tims_keyboard.c | 44 ++++++ app/tims_keyboard.ld | 43 ++++++ app/usart.c | 100 ++++++++++++++ app/usb.c | 133 +++++++++++++++++++ board/STM32F103R_BOARD.cfg | 6 + boot/Makefile | 32 +++++ boot/project.h | 20 +++ boot/usbdfu.c | 290 ++++++++++++++++++++++++++++++++++++++++ boot/usbdfu.ld | 40 ++++++ docs/rm0008.pdf | Bin 0 -> 13016697 bytes docs/stm32f103c8.pdf | Bin 0 -> 1697666 bytes id.h | 5 + interface/j-link.cfg | 5 + libopencm3 | 1 + 36 files changed, 2085 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 Makefile.include create mode 100644 Makefile.rules create mode 100644 app/.Makefile.swo create mode 100644 app/.Makefile.swp create mode 100644 app/.main.c.swp create mode 100644 app/.usart.c.swn create mode 100644 app/.usart.c.swo create mode 100644 app/Makefile create mode 100644 app/atkbd.c create mode 100644 app/consumer.c create mode 100644 app/dfu.c create mode 100644 app/gdb.script create mode 100644 app/keyboard.c create mode 100644 app/pins.h create mode 100644 app/project.h create mode 100644 app/prototypes.h create mode 100644 app/ring.c create mode 100644 app/ring.h create mode 100644 app/stdio.c create mode 100644 app/ticker.c create mode 100644 app/tims_keyboard.c create mode 100644 app/tims_keyboard.ld create mode 100644 app/usart.c create mode 100644 app/usb.c create mode 100644 board/STM32F103R_BOARD.cfg create mode 100644 boot/Makefile create mode 100644 boot/project.h create mode 100644 boot/usbdfu.c create mode 100644 boot/usbdfu.ld create mode 100644 docs/rm0008.pdf create mode 100644 docs/stm32f103c8.pdf create mode 100644 id.h create mode 100644 interface/j-link.cfg create mode 160000 libopencm3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd6b363 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.gitmodules +*.o +*.d +*.map +*.hex +*.elf diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..29e93ae --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libopencm3"] + path = libopencm3 + url = git://github.com/libopencm3/libopencm3.git diff --git a/Makefile.include b/Makefile.include new file mode 100644 index 0000000..3eccc25 --- /dev/null +++ b/Makefile.include @@ -0,0 +1,48 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +LIBNAME = opencm3_stm32f1 +DEFS = -DSTM32F1 + +FP_FLAGS ?= -msoft-float +ARCH_FLAGS = -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd + +################################################################################ +# OpenOCD specific variables + +#OOCD ?= openocd +#OOCD_INTERFACE ?= flossjtag +#OOCD_BOARD ?= olimex_stm32_h103 + +OOCD ?= openocd +OOCD_INTERFACE ?= ../interface/j-link.cfg +OOCD_BOARD ?= ../board/STM32F103R_BOARD.cfg + +################################################################################ +# Black Magic Probe specific variables +# Set the BMP_PORT to a serial port and then BMP is used for flashing +BMP_PORT ?= + +################################################################################ +# texane/stlink specific variables +#STLINK_PORT ?= :4242 + + +include ../Makefile.rules diff --git a/Makefile.rules b/Makefile.rules new file mode 100644 index 0000000..73bdf20 --- /dev/null +++ b/Makefile.rules @@ -0,0 +1,251 @@ +# +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## Copyright (C) 2010 Piotr Esden-Tempski +## Copyright (C) 2013 Frantisek Burian +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +# Be silent per default, but 'make V=1' will show all compiler calls. +ifneq ($(V),1) +Q := @ +NULL := 2>/dev/null +endif + +############################################################################### +# Executables + +PREFIX ?= arm-none-eabi + +CC := $(PREFIX)-gcc +CXX := $(PREFIX)-g++ +LD := $(PREFIX)-gcc +AR := $(PREFIX)-ar +AS := $(PREFIX)-as +OBJCOPY := $(PREFIX)-objcopy +OBJDUMP := $(PREFIX)-objdump +GDB := $(PREFIX)-gdb +STFLASH = $(shell which st-flash) +STYLECHECK := /checkpatch.pl +STYLECHECKFLAGS := --no-tree -f --terse --mailback +STYLECHECKFILES := $(shell find . -name '*.[ch]') + + +############################################################################### +# Source files + +LDSCRIPT ?= $(BINARY).ld + +#OBJS += $(BINARY).o + + +ifeq ($(strip $(OPENCM3_DIR)),) +# user has not specified the library path, so we try to detect it + +# where we search for the library +LIBPATHS := ./libopencm3 ../libopencm3 + +OPENCM3_DIR := $(wildcard $(LIBPATHS:=/locm3.sublime-project)) +OPENCM3_DIR := $(firstword $(dir $(OPENCM3_DIR))) + +ifeq ($(strip $(OPENCM3_DIR)),) +$(warning Cannot find libopencm3 library in the standard search paths.) +$(error Please specify it through OPENCM3_DIR variable!) +endif +endif + +ifeq ($(V),1) +$(info Using $(OPENCM3_DIR) path to library) +endif + +INCLUDE_DIR = $(OPENCM3_DIR)/include +LIB_DIR = $(OPENCM3_DIR)/lib +SCRIPT_DIR = $(OPENCM3_DIR)/scripts + +############################################################################### +# C flags + +CFLAGS += -Os -g +CFLAGS += -Wextra -Wimplicit-function-declaration +CFLAGS += -Wmissing-prototypes -Wstrict-prototypes +CFLAGS += -fno-common -ffunction-sections -fdata-sections + +############################################################################### +# C++ flags + +CXXFLAGS += -Os -g +CXXFLAGS += -Wextra -Wshadow -Wredundant-decls -Weffc++ +CXXFLAGS += -fno-common -ffunction-sections -fdata-sections + +############################################################################### +# C & C++ preprocessor common flags + +CPPFLAGS += -MD +CPPFLAGS += -Wall -Wundef + +INCLUDES = -I$(INCLUDE_DIR) +DEFINES = $(DEFS) + +CPPFLAGS += $(INCLUDES) $(DEFINES) + +############################################################################### +# Linker flags + +LDFLAGS += --static -nostartfiles +LDFLAGS += -L$(LIB_DIR) +LDFLAGS += -T$(LDSCRIPT) +LDFLAGS += -Wl,-Map=$(*).map +LDFLAGS += -Wl,--gc-sections +ifeq ($(V),99) +LDFLAGS += -Wl,--print-gc-sections +endif + +############################################################################### +# Used libraries + +LDLIBS += -l$(LIBNAME) +LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group + +############################################################################### +############################################################################### +############################################################################### + +.SUFFIXES: .elf .bin .hex .srec .list .map .images .dfu +.SECONDEXPANSION: +.SECONDARY: + +all: elf + +elf: $(BINARY).elf +bin: $(BINARY).bin +hex: $(BINARY).hex +srec: $(BINARY).srec +list: $(BINARY).list + +images: $(BINARY).images +flash: $(BINARY).flash + +%.images: %.bin %.hex %.srec %.list %.map %.dfu + @#printf "*** $* images generated ***\n" + +%.bin: %.elf + @#printf " OBJCOPY $(*).bin\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).bin + +%.hex: %.elf + @#printf " OBJCOPY $(*).hex\n" + $(Q)$(OBJCOPY) -Oihex $(*).elf $(*).hex + +%.dfu: %.elf + @#printf " OBJCOPY $(*).dfu\n" + $(Q)$(OBJCOPY) -Obinary $(*).elf $(*).dfu + +%.srec: %.elf + @#printf " OBJCOPY $(*).srec\n" + $(Q)$(OBJCOPY) -Osrec $(*).elf $(*).srec + +%.list: %.elf + @#printf " OBJDUMP $(*).list\n" + $(Q)$(OBJDUMP) -S $(*).elf > $(*).list + +fish: + echo %.elf %.map: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a + echo $(BINARY).elf + +%.elf %.map: $(OBJS) $(LDSCRIPT) $(LIB_DIR)/lib$(LIBNAME).a + @#printf " LD $(*).elf\n" + $(Q)$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(*).elf + +%.o: %.c + @#printf " CC $(*).c\n" + $(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).c + +%.o: %.cxx + @#printf " CXX $(*).cxx\n" + $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).cxx + +%.o: %.cpp + @#printf " CXX $(*).cpp\n" + $(Q)$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(ARCH_FLAGS) -o $(*).o -c $(*).cpp + +clean: + @#printf " CLEAN\n" + $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map *~ *.dfu + +stylecheck: $(STYLECHECKFILES:=.stylecheck) +styleclean: $(STYLECHECKFILES:=.styleclean) + +# the cat is due to multithreaded nature - we like to have consistent chunks of text on the output +%.stylecheck: % + $(Q)$(SCRIPT_DIR)$(STYLECHECK) $(STYLECHECKFLAGS) $* > $*.stylecheck; \ + if [ -s $*.stylecheck ]; then \ + cat $*.stylecheck; \ + else \ + rm -f $*.stylecheck; \ + fi; + +%.styleclean: + $(Q)rm -f $*.stylecheck; + + +%.stlink-flash: %.bin + @printf " FLASH $<\n" + $(Q)$(STFLASH) write $(*).bin 0x8000000 + +ifeq ($(STLINK_PORT),) +ifeq ($(BMP_PORT),) +ifeq ($(OOCD_SERIAL),) +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) \ + -c "init" -c "reset init" \ + -c "flash write_image erase $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +else +%.flash: %.hex + @printf " FLASH $<\n" + @# IMPORTANT: Don't use "resume", only "reset" will work correctly! + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) \ + -c "ft2232_serial $(OOCD_SERIAL)" \ + -c "init" -c "reset init" \ + -c "flash write_image erase $(*).hex" \ + -c "reset" \ + -c "shutdown" $(NULL) +endif +else +%.flash: %.elf + @printf " GDB $(*).elf (flash)\n" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(BMP_PORT)' \ + -x $(SCRIPT_DIR)/black_magic_probe_flash.scr \ + $(*).elf +endif +else +%.flash: %.elf + @printf " GDB $(*).elf (flash)\n" + $(Q)$(GDB) --batch \ + -ex 'target extended-remote $(STLINK_PORT)' \ + -x $(SCRIPT_DIR)/stlink_flash.scr \ + $(*).elf +endif + +.PHONY: images clean stylecheck styleclean elf bin hex srec list + +-include $(OBJS:.o=.d) diff --git a/app/.Makefile.swo b/app/.Makefile.swo new file mode 100644 index 0000000..c28d39f Binary files /dev/null and b/app/.Makefile.swo differ diff --git a/app/.Makefile.swp b/app/.Makefile.swp new file mode 100644 index 0000000..db8a3c3 Binary files /dev/null and b/app/.Makefile.swp differ diff --git a/app/.main.c.swp b/app/.main.c.swp new file mode 100644 index 0000000..633f747 Binary files /dev/null and b/app/.main.c.swp differ diff --git a/app/.usart.c.swn b/app/.usart.c.swn new file mode 100644 index 0000000..88c369d Binary files /dev/null and b/app/.usart.c.swn differ diff --git a/app/.usart.c.swo b/app/.usart.c.swo new file mode 100644 index 0000000..e5985d3 Binary files /dev/null and b/app/.usart.c.swo differ diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..39d9779 --- /dev/null +++ b/app/Makefile @@ -0,0 +1,61 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +CPROTO=cproto +PROG=tims_keyboard + + +V=1 +default: ${PROG}.elf + +CSRCS=tims_keyboard.c dfu.c keyboard.c usb.c consumer.c atkbd.c ring.c usart.c stdio.c ticker.c + + +BINARY = ${PROG} +OBJS = ${CSRCS:%.c=%.o} + +include ../Makefile.include + +CFLAGS+=-Wno-redundant-decls -Wno-unused-parameter + +DID=$(shell printf '\#include "id.h"\nID_PRODUCT' | ${CC} -I.. -E - | grep -v ^\# ) + +INCLUDES += -I.. + +dfu:${PROG}.dfu + dfu-util -R -a 0 -d 1d6b:${DID} -s 0x08002000:leave -D $< + +program: ${PROG}.elf + echo halt | nc -t localhost 4444 + echo flash write_image erase ${PWD}/$< 0x2000 | nc -t localhost 4444 + echo reset run | nc -t localhost 4444 + +protos: + echo -n > prototypes.h + ${CPROTO} $(INCLUDES) $(DEFINES) -e -v ${CSRCS} > prototypes.h.tmp + mv -f prototypes.h.tmp prototypes.h + +ds: + $(Q)$(OOCD) -f $(OOCD_INTERFACE) \ + -f $(OOCD_BOARD) + +debug: ${PROG}.elf + ${PREFIX}-gdb -x gdb.script ${PROG}.elf + + diff --git a/app/atkbd.c b/app/atkbd.c new file mode 100644 index 0000000..0dfc9c8 --- /dev/null +++ b/app/atkbd.c @@ -0,0 +1,325 @@ +#include "project.h" + +#define KBCLK (1UL << 0) +#define KBCLK_PORT GPIOA +#define KBCLK_IRQ NVIC_EXTI0_IRQ + +#define KBDAT (1UL << 1) +#define KBDAT_PORT GPIOA + +/*Cope with lost sync */ +/*If we've had no bits for this long, it's definately the start of a new word, 72MHz units */ +#define RESYNC_GAP (7200000) /* 100ms */ + +#define ATKBD_TIMEOUT 1000 /*how long to wait for the keyboard to respond to a command in ms */ + + +typedef enum { + STATE_START=0, + STATE_BIT0, + STATE_BIT1, + STATE_BIT2, + STATE_BIT3, + STATE_BIT4, + STATE_BIT5, + STATE_BIT6, + STATE_BIT7, + STATE_PARITY, + STATE_STOP, +} atkbd_state; + + +#define ATKBD_CMD_SETLEDS 0xed +#define ATKBD_CMD_GSCANSET 0xf0 +#define ATKBD_CMD_SSCANSET 0xf0 +#define ATKBD_CMD_GETID 0xf2 +#define ATKBD_CMD_SETREP 0xf3 +#define ATKBD_CMD_ENABLE 0xf4 +#define ATKBD_CMD_RESET_DIS 0xf5 +#define ATKBD_CMD_RESET_DEF 0xf6 +#define ATKBD_CMD_SETALL_MB 0xf8 +#define ATKBD_CMD_SETALL_MBR 0xfa +#define ATKBD_CMD_RESET_BAT 0xff +#define ATKBD_CMD_RESEND 0xfe +#define ATKBD_CMD_EX_ENABLE 0xea +#define ATKBD_CMD_EX_SETLEDS 0xeb +#define ATKBD_CMD_OK_GETID 0xe8 + +#define ATKBD_RET_ECHO 0xee +#define ATKBD_RET_ACK 0xfa +#define ATKBD_RET_NAK 0xfe +#define ATKBD_RET_BAT 0xaa +#define ATKBD_RET_EMUL0 0xe0 +#define ATKBD_RET_EMUL1 0xe1 +#define ATKBD_RET_RELEASE 0xf0 +#define ATKBD_RET_HANJA 0xf1 +#define ATKBD_RET_HANGEUL 0xf2 +#define ATKBD_RET_ERR 0xff + +#define ATKBD_KEY_UNKNOWN 0 +#define ATKBD_KEY_NULL 255 + + +static int atkbd_ack; +static int atkbd_nack; +static int atkbd_bat; +static int atkbd_echo; + + + +static uint32_t cycle_diff(uint32_t a,uint32_t b) +{ +return b-a; +} + + +static void atkbd_dispatch(int emul,int key,int updown) +{ + +printf("KEY> %x %x %x\r\n",emul,key,updown); + + +} + +static void atkbd_data_dispatch(uint8_t byte) +{ +static int release; +static int emul; + +printf("ATKBD < 0x%02x\r\n",byte); + +switch (byte) { +case ATKBD_RET_ACK: + atkbd_ack++; + break; +case ATKBD_RET_NAK: + atkbd_nack++; + break; +case ATKBD_RET_BAT: + atkbd_bat++; + break; +case ATKBD_RET_ECHO: + atkbd_echo++; + break; +case ATKBD_RET_ERR: +case ATKBD_KEY_UNKNOWN: +case ATKBD_RET_HANJA: /*Don't handle japanese or korean for the moment*/ +case ATKBD_RET_HANGEUL: /*Don't handle japanese or korean for the moment*/ + /*All these need no action */ + break; +case ATKBD_RET_EMUL0: + emul=1; + break; +case ATKBD_RET_EMUL1: + emul=2; + break; +case ATKBD_RET_RELEASE: + release=1; + break; +default: + atkbd_dispatch(emul,byte,release); + emul=0; + release=0; +} + +} + + +void exti0_isr(void) +{ + static uint32_t last_interrupt=0; + static atkbd_state state=STATE_START; + static uint8_t atkbd_byte; + static int parity; + + uint32_t now,diff; + int d; + + d=!!GET(KBDAT); + + exti_reset_request(KBCLK); + + now=dwt_read_cycle_counter(); + diff=cycle_diff(last_interrupt,now); + last_interrupt=now; + + + if (diff>RESYNC_GAP) state=STATE_START; + + + switch (state) { + case STATE_START: + atkbd_byte=0; + parity=0; + if (!d) state++; + break; + case STATE_BIT0: + case STATE_BIT1: + case STATE_BIT2: + case STATE_BIT3: + case STATE_BIT4: + case STATE_BIT5: + case STATE_BIT6: + case STATE_BIT7: + atkbd_byte|=d<<(state-STATE_BIT0); + /* fall through*/ + case STATE_PARITY: + parity ^= d; + state++; + break; + case STATE_STOP: + if (d && parity) atkbd_data_dispatch(atkbd_byte); + state=STATE_START; + break; + } +} + + + +void atkbd_set(int clk,int dat) +{ +if (clk) { + MAP_INPUT_PU(KBCLK); +} else { + CLEAR(KBCLK); + MAP_OUTPUT_PP(KBCLK); +} + + +if (dat) { + MAP_INPUT_PU(KBDAT); +} else { + CLEAR(KBDAT); + MAP_OUTPUT_PP(KBDAT); +} + +} + + +int atkbd_send(uint8_t d) +{ +uint32_t then=ticks; +int parity=1; +uint32_t c; + + +nvic_disable_irq(KBCLK_IRQ); + +#define PS2_CLOCK_SEIZE_DELAY 200 +#define PS2_BIT_DELAY 10 + +atkbd_set(0,1); +delay_us(PS2_CLOCK_SEIZE_DELAY); +atkbd_set(0,0); +delay_us(PS2_BIT_DELAY); +atkbd_set(1,0); +delay_us(PS2_BIT_DELAY); + +for (c=1;c<0x100;c<<=1) { +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +atkbd_set(1,c&d); +parity^=!!(c&d); +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +} +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +atkbd_set(1,parity); +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +atkbd_set(1,1); +while (GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + + + +while (!GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + +printf("ATKBD > 0x%02x\r\n",d); + +return 0; + +err: +atkbd_set(1,1); +while (!GET(KBDAT)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; +while (!GET(KBCLK)) if (timed_out(then,ATKBD_TIMEOUT)) goto err; + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + +printf("ATKBD >! 0x%02x\r\n",d); +return -1; +} + +static int atkbd_reset(void) +{ +uint32_t then=ticks; +atkbd_bat=0; +atkbd_send(ATKBD_CMD_RESET_BAT); +while (!atkbd_bat) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_SETALL_MBR); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + + +return 0; +} + +int atkbd_request_echo(void) +{ +uint32_t then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_ECHO); +while (!atkbd_echo) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +return 0; +} + + +int atkbd_set_leds(uint8_t leds) +{ +uint32_t then=ticks; +atkbd_ack=0; +atkbd_send(ATKBD_CMD_SETLEDS); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +then=ticks; +atkbd_ack=0; +atkbd_send(leds); +while (!atkbd_ack) if (timed_out(then,ATKBD_TIMEOUT)) return -1; + +return 0; +} + + +void atkbd_init(void) +{ +atkbd_set(1,1); +delay_ms(200); + +nvic_enable_irq(NVIC_EXTI0_IRQ); + + +exti_select_source(KBCLK, KBCLK_PORT); +exti_set_trigger(KBCLK, EXTI_TRIGGER_FALLING); +exti_enable_request(KBCLK); + +exti_reset_request(KBCLK); +nvic_enable_irq(KBCLK_IRQ); + + +atkbd_reset(); +atkbd_request_echo(); +atkbd_set_leds(0x07); + + +} + + diff --git a/app/consumer.c b/app/consumer.c new file mode 100644 index 0000000..044c15b --- /dev/null +++ b/app/consumer.c @@ -0,0 +1,93 @@ +#include "project.h" + + +static const uint8_t consumer_report_descriptor[] = { + 0x05, 0x0c, /* Usage Page (Consumer Devices) */ + 0x09, 0x01, /* Usage (Consumer Control) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x01, /* Logical maximum (1) */ + 0x75, 0x01, /* Report size (1) */ + 0x95, 0x10, /* Report count (16) */ + + 0x09, 0xb5, /* USAGE (Scan Next Track) */ + 0x09, 0xb6, /* USAGE (Scan Prev Track) */ + 0x09, 0xb7, /* USAGE (Stop) */ + 0x09, 0xcd, /* USAGE (Play/Pause) */ + + 0x09, 0xe2, /* USAGE (Mute) */ + 0x09, 0xe9, /* USAGE (Volume up) */ + 0x09, 0xea, /* USAGE (Volume down) */ + 0x0a, 0x21, 0x02, /* USAGE (www search) */ + + 0x0a, 0x23, 0x02, /* USAGE (www home) */ + 0x0a, 0x24, 0x02, /* USAGE (www back) */ + 0x0a, 0x25, 0x02, /* USAGE (www forward) */ + 0x0a, 0x26, 0x02, /* USAGE (www stop) */ + + 0x0a, 0x27, 0x02, /* USAGE (www refresh) */ + 0x0a, 0x2a, 0x02, /* USAGE (www favourites) */ + 0x0a, 0x8a, 0x01, /* USAGE (Mail) */ + 0x0a, 0x92, 0x01, /* USAGE (Calculator) */ + + 0x81, 0x62, /* Input (data, variable, relative, preferreed) */ + 0xC0 /* End Collection */ +}; + + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) consumer_function = +{ + .hid_descriptor = + { + .bLength = sizeof (consumer_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (consumer_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor consumer_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 2, + .bInterval = 0x1 //0x20, +}; + +const struct usb_interface_descriptor consumer_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 1, /* consumer */ + .iInterface = 6, + + .endpoint = &consumer_endpoint, + + .extra = &consumer_function, + .extralen = sizeof (consumer_function), +}; + + +void +consumer_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) consumer_report_descriptor; + *len = sizeof (consumer_report_descriptor); +} + diff --git a/app/dfu.c b/app/dfu.c new file mode 100644 index 0000000..285c2b2 --- /dev/null +++ b/app/dfu.c @@ -0,0 +1,80 @@ +#include "project.h" + + +extern uint32_t dfu_flag; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof (struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 4, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 9, + + .extra = &dfu_function, + .extralen = sizeof (dfu_function), +}; + + +static void +dfu_detach_complete (usbd_device * usbd_dev, struct usb_setup_data *req) +{ + (void) req; + (void) usbd_dev; + + dfu_flag = 0xfee1dead; + + scb_reset_core (); +} + +int +dfu_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, + uint8_t ** buf, uint16_t * len, + void (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) +{ + (void) buf; + (void) len; + (void) usbd_dev; + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) + { + case DFU_GETSTATUS: + { + (*buf)[0] = DFU_STATUS_OK; + (*buf)[1] = 0; + (*buf)[2] = 0; + (*buf)[3] = 0; + (*buf)[4] = STATE_APP_IDLE; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = STATE_APP_IDLE; + *len = 1; + return 1; + case DFU_DETACH: + *complete = dfu_detach_complete; + return 1; + } + + return 0; + +} diff --git a/app/gdb.script b/app/gdb.script new file mode 100644 index 0000000..7cf9d09 --- /dev/null +++ b/app/gdb.script @@ -0,0 +1,2 @@ +target remote localhost:3333 +cont diff --git a/app/keyboard.c b/app/keyboard.c new file mode 100644 index 0000000..44eb6c6 --- /dev/null +++ b/app/keyboard.c @@ -0,0 +1,117 @@ +#include "project.h" + +static const uint8_t keyboard_report_descriptor[] = { + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x05, 0x07, /* Usage page (Key Codes) */ + 0x19, 0xE0, /* Usage minimum (224) */ + 0x29, 0xE7, /* Usage maximum (231) */ + 0x15, 0x00, /* Logical minimum (0) */ + 0x25, 0x01, /* Logical maximum (1) */ + 0x75, 0x01, /* Report size (1) */ + 0x95, 0x08, /* Report count (8) */ + 0x81, 0x02, /* Input (data, variable, absolute) */ + 0x95, 0x01, /* Report count (1) */ + 0x75, 0x08, /* Report size (8) */ + 0x81, 0x01, /* Input (constant) */ + 0x95, 0x06, /* Report count (6) */ + 0x75, 0x08, /* Report size (8) */ + 0x15, 0x00, /* Logical minimum (0) */ + //0x25, 0x65, /* Logical maximum (101) */ + 0x26, 0xff, 0x00, /* Logical maximum (255) */ + //0x26, 0x7f, 0x00, /* Logical maximum (255) */ + 0x05, 0x07, /* Usage page (key codes) */ + 0x19, 0x00, /* Usage minimum (0) */ + 0x2A, 0xff, 0x00, /* Usage maximum (255) */ +// 0x29, 0x65, /* Usage maximum (101) */ +// 0x2A, 0xff, 0x03, /* Usage maximum (1023) */ + 0x81, 0x00, /* Input (data, array) */ + 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */ + 0x09, 0x80, /* USAGE (System Control) */ + 0xA1, 0x01, /* COLLECTION (Application) */ + 0x75, 0x02, /* REPORT_SIZE (2) */ + 0x95, 0x01, /* REPORT_COUNT (1) */ + 0x15, 0x01, /* LOGICAL_MIN (1) */ + 0x25, 0x03, /* LOGICAL_MAX (3) */ + 0x09, 0x82, /* USAGE (System Sleep) */ + 0x09, 0x81, /* USAGE (System Power Down) */ + 0x09, 0x83, /* USAGE (System Wake Up) */ + 0x81, 0x60, /* INPUT (Data Ary Abs NPrf Null) */ + 0x75, 0x06, /* REPORT_SIZE (6) */ + 0x81, 0x03, /* INPUT (Cnst Var Abs) */ + 0xc0, /* END COLLECTION */ +0xC0 /* End Collection */ +}; + + +static const struct +{ + struct usb_hid_descriptor hid_descriptor; + struct + { + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; + } __attribute__ ((packed)) hid_report; +} __attribute__ ((packed)) keyboard_function = +{ + .hid_descriptor = + { + .bLength = sizeof (keyboard_function),.bDescriptorType = + USB_DT_HID,.bcdHID = 0x0100,.bCountryCode = 0,.bNumDescriptors = 1,} + ,.hid_report = + { + .bReportDescriptorType = USB_DT_REPORT,.wDescriptorLength = + sizeof (keyboard_report_descriptor),} +,}; + +const struct usb_endpoint_descriptor keyboard_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 9, + .bInterval = 0x1 //0x20, +}; + +const struct usb_interface_descriptor keyboard_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 1, /* keyboard */ + .iInterface = 5, + + .endpoint = &keyboard_endpoint, + + .extra = &keyboard_function, + .extralen = sizeof (keyboard_function), +}; + + +void +keyboard_get_descriptor (uint8_t ** buf, uint16_t * len) +{ + /* Handle the HID report descriptor. */ + *buf = (uint8_t *) keyboard_report_descriptor; + *len = sizeof (keyboard_report_descriptor); +} + +void +keyboard_test (void) +{ + static int c = 0; + uint8_t buf[9] = { 0, 0, 0, 0, 0, 0, 0, 0 ,0}; + + + buf[0] = (c >> 1) & 7; + + buf[2] = (c & 1) ? 12 : 0; + + c++; + + usbd_ep_write_packet (usbd_dev, 0x81, buf, 9); +} diff --git a/app/pins.h b/app/pins.h new file mode 100644 index 0000000..4c55d74 --- /dev/null +++ b/app/pins.h @@ -0,0 +1,49 @@ +#ifndef _PINS_H_ +#define _PINS_H_ + +/* st seem to change these with every chip revision */ + +#define MAP_AF(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, a ); \ + } while (0) + +/* STM32F1 doesn't have AF pull up, but also doesn't disconnect af inputs so just use regular pull up */ +#define MAP_AF_PU(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, a); \ + gpio_set( a ## _PORT, a); \ + } while (0) + +#define MAP_AF_OD(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, a ); \ + } while (0) + + +#define MAP_OUTPUT_PP(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, a ); \ + } while (0) + + +#define MAP_OUTPUT_OD(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, a ); \ + } while (0) + + +/* STM32F1 madly uses the output register to drive the other end of the resistor, so pull up */ +/* requires us to write a 1 there */ + +#define MAP_INPUT_PU(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, a); \ + gpio_set( a ## _PORT, a); \ + } while (0) + + +#define MAP_INPUT(a) do { \ + gpio_set_mode( a ## _PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, a); \ + } while (0) + + +#define CLEAR(a) gpio_clear( a ## _PORT, a) +#define SET(a) gpio_set( a ## _PORT, a) +#define GET(a) gpio_get( a ## _PORT, a) + +#endif diff --git a/app/project.h b/app/project.h new file mode 100644 index 0000000..24ca271 --- /dev/null +++ b/app/project.h @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "ring.h" +#include "pins.h" + + +#include "id.h" + + +#include "prototypes.h" diff --git a/app/prototypes.h b/app/prototypes.h new file mode 100644 index 0000000..52b504f --- /dev/null +++ b/app/prototypes.h @@ -0,0 +1,57 @@ +/* tims_keyboard.c */ +extern int main(void); +/* dfu.c */ +extern const struct usb_dfu_descriptor dfu_function; +extern const struct usb_interface_descriptor dfu_iface; +extern int dfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)); +/* keyboard.c */ +extern const struct usb_endpoint_descriptor keyboard_endpoint; +extern const struct usb_interface_descriptor keyboard_iface; +extern void keyboard_get_descriptor(uint8_t **buf, uint16_t *len); +extern void keyboard_test(void); +/* usb.c */ +extern const struct usb_device_descriptor dev; +extern const struct usb_interface ifaces[]; +extern const struct usb_config_descriptor config; +extern usbd_device *usbd_dev; +extern void usb_set_config(usbd_device *usbd_dev, uint16_t wValue); +extern void usb_init(void); +extern void usb_run(void); +/* consumer.c */ +extern const struct usb_endpoint_descriptor consumer_endpoint; +extern const struct usb_interface_descriptor consumer_iface; +extern void consumer_get_descriptor(uint8_t **buf, uint16_t *len); +/* ps2.c */ +extern void exti0_isr(void); +extern void atkbd_set(int clk, int dat); +extern int atkbd_send(uint8_t d); +extern void atkbd_init(void); +/* ring.c */ +extern void ring_init(volatile ring_t *r, uint8_t *buf, size_t len); +extern int ring_write_byte(volatile ring_t *r, uint8_t c); +extern int ring_read_byte(volatile ring_t *r, uint8_t *c); +extern int ring_write(volatile ring_t *r, uint8_t *buf, size_t len, int blocking); +extern int ring_empty(volatile ring_t *r); +/* usart.c */ +extern ring_t rx1_ring; +extern ring_t tx1_ring; +extern void usart1_isr(void); +extern void usart1_queue(uint8_t d); +extern void usart1_drain(void); +extern int usart1_write(char *ptr, int len, int blocking); +extern void usart_init(void); +/* stdio.c */ +extern int _open(const char *name, int flags, int mode); +extern int _close(int file); +extern int _write(int file, char *buf, int nbytes); +extern int _read(int file, char *buf, int nbytes); +extern int _lseek(int file, int offset, int whence); +extern int isatty(int file); +extern void stdio_drain(void); +/* ticker.c */ +extern volatile uint32_t ticks; +extern void delay_us(uint32_t d); +extern void sys_tick_handler(void); +extern void delay_ms(uint32_t d); +extern int timed_out(uint32_t then, unsigned int ms); +extern void ticker_init(void); diff --git a/app/ring.c b/app/ring.c new file mode 100644 index 0000000..8f13567 --- /dev/null +++ b/app/ring.c @@ -0,0 +1,75 @@ +#include "project.h" + + +static inline size_t +ring_next (volatile ring_t * r, size_t p) +{ + p++; + if (p >= r->size) + p -= r->size; + return p; +} + +void +ring_init (volatile ring_t * r, uint8_t * buf, size_t len) +{ + r->data = buf; + r->size = len; + r->write = 0; + r->read = 0; +} + +int +ring_write_byte (volatile ring_t * r, uint8_t c) +{ + size_t n = ring_next (r, r->write); + + if (n == r->read) + return -EAGAIN; + + r->data[r->write] = c; + + r->write = n; + + return 0; +} + + +int +ring_read_byte (volatile ring_t * r, uint8_t * c) +{ + size_t n = ring_next (r, r->read); + + if (r->read == r->write) + return -EAGAIN; + + *c = r->data[r->read]; + r->read = n; + + return 0; +} + +int +ring_write (volatile ring_t * r, uint8_t * buf, size_t len, int blocking) +{ + while (len--) + { + if (blocking) { + while (ring_write_byte (r, *buf)); + buf++; + } else { + if (ring_write_byte (r, *(buf++))) + return -EAGAIN; + } + } + + return 0; +} + + + +int +ring_empty (volatile ring_t * r) +{ + return (r->read == r->write) ? 1 : 0; +} diff --git a/app/ring.h b/app/ring.h new file mode 100644 index 0000000..e551a5e --- /dev/null +++ b/app/ring.h @@ -0,0 +1,14 @@ +#ifndef _RING_H_ +#define _RING_H_ + +typedef struct ring +{ + uint8_t *data; + size_t size; + size_t write; + size_t read; +} ring_t; + +#endif + + diff --git a/app/stdio.c b/app/stdio.c new file mode 100644 index 0000000..b3fee4f --- /dev/null +++ b/app/stdio.c @@ -0,0 +1,83 @@ +#include "project.h" + +int +_open (const char *name, + int flags, + int mode) +{ + errno = ENOSYS; + return -1; /* Always fails */ + +} /* _open () */ + +int +_close (int file) +{ + errno = EBADF; + return -1; /* Always fails */ + +} /* _close () */ + +int +_write (int file, + char *buf, + int nbytes) +{ + + int ret; + + + ret=usart1_write(buf,nbytes, 1); + + if (ret<0) { + errno=-ret; + return -1; + } + + return ret; +} /* _write () */ + + +int +_read (int file, + char *buf, + int nbytes) +{ + + errno=-EAGAIN; + return -1; /* EOF */ + +} /* _read () */ + +#if 0 +int +_fstat (int file, + struct stat *st) +{ + st->st_mode = S_IFCHR; + return 0; + +} /* _fstat () */ +#endif + +int +_lseek (int file, + int offset, + int whence) +{ + return 0; + +} /* _lseek () */ + +int +isatty (int file) +{ + return 1; + +} /* _isatty () */ + + +void stdio_drain(void) +{ +usart1_drain(); +} diff --git a/app/ticker.c b/app/ticker.c new file mode 100644 index 0000000..0f7d856 --- /dev/null +++ b/app/ticker.c @@ -0,0 +1,76 @@ +#include "project.h" + + +static volatile uint32_t delay_ms_count; +volatile uint32_t ticks; +static uint32_t scale=10; + +void +delay_us (uint32_t d) +{ + d *= scale; + while (d--) + { + __asm__ ("nop"); + } +} + +void +sys_tick_handler (void) +{ + if (delay_ms_count) + delay_ms_count--; + + ticks++; +} + + + +void +delay_ms (uint32_t d) +{ + delay_ms_count = d; + while (delay_ms_count); +} + +int timed_out(uint32_t then,unsigned int ms) +{ +then=ticks-then; + +if (then>ms) return 1; +return 0; +} + + +void ticker_init(void) +{ + uint32_t v,w; + + /*Start periodic timer*/ + + systick_set_clocksource (STK_CSR_CLKSOURCE_AHB_DIV8); + /* 48MHz / 8 = > 6Mhz */ + systick_set_reload (6000); + /* 6MHz / 6000 => 1kHz */ + systick_interrupt_enable (); + systick_counter_enable (); + + /*Calibrate the delay loop */ + + + do + { + scale--; + v = ticks; + while (v == ticks); + delay_us (1000); + w = ticks; + v++; + w -= v; + } + while (w); + + +} + + diff --git a/app/tims_keyboard.c b/app/tims_keyboard.c new file mode 100644 index 0000000..e6a4aae --- /dev/null +++ b/app/tims_keyboard.c @@ -0,0 +1,44 @@ +#include "project.h" +extern uint32_t dfu_flag; + +int +main (void) +{ + + +// rcc_clock_setup_in_hsi_out_48mhz (); + //nvic_set_priority_grouping(NVIC_PriorityGroup_4); + + /*set up pll*/ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /*turn on clocks to periferals*/ + rcc_periph_clock_enable (RCC_GPIOA); + rcc_periph_clock_enable (RCC_GPIOB); + rcc_periph_clock_enable (RCC_GPIOC); + rcc_periph_clock_enable (RCC_AFIO); + rcc_periph_clock_enable (RCC_USART1); + rcc_periph_clock_enable (RCC_USART1); + dwt_enable_cycle_counter(); + + /*Change interrupt priorities so that USART trumps everything else */ + nvic_set_priority(NVIC_USART1_IRQ, 0x40); + nvic_set_priority(NVIC_EXTI0_IRQ, 0xff); + nvic_set_priority(NVIC_SYSTICK_IRQ, 0xff); + + usart_init(); + + ticker_init(); + + + printf("Hello world!\r\n"); + + atkbd_init(); + + usb_init (); + + + usb_run (); + + return 0; +} diff --git a/app/tims_keyboard.ld b/app/tims_keyboard.ld new file mode 100644 index 0000000..a2d5a9c --- /dev/null +++ b/app/tims_keyboard.ld @@ -0,0 +1,43 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08002000, LENGTH = 120K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + +dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; + +/* PROVIDE(_stack = dfu_shared_location ); */ + +SECTIONS +{ + .dfu_shared dfu_shared_location :{ + dfu_flag = .; + } +} + + diff --git a/app/usart.c b/app/usart.c new file mode 100644 index 0000000..74e5383 --- /dev/null +++ b/app/usart.c @@ -0,0 +1,100 @@ +#include "project.h" + +#define BUFFER_SIZE 256 + + +#define USART1_TX GPIO_USART1_TX +#define USART1_TX_PORT GPIOA + +#define USART1_RX GPIO_USART1_RX +#define USART1_RX_PORT GPIOA + + + +ring_t rx1_ring; +static uint8_t rx1_ring_buf[BUFFER_SIZE]; + +ring_t tx1_ring; +static uint8_t tx1_ring_buf[BUFFER_SIZE]; + +void +usart1_isr (void) +{ + uint8_t data; + + /* Check if we were called because of RXNE. */ + if (((USART_CR1 (USART1) & USART_CR1_RXNEIE) != 0) && + ((USART_SR (USART1) & USART_SR_RXNE) != 0)) + { + data = usart_recv (USART1); + ring_write_byte(&rx1_ring,data); + } + + /* Check if we were called because of TXE. */ + if (((USART_CR1 (USART1) & USART_CR1_TXEIE) != 0) && + ((USART_SR (USART1) & USART_SR_TXE) != 0)) + { + + if (ring_read_byte (&tx1_ring, &data)) + { + /*No more data, Disable the TXE interrupt, it's no longer needed. */ + usart_disable_tx_interrupt(USART1); + } + else + { + usart_send_blocking (USART1, data); + } + } + +} + + +void +usart1_queue (uint8_t d) +{ + ring_write_byte (&tx1_ring, d); + usart_enable_tx_interrupt(USART1); +} + +void +usart1_drain (void) +{ + while (!ring_empty (&tx1_ring)); +} + + +int +usart1_write (char *ptr, int len,int blocking) +{ + int ret; + + ret = ring_write (&tx1_ring, (uint8_t *) ptr, len,blocking); + usart_enable_tx_interrupt(USART1); + return ret; +} + + +void +usart_init (void) +{ + ring_init (&rx1_ring, rx1_ring_buf, sizeof (rx1_ring_buf)); + ring_init (&tx1_ring, tx1_ring_buf, sizeof (tx1_ring_buf)); + + nvic_enable_irq (NVIC_USART1_IRQ); + + + MAP_AF(USART1_TX); + MAP_AF_PU(USART1_RX); + + + usart_set_baudrate (USART1, 38400); + usart_set_databits (USART1, 8); + usart_set_stopbits (USART1, USART_STOPBITS_1); + usart_set_parity (USART1, USART_PARITY_NONE); + usart_set_flow_control (USART1, USART_FLOWCONTROL_NONE); + usart_set_mode (USART1, USART_MODE_TX_RX); + + USART_CR1 (USART1) |= USART_CR1_RXNEIE; + + usart_enable (USART1); +} diff --git a/app/usb.c b/app/usb.c new file mode 100644 index 0000000..659a491 --- /dev/null +++ b/app/usb.c @@ -0,0 +1,133 @@ +#include "project.h" + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = ID_VENDOR, + .idProduct = ID_PRODUCT, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_interface ifaces[] = { + { + .num_altsetting = 1, + .altsetting = &keyboard_iface, + }, + { + .num_altsetting = 1, + .altsetting = &consumer_iface, + }, + { + .num_altsetting = 1, + .altsetting = &dfu_iface, + }, +}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 3, + .bConfigurationValue = 1, + .iConfiguration = 4, + .bmAttributes = 0xa0, + .bMaxPower = 0x31, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "James", + "Tim's keyboard", + "000001", + "composite", + "keyboard", + "media buttons", + "dfu device", +}; + + + +usbd_device *usbd_dev; + + +static int +usb_control_request (usbd_device * usbd_dev, struct usb_setup_data *req, + uint8_t ** buf, uint16_t * len, + void (**complete) (usbd_device * usbd_dev, + struct usb_setup_data * req)) +{ + + (void) complete; + (void) usbd_dev; + + if ((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || (req->wValue != 0x2200)) + return 0; + + switch (req->wIndex) + { + case 0: + keyboard_get_descriptor (buf, len); + return 1; + case 1: + consumer_get_descriptor (buf, len); + return 1; + } + + *len = 0; + return 0; +} + + +void +usb_set_config (usbd_device * usbd_dev, uint16_t wValue) +{ + (void) wValue; + (void) usbd_dev; + + usbd_ep_setup (usbd_dev, 0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + usbd_ep_setup (usbd_dev, 0x82, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_STANDARD | + USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usb_control_request); + + usbd_register_control_callback (usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +} + +/* Buffer to be used for control requests. */ +static uint8_t usbd_control_buffer[128]; + +void +usb_init (void) +{ + + usbd_dev = + usbd_init (&stm32f103_usb_driver, &dev, &config, usb_strings, 8, + usbd_control_buffer, sizeof (usbd_control_buffer)); + + usbd_register_set_config_callback (usbd_dev, usb_set_config); + +} + +void +usb_run (void) +{ + while (1) + usbd_poll (usbd_dev); +} diff --git a/board/STM32F103R_BOARD.cfg b/board/STM32F103R_BOARD.cfg new file mode 100644 index 0000000..11217d2 --- /dev/null +++ b/board/STM32F103R_BOARD.cfg @@ -0,0 +1,6 @@ +# + +source [find target/stm32f1x.cfg] + +adapter_khz 500 + diff --git a/boot/Makefile b/boot/Makefile new file mode 100644 index 0000000..86d4d2c --- /dev/null +++ b/boot/Makefile @@ -0,0 +1,32 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + + +CSRCS=usbdfu.c +PROG = usbdfu + +CPROTO=cproto +V=1 +BINARY = ${PROG} +OBJS = ${CSRCS:%.c=%.o} + +include ../Makefile.include + +CFLAGS += -I.. + diff --git a/boot/project.h b/boot/project.h new file mode 100644 index 0000000..34c96b7 --- /dev/null +++ b/boot/project.h @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define INCLUDE_DFU_INTERFACE + + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +#include +#include + diff --git a/boot/usbdfu.c b/boot/usbdfu.c new file mode 100644 index 0000000..d538234 --- /dev/null +++ b/boot/usbdfu.c @@ -0,0 +1,290 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2010 Gareth McMullin + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +void usb_set_config (usbd_device * usbd_dev, uint16_t wValue); + +/* We need a special large control buffer for this device: */ +uint8_t usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +extern uint32_t dfu_flag; + +static struct { + uint8_t buf[sizeof(usbd_control_buffer)]; + uint16_t len; + uint32_t addr; + uint16_t blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = ID_VENDOR, + .idProduct = ID_PRODUCT, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "Cabbages are good for you", + "fish", + "soup", + /* This string is used by ST Microelectronics' DfuSe utility. */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg", +}; + +static uint8_t usbdfu_getstatus(usbd_device *usbd_dev, uint32_t *bwPollTimeout) +{ + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete. */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(usbd_device *usbd_dev, struct usb_setup_data *req) +{ + int i; + (void)req; + (void)usbd_dev; + + switch (usbdfu_state) { + case STATE_DFU_DNBUSY: + flash_unlock(); + if (prog.blocknum == 0) { + switch (prog.buf[0]) { + case CMD_ERASE: + { + uint32_t *dat = (uint32_t *)(prog.buf + 1); + flash_erase_page(*dat); + } + case CMD_SETADDR: + { + uint32_t *dat = (uint32_t *)(prog.buf + 1); + prog.addr = *dat; + } + } + } else { + uint32_t baseaddr = prog.addr + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for (i = 0; i < prog.len; i += 2) { + uint16_t *dat = (uint16_t *)(prog.buf + i); + flash_program_half_word(baseaddr + i, + *dat); + } + } + flash_lock(); + + /* Jump straight to dfuDNLOAD-IDLE, skipping dfuDNLOAD-SYNC. */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return. */ + default: + return; + } +} + +static int usbdfu_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, + uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) +{ + + if ((req->bmRequestType & 0x7F) != 0x21) + return 0; /* Only accept class request. */ + + switch (req->bRequest) { + case DFU_DNLOAD: + if ((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS. */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE. */ + if (usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* Abort returns to dfuIDLE state. */ + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_UPLOAD: + /* Upload not supported for now. */ + return 0; + case DFU_GETSTATUS: { + uint32_t bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + (*buf)[0] = usbdfu_getstatus(usbd_dev, &bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + *complete = usbdfu_getstatus_complete; + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision. */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + return 0; +} + +void +usb_set_config (usbd_device * usbd_dev, uint16_t wValue) +{ + (void) wValue; + (void) usbd_dev; + + usbd_register_control_callback( + usbd_dev, + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); + +} + +int main(void) +{ + usbd_device *usbd_dev; + + rcc_periph_clock_enable(RCC_GPIOA); + + if (dfu_flag!=0xfee1dead) { + /* Boot the application if it's valid. */ + if ((*(volatile uint32_t *)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address. */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer. */ + asm volatile("msr msp, %0"::"g" + (*(volatile uint32_t *)APP_ADDRESS)); + /* Jump to application. */ + (*(void (**)())(APP_ADDRESS + 4))(); + } + } + + dfu_flag=0; + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_periph_clock_enable(RCC_GPIOC); + + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + gpio_set(GPIOC, GPIO11); + + usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 4, usbd_control_buffer, sizeof(usbd_control_buffer)); + + usbd_register_set_config_callback (usbd_dev, usb_set_config); + + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(usbd_dev); +} diff --git a/boot/usbdfu.ld b/boot/usbdfu.ld new file mode 100644 index 0000000..8680dc9 --- /dev/null +++ b/boot/usbdfu.ld @@ -0,0 +1,40 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32f1.ld + +dfu_shared_location = ORIGIN(ram) + LENGTH(ram) - 1024; + +SECTIONS +{ + .dfu_shared dfu_shared_location :{ + dfu_flag = .; + } +} + diff --git a/docs/rm0008.pdf b/docs/rm0008.pdf new file mode 100644 index 0000000..8f3d0e1 Binary files /dev/null and b/docs/rm0008.pdf differ diff --git a/docs/stm32f103c8.pdf b/docs/stm32f103c8.pdf new file mode 100644 index 0000000..2723cee Binary files /dev/null and b/docs/stm32f103c8.pdf differ diff --git a/id.h b/id.h new file mode 100644 index 0000000..9b4da61 --- /dev/null +++ b/id.h @@ -0,0 +1,5 @@ + +#define ID_VENDOR 0x1d6b +#define ID_PRODUCT 0x76eb + + diff --git a/interface/j-link.cfg b/interface/j-link.cfg new file mode 100644 index 0000000..3e95768 --- /dev/null +++ b/interface/j-link.cfg @@ -0,0 +1,5 @@ +# +telnet_port 4444 +gdb_port 3333 + +source [find interface/jlink.cfg] diff --git a/libopencm3 b/libopencm3 new file mode 160000 index 0000000..3a106db --- /dev/null +++ b/libopencm3 @@ -0,0 +1 @@ +Subproject commit 3a106dbd10dfde1a1da16692f6949b455b549235 -- cgit v1.2.3