aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--contrib/automake/Makefile.am115
-rw-r--r--contrib/automake/configure.ac8
-rw-r--r--libmaple/nvic.h8
-rwxr-xr-xsupport/stm32loader.py119
-rw-r--r--wirish/time.h9
5 files changed, 227 insertions, 32 deletions
diff --git a/contrib/automake/Makefile.am b/contrib/automake/Makefile.am
new file mode 100644
index 0000000..50747db
--- /dev/null
+++ b/contrib/automake/Makefile.am
@@ -0,0 +1,115 @@
+# Top level Makefile for libmaple
+
+# The main library
+lib_LIBRARIES = \
+ libmaple.a \
+ libmapleusb.a \
+ libwirish.a
+
+noinst_PROGRAMS = \
+ main
+
+main_SOURCES = \
+ startup2.c \
+ main.cpp
+
+X = $(srcdir)/support/ld/maple_native
+
+main_LDFLAGS = \
+ --gc-sections \
+ -Map=main.map
+
+main_LDADD = libmaple.a
+
+# Main library
+libmaple_a_SOURCES = \
+ libmaple/adc.c \
+ libmaple/dac.c \
+ libmaple/exc.c \
+ libmaple/exti.c \
+ libmaple/flash.c \
+ libmaple/fsmc.c \
+ libmaple/gpio.c \
+ libmaple/nvic.c \
+ libmaple/rcc.c \
+ libmaple/spi.c \
+ libmaple/systick.c \
+ libmaple/timers.c \
+ libmaple/usart.c \
+ libmaple/util.c
+
+nobase_include_HEADERS = \
+ libmaple/adc.h \
+ libmaple/dac.h \
+ libmaple/exti.h \
+ libmaple/flash.h \
+ libmaple/fsmc.h \
+ libmaple/gpio.h \
+ libmaple/libmaple.h \
+ libmaple/libmaple_types.h \
+ libmaple/nvic.h \
+ libmaple/rcc.h \
+ libmaple/ring_buffer.h \
+ libmaple/spi.h \
+ libmaple/systick.h \
+ libmaple/timers.h \
+ libmaple/usart.h \
+ libmaple/util.h \
+ libmaple/usb/descriptors.h \
+ libmaple/usb/usb.h \
+ libmaple/usb/usb_callbacks.h \
+ libmaple/usb/usb_config.h \
+ libmaple/usb/usb_hardware.h \
+ libmaple/usb/usb_lib/usb_core.h \
+ libmaple/usb/usb_lib/usb_def.h \
+ libmaple/usb/usb_lib/usb_init.h \
+ libmaple/usb/usb_lib/usb_int.h \
+ libmaple/usb/usb_lib/usb_lib.h \
+ libmaple/usb/usb_lib/usb_mem.h \
+ libmaple/usb/usb_lib/usb_regs.h \
+ libmaple/usb/usb_lib/usb_type.h
+
+libmapleusb_a_SOURCES = \
+ libmaple/usb/descriptors.c \
+ libmaple/usb/usb.c \
+ libmaple/usb/usb_callbacks.c \
+ libmaple/usb/usb_hardware.c \
+ libmaple/usb/usb_lib/usb_core.c \
+ libmaple/usb/usb_lib/usb_init.c \
+ libmaple/usb/usb_lib/usb_int.c \
+ libmaple/usb/usb_lib/usb_mem.c \
+ libmaple/usb/usb_lib/usb_regs.c
+
+libwirish_a_SOURCES = \
+ wirish/HardwareTimer.cpp \
+ wirish/Print.cpp \
+ wirish/comm/HardwareSPI.cpp \
+ wirish/comm/HardwareSerial.cpp \
+ wirish/cxxabi-compat.cpp \
+ wirish/ext_interrupts.c \
+ wirish/pwm.c \
+ wirish/time.c \
+ wirish/usb_serial.cpp \
+ wirish/wirish.c \
+ wirish/wirish_analog.c \
+ wirish/wirish_digital.c \
+ wirish/wirish_math.cpp \
+ wirish/wirish_shift.c
+
+MCU := STM32F103RB
+BOARD ?= maple_native
+
+FLAGS = \
+ -Os -g -nostdlib -Wall \
+ -ffunction-sections -fdata-sections -Wl,--gc-sections \
+ -mcpu=cortex-m3 -mthumb -fshort-enums -mfloat-abi=soft \
+ -DBOARD_$(BOARD) -DMCU_$(MCU) -DVECT_TAB_BASE \
+ -I$(srcdir)/libmaple \
+ -I$(srcdir)/libmaple/usb \
+ -I$(srcdir)/libmaple/usb/usb_lib
+
+AM_CFLAGS = $(FLAGS) -std=gnu99
+
+AM_CXXFLAGS = $(FLAGS) \
+ -I$(srcdir)/wirish -I$(srcdir)/wirish/comm \
+ -fno-rtti -fno-exceptions
diff --git a/contrib/automake/configure.ac b/contrib/automake/configure.ac
new file mode 100644
index 0000000..4c52ce2
--- /dev/null
+++ b/contrib/automake/configure.ac
@@ -0,0 +1,8 @@
+AC_INIT(libmaple, 0.1)
+AM_INIT_AUTOMAKE(foreign subdir-objects color-tests)
+AC_CONFIG_HEADERS([config.h])
+AC_CONFIG_FILES(Makefile)
+AM_PROG_AS
+AC_PROG_CXX
+AM_PROG_LIBTOOL
+AC_OUTPUT
diff --git a/libmaple/nvic.h b/libmaple/nvic.h
index 3cdac5a..e8ca22d 100644
--- a/libmaple/nvic.h
+++ b/libmaple/nvic.h
@@ -29,6 +29,10 @@
#ifndef _NVIC_H_
#define _NVIC_H_
+#ifdef __cplusplus
+extern "C"{
+#endif
+
#define NVIC_INT_USBHP 19
#define NVIC_INT_USBLP 20
@@ -50,10 +54,6 @@
#define NVIC_VectTab_RAM ((u32)0x20000000)
#define NVIC_VectTab_FLASH ((u32)0x08000000)
-#ifdef __cplusplus
-extern "C"{
-#endif
-
enum {
NVIC_TIMER1 = 27,
NVIC_TIMER2 = 28,
diff --git a/support/stm32loader.py b/support/stm32loader.py
index f717c9a..02ca4e8 100755
--- a/support/stm32loader.py
+++ b/support/stm32loader.py
@@ -25,6 +25,11 @@
import sys, getopt
import serial
import time
+import glob
+import time
+import tempfile
+import os
+import subprocess
try:
from progressbar import *
@@ -53,28 +58,29 @@ class CommandInterface:
stopbits=1,
xonxoff=0, # enable software flow control
rtscts=0, # disable RTS/CTS flow control
- timeout=5 # set a timeout value, None for waiting forever
+ timeout=0.5 # set a timeout value, None for waiting forever
)
def _wait_for_ask(self, info = ""):
+ got = self.sp.read(1)
+
+ if not got:
+ raise CmdException("No response to %s" % info)
+
# wait for ask
- try:
- ask = ord(self.sp.read())
- except:
- raise CmdException("Can't read port or timeout")
+ ask = ord(got)
+
+ if ask == 0x79:
+ # ACK
+ return 1
else:
- if ask == 0x79:
- # ACK
- return 1
+ if ask == 0x1F:
+ # NACK
+ raise CmdException("Chip replied with a NACK during %s" % info)
else:
- if ask == 0x1F:
- # NACK
- raise CmdException("NACK "+info)
- else:
- # Unknow responce
- raise CmdException("Unknow response. "+info+": "+hex(ask))
-
+ # Unknown responce
+ raise CmdException("Unrecognised response %x to %s" % (ask, info))
def reset(self):
self.sp.setDTR(0)
@@ -87,8 +93,21 @@ class CommandInterface:
self.sp.setRTS(0)
self.reset()
- self.sp.write("\x7F") # Syncro
- return self._wait_for_ask("Syncro")
+ # Be a bit more persistant when trying to initialise the chip
+ stop = time.time() + 5.0
+
+ while time.time() <= stop:
+ self.sp.write('\x7f')
+
+ got = self.sp.read()
+
+ # The chip will ACK a sync the very first time and
+ # NACK it every time afterwards
+ if got and got in '\x79\x1f':
+ # Synced up
+ return
+
+ raise CmdException('No response while trying to sync')
def releaseChip(self):
self.sp.setRTS(1)
@@ -314,7 +333,7 @@ def usage():
-v Verify
-r Read
-l length Length of read
- -p port Serial port (default: /dev/tty.usbserial-ftCYPMYJ)
+ -p port Serial port (default: first USB-like port in /dev)
-b baud Baud speed (default: 115200)
-a addr Target address
@@ -322,6 +341,34 @@ def usage():
""" % sys.argv[0]
+def read(filename):
+ """Read the file to be programmed and turn it into a binary"""
+ with open(filename, 'rb') as f:
+ bytes = f.read()
+
+ if bytes.startswith('\x7FELF'):
+ # Actually an ELF file. Convert to binary
+ handle, path = tempfile.mkstemp(suffix='.bin', prefix='stm32loader')
+
+ try:
+ os.close(handle)
+
+ # Try a couple of options for objcopy
+ for name in ['arm-none-eabi-objcopy', 'arm-linux-gnueabi-objcopy']:
+ try:
+ code = subprocess.call([name, '-Obinary', filename, path])
+
+ if code == 0:
+ return read(path)
+ except OSError:
+ pass
+ else:
+ raise Exception('Error %d while converting to a binary file' % code)
+ finally:
+ # Remove the temporary file
+ os.unlink(path)
+ else:
+ return [ord(x) for x in bytes]
if __name__ == "__main__":
@@ -334,7 +381,7 @@ if __name__ == "__main__":
pass
conf = {
- 'port': '/dev/tty.usbserial-FTD3TMCH',
+ 'port': 'auto',
'baud': 115200,
'address': 0x08000000,
'erase': 0,
@@ -386,27 +433,51 @@ if __name__ == "__main__":
else:
assert False, "unhandled option"
+ # Try and find the port automatically
+ if conf['port'] == 'auto':
+ ports = []
+
+ # Get a list of all USB-like names in /dev
+ for name in ['tty.usbserial', 'ttyUSB']:
+ ports.extend(glob.glob('/dev/%s*' % name))
+
+ ports = sorted(ports)
+
+ if ports:
+ # Found something - take it
+ conf['port'] = ports[0]
+
cmd = CommandInterface()
cmd.open(conf['port'], conf['baud'])
mdebug(10, "Open port %(port)s, baud %(baud)d" % {'port':conf['port'], 'baud':conf['baud']})
try:
+ if (conf['write'] or conf['verify']):
+ data = read(args[0])
+
try:
cmd.initChip()
- except:
+ except CmdException:
print "Can't init. Ensure that BOOT0 is enabled and reset device"
bootversion = cmd.cmdGet()
+
mdebug(0, "Bootloader version %X" % bootversion)
- mdebug(0, "Chip id `%s'" % str(map(lambda c: hex(ord(c)), cmd.cmdGetID())))
+
+ if bootversion < 20 or bootversion >= 100:
+ raise Exception('Unreasonable bootloader version %d' % bootversion)
+
+ id = [ord(x) for x in cmd.cmdGetID()]
+ mdebug(0, "Chip id '%s'" % ' '.join('0x%x' % x for x in id))
+
+ if len(id) < 2 or id[0] != 0x04:
+ raise Exception('Unrecognised chip ID')
+
# cmd.cmdGetVersion()
# cmd.cmdGetID()
# cmd.cmdReadoutUnprotect()
# cmd.cmdWriteUnprotect()
# cmd.cmdWriteProtect([0, 1])
- if (conf['write'] or conf['verify']):
- data = map(lambda c: ord(c), file(args[0]).read())
-
if conf['erase']:
cmd.cmdEraseMemory()
diff --git a/wirish/time.h b/wirish/time.h
index fad47a4..742f28d 100644
--- a/wirish/time.h
+++ b/wirish/time.h
@@ -29,14 +29,15 @@
#ifndef _TIME_H
#define _TIME_H
-#ifdef __cplusplus
-extern "C"{
-#endif
-
+#include "libmaple.h"
#include "nvic.h"
#include "systick.h"
#include "boards.h"
+#ifdef __cplusplus
+extern "C"{
+#endif
+
#define US_PER_MS 1000
extern volatile uint32 systick_timer_millis;