Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Commit

Permalink
Merging Colin's master branch, including a couple of fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnoble committed May 9, 2013
1 parent 9e509d3 commit efb3c30
Show file tree
Hide file tree
Showing 45 changed files with 893 additions and 169 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ docs/html
*.pyc
*.gcno
*.gcda
*.swp
libswiftnav/lcov
/tags
/libopencm3_for_ctags
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ else
MAKEFLAGS += PRN=$(PRN)
endif

.PHONY: all tests firmware docs
.PHONY: all tests firmware bootloader docs

all: firmware tests
all: firmware bootloader tests

firmware:
@printf "BUILD src\n"; \
$(MAKE) -C src $(MAKEFLAGS)

bootloader:
@printf "BUILD bootloader\n"; \
$(MAKE) -C bootloader $(MAKEFLAGS)

tests:
$(Q)for i in tests/*; do \
if [ -d $$i ]; then \
Expand All @@ -32,6 +36,8 @@ tests:

clean:
@printf "CLEAN src\n"; \
$(MAKE) -C bootloader $(MAKEFLAGS) clean
@printf "CLEAN bootloader\n"; \
$(MAKE) -C src $(MAKEFLAGS) clean
$(Q)for i in tests/*; do \
if [ -d $$i ]; then \
Expand Down
19 changes: 19 additions & 0 deletions bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BINARY = bootloader

OBJS = bootloader.o \
../src/debug.o \
../src/settings.o \
../src/error.o \
../src/hw/usart.o \
../src/hw/usart_rx.o \
../src/hw/usart_tx.o \
../src/hw/leds.o \
../src/hw/stm_flash.o \

CFLAGS += -I../src/

SWIFTNAV_ROOT ?= ..

LDSCRIPT ?= $(SWIFTNAV_ROOT)/bootloader/bootloader.ld

include $(SWIFTNAV_ROOT)/stm32/Makefile.include
4 changes: 4 additions & 0 deletions bootloader/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Use stm32_flash.py (in scripts directory) to upload new firmware using
the bootloader, via

"./stm32flash.py [filename.bin]"
132 changes: 132 additions & 0 deletions bootloader/bootloader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* bootloader for the Swift Navigation Piksi GPS Receiver
*
* Copyright (C) 2010 Gareth McMullin <[email protected]>
* Copyright (C) 2011 Piotr Esden-Tempski <[email protected]>
* Copyright (C) 2013 Swift Navigation Inc <www.swift-nav.com>
*
* Contact: Colin Beighley <[email protected]>
*
* Based on luftboot, a bootloader for the Paparazzi UAV project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <libopencm3/cm3/scb.h>

#include "main.h"
#include "hw/leds.h"
#include "debug.h"
#include "hw/stm_flash.h"
#include "hw/usart.h"

#define APP_ADDRESS 0x08010000
#define STACK_ADDRESS 0x10010000

u8 pc_wants_bootload = 0;
u8 current_app_valid = 0;

void jump_to_app_callback(u8 buff[] __attribute__((unused))){
/* Disable peripherals used in the bootloader */
debug_disable();
/* Set vector table base address */
SCB_VTOR = APP_ADDRESS & 0x1FFFFF00;
/* Initialise master stack pointer */
asm volatile ("msr msp, %0"::"g"(*(volatile u32*)APP_ADDRESS));
/* Jump to application */
(*(void(**)())(APP_ADDRESS + 4))();
}

void pc_wants_bootload_callback(u8 buff[] __attribute__((unused))){
pc_wants_bootload = 1;
}

int main(void)
{
/* Setup and turn on LEDs */
led_setup();
led_on(LED_GREEN);
led_on(LED_RED);

/* Setup UART and debug interface for
* transmitting and receiving callbacks */
debug_setup();

/* Add callbacks for erasing, programming and reading flash */
stm_flash_callbacks_setup();

/* Add callback for jumping to application after bootloading is finished */
static msg_callbacks_node_t jump_to_app_node;
debug_register_callback(MSG_JUMP_TO_APP, &jump_to_app_callback,
&jump_to_app_node);

/* Add callback for PC to tell bootloader it wants to load program */
static msg_callbacks_node_t pc_wants_bootload_node;
debug_register_callback(MSG_BOOTLOADER_HANDSHAKE,&pc_wants_bootload_callback,
&pc_wants_bootload_node);

/* rom (rx) : ORIGIN = 0x08010000, LENGTH = 896K */
/* Send message to PC indicating bootloader is ready to load program */
debug_send_msg(MSG_BOOTLOADER_HANDSHAKE,0,0);

/* Is current application we are programmed with valid? Check this
* by seeing if the first address of the application contains the
* correct stack address */
current_app_valid = (*(volatile u32*)APP_ADDRESS == STACK_ADDRESS) ? 1:0;

/*
* Wait a bit for response from PC. If it doesn't respond by calling
* pc_wants_bootload_callback and we have a valid application,
* then boot the application
* TODO : might as well make this as long as FPGA takes to configure itself
* from the configuration flash, as it doesn't add to the startup time
*/
for (u64 i=0; i<100000; i++){
DO_EVERY(3000,
led_toggle(LED_GREEN);
led_toggle(LED_RED);
debug_send_msg(MSG_BOOTLOADER_HANDSHAKE,0,0);
);
debug_process_messages(); /* to service pc_wants_bootload_callback */
if (pc_wants_bootload) break;
}
if ((pc_wants_bootload) || !(current_app_valid)){
/*
* We expect PC application passing application data to call
* jump_to_app_callback to break us out of this while loop after it has
* finished sending flash programming callbacks
*/
while(1){
debug_process_messages();
DO_EVERY(50000,
DO_EVERY(2,
led_toggle(LED_GREEN);
);
led_toggle(LED_RED);
/*
* In case PC application was started after we entered the loop. It
* is expecting to get a bootloader handshake message before it will
* send flash programming callbacks
*/
debug_send_msg(MSG_BOOTLOADER_HANDSHAKE,0,0);
);
}
}

/* Looks like the PC didn't want to update - boot the existing application */
jump_to_app_callback(NULL);

return 0;
}
47 changes: 47 additions & 0 deletions bootloader/bootloader.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* bootloader for the Swift Navigation Piksi GPS Receiver
*
* Copyright (C) 2013 Swift Navigation Inc <www.swift-nav.com>
*
* Contacts: Fergus Noble <[email protected]>
* Colin Beighley <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* Define memory regions. */
MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 896K
settings_flash (r) : ORIGIN = 0x080E0000, LENGTH = 128K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
/* STM32F4 Core Coupled Memory, not accesible by peripherals or DMA. */
ccm (rwx) : ORIGIN = 0x10000000, LENGTH = 64K
}

/* Include the common ld script. */
INCLUDE libopencm3_stm32f4.ld

SECTIONS {
_stack_size = 65536;
_stack = ORIGIN(ccm)+LENGTH(ccm);
_stack_begin = _stack - _stack_size;

.settings_area : {
_settings_area = .;
*(.settings_area*)
_esettings_area = .;
} > settings_flash

}
6 changes: 3 additions & 3 deletions scripts/flash.py → scripts/m25_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def roundup_multiple(x, multiple):
def rounddown_multiple(x, multiple):
return x if x % multiple == 0 else x - x % multiple

class Flash(Thread):
class M25_flash(Thread):
_flash_ready = Event()
_command_queue = []
_wants_to_exit = False
Expand All @@ -48,7 +48,7 @@ class Flash(Thread):
_rd_cb_data = []

def __init__(self, link):
super(Flash, self).__init__()
super(M25_flash, self).__init__()
self._flash_ready.set()
self.link = link
self.link.add_callback(0xF0, self._flash_done_callback)
Expand Down Expand Up @@ -209,7 +209,7 @@ def write_ihx(self, filename):
#Create SerialLink and Flash objects
link = serial_link.SerialLink(port=serial_port)
link.add_callback(serial_link.MSG_PRINT, serial_link.default_print_callback)
piksi_flash = Flash(link)
piksi_flash = M25_flash(link)
piksi_flash.start()
#Write configuration file to FPGA's flash
piksi_flash.write_ihx(flash_file)
Expand Down
4 changes: 2 additions & 2 deletions scripts/serial_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def get_message(self):
break
else:
self.unhandled_bytes += 1
print "Total unhandled bytes =", self.unhandled_bytes
print "Unhandled byte : 0x%02x," % ord(magic), "total", self.unhandled_bytes
else:
self.unhandled_bytes += 1
print "Total unhandled bytes =", self.unhandled_bytes
print "Unhandled byte : 0x%02x," % ord(magic), "total", self.unhandled_bytes
msg_type = ord(self.ser.read(1))
msg_len = ord(self.ser.read(1))
data = ""
Expand Down
Loading

0 comments on commit efb3c30

Please sign in to comment.