stm32_can: Changes to get blackmagic to work

This commit is contained in:
Uwe Bonnes 2013-01-15 16:55:24 +01:00
parent 313a24b316
commit 72ca77af53
5 changed files with 474 additions and 4 deletions

View File

@ -0,0 +1,28 @@
/*
* This file is part of the libopenstm32 project.
*
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
*
* 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 = 256K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K
}
/* Include the common ld script from libopenstm32. */
INCLUDE libopencm3_stm32f1.ld

View File

@ -130,14 +130,26 @@ static char serial_no[9];
static const char *usb_strings[] = { static const char *usb_strings[] = {
"Black Sphere Technologies", "Black Sphere Technologies",
#if defined(DISCOVERY_STLINK) #if defined(BLACKMAGIC)
"Black Magic (Upgrade) for STLink/Discovery",
#else
"Black Magic Probe (Upgrade)", "Black Magic Probe (Upgrade)",
#elif defined(DISCOVERY_STLINK)
"Black Magic (Upgrade) for STLink/Discovery",
#elif defined(STM32_CAN)
"Black Magic (Upgrade) for STM32_CAN",
#else
#warning "Unhandled board"
#endif #endif
serial_no, serial_no,
/* This string is used by ST Microelectronics' DfuSe utility */ /* This string is used by ST Microelectronics' DfuSe utility */
#if defined(BLACKMAGIC)
"@Internal Flash /0x08000000/8*001Ka,120*001Kg" "@Internal Flash /0x08000000/8*001Ka,120*001Kg"
#elif defined(DISCOVERY_STLINK)
"@Internal Flash /0x08000000/8*001Ka,56*001Kg"
#elif defined(STM32_CAN)
"@Internal Flash /0x08000000/4*002Ka,124*002Kg"
#else
#warning "Unhandled board"
#endif
}; };
static u8 usbdfu_getstatus(u32 *bwPollTimeout) static u8 usbdfu_getstatus(u32 *bwPollTimeout)
@ -190,6 +202,7 @@ usbdfu_getstatus_complete(usbd_device *dev, struct usb_setup_data *req)
flash_erase_page(page_start); flash_erase_page(page_start);
last_erased_pages = page_start; last_erased_pages = page_start;
} }
} }
case CMD_SETADDR: case CMD_SETADDR:
prog.addr = *(u32*)(prog.buf+1); prog.addr = *(u32*)(prog.buf+1);
@ -295,6 +308,9 @@ int main(void)
#if defined (DISCOVERY_STLINK) #if defined (DISCOVERY_STLINK)
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
if(!gpio_get(GPIOC, GPIO13)) { if(!gpio_get(GPIOC, GPIO13)) {
#elif defined (STM32_CAN)
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
if(!gpio_get(GPIOA, GPIO0)) {
#else #else
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
if(gpio_get(GPIOB, GPIO12)) { if(gpio_get(GPIOB, GPIO12)) {
@ -316,6 +332,8 @@ int main(void)
FLASH_CR = 0; FLASH_CR = 0;
flash_erase_option_bytes(); flash_erase_option_bytes();
flash_program_option_bytes(FLASH_OBP_RDP, FLASH_OBP_RDP_KEY); flash_program_option_bytes(FLASH_OBP_RDP, FLASH_OBP_RDP_KEY);
/* CL Device: Protect 2 bits with (2 * 2k pages each)*/
/* MD Device: Protect 2 bits with (4 * 1k pages each)*/
flash_program_option_bytes(FLASH_OBP_WRP10, 0x03FC); flash_program_option_bytes(FLASH_OBP_WRP10, 0x03FC);
} }
@ -332,13 +350,18 @@ int main(void)
#endif #endif
rcc_clock_setup_in_hse_8mhz_out_72mhz(); rcc_clock_setup_in_hse_8mhz_out_72mhz();
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
#if defined(DISCOVERY_STLINK) #if defined(DISCOVERY_STLINK)
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG, GPIO0); GPIO_CNF_INPUT_ANALOG, GPIO0);
#elif defined (STM32_CAN)
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
#else #else
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO8); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO8);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
@ -353,8 +376,13 @@ int main(void)
get_dev_unique_id(serial_no); get_dev_unique_id(serial_no);
#if defined(STM32_CAN)
usbdev = usbd_init(&stm32f107_usb_driver,
&dev, &config, usb_strings, 4);
#else
usbdev = usbd_init(&stm32f103_usb_driver, usbdev = usbd_init(&stm32f103_usb_driver,
&dev, &config, usb_strings, 4); &dev, &config, usb_strings, 4);
#endif
usbd_set_control_buffer_size(usbdev, sizeof(usbd_control_buffer)); usbd_set_control_buffer_size(usbdev, sizeof(usbd_control_buffer));
usbd_register_control_callback(usbdev, usbd_register_control_callback(usbdev,
USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
@ -401,6 +429,8 @@ void sys_tick_handler()
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG, GPIO9); GPIO_CNF_INPUT_ANALOG, GPIO9);
led2_state++; led2_state++;
#elif defined(STM32_CAN)
gpio_toggle(GPIOB, GPIO0); /* LED2 on/off */
#else #else
gpio_toggle(GPIOB, GPIO11); /* LED2 on/off */ gpio_toggle(GPIOB, GPIO11); /* LED2 on/off */
#endif #endif

View File

@ -0,0 +1,35 @@
CROSS_COMPILE ?= arm-none-eabi-
CC = $(CROSS_COMPILE)gcc
OBJCOPY = $(CROSS_COMPILE)objcopy
CFLAGS += -Istm32/include -mcpu=cortex-m3 -mthumb \
-DSTM32F1 -DSTM32_CAN -I../libopencm3/include
LDFLAGS_BOOT = -lopencm3_stm32f1 -Wl,--defsym,_stack=0x20010000 \
-Wl,-T,platforms/stm32/stm32_can.ld -nostartfiles -lc -lnosys \
-Wl,-Map=mapfile -mthumb -mcpu=cortex-m3 -Wl,-gc-sections \
-L../libopencm3/lib
LDFLAGS = $(LDFLAGS_BOOT) -Wl,-Ttext=0x8002000
VPATH += platforms/stm32
SRC += cdcacm.c \
platform.c \
usbuart.c \
all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex
blackmagic.bin: blackmagic
$(OBJCOPY) -O binary $^ $@
blackmagic_dfu: usbdfu.o
$(CC) $^ -o $@ $(LDFLAGS_BOOT)
blackmagic_dfu.bin: blackmagic_dfu
$(OBJCOPY) -O binary $^ $@
blackmagic_dfu.hex: blackmagic_dfu
$(OBJCOPY) -O ihex $^ $@
host_clean:
-rm blackmagic.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex

View File

@ -0,0 +1,197 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
*
* 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/>.
*/
/* This file implements the platform specific functions for the STM32
* implementation.
*/
#include <libopencm3/stm32/f1/rcc.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/stm32/f1/adc.h>
#include "platform.h"
#include "jtag_scan.h"
#include <usbuart.h>
#include <ctype.h>
uint8_t running_status;
volatile uint32_t timeout_counter;
jmp_buf fatal_error_jmpbuf;
static void morse_update(void);
int platform_init(void)
{
/* Enable peripherals */
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
rcc_clock_setup_in_hse_8mhz_out_72mhz();
/* Setup GPIO ports */
gpio_set_mode(TMS_PORT, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
TMS_PIN);
gpio_set_mode(TCK_PORT, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
TCK_PIN | TDI_PIN);
gpio_set_mode(TCK_PORT, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT,
TDO_PIN);
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
LED_UART | LED_IDLE_RUN | LED_ERROR);
/* Setup heartbeat timer */
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
systick_set_reload(900000); /* Interrupt us at 10 Hz */
SCB_SHPR(11) &= ~((15 << 4) & 0xff);
SCB_SHPR(11) |= ((14 << 4) & 0xff);
systick_interrupt_enable();
systick_counter_enable();
usbuart_init();
SCB_VTOR = 0x2000; // Relocate interrupt vector table here
cdcacm_init();
jtag_scan(NULL);
return 0;
}
void platform_delay(uint32_t delay)
{
timeout_counter = delay;
while(timeout_counter);
}
void sys_tick_handler(void)
{
if(running_status)
gpio_toggle(LED_PORT, LED_IDLE_RUN);
if(timeout_counter)
timeout_counter--;
morse_update();
}
/* Morse code patterns and lengths */
static const struct {
uint16_t code;
uint8_t bits;
} morse_letter[] = {
{ 0b00011101, 8}, // 'A' .-
{ 0b000101010111, 12}, // 'B' -...
{ 0b00010111010111, 14}, // 'C' -.-.
{ 0b0001010111, 10}, // 'D' -..
{ 0b0001, 4}, // 'E' .
{ 0b000101110101, 12}, // 'F' ..-.
{ 0b000101110111, 12}, // 'G' --.
{ 0b0001010101, 10}, // 'H' ....
{ 0b000101, 6}, // 'I' ..
{0b0001110111011101, 16}, // 'J' .---
{ 0b000111010111, 12}, // 'K' -.-
{ 0b000101011101, 12}, // 'L' .-..
{ 0b0001110111, 10}, // 'M' --
{ 0b00010111, 8}, // 'N' -.
{ 0b00011101110111, 14}, // 'O' ---
{ 0b00010111011101, 14}, // 'P' .--.
{0b0001110101110111, 16}, // 'Q' --.-
{ 0b0001011101, 10}, // 'R' .-.
{ 0b00010101, 8}, // 'S' ...
{ 0b000111, 6}, // 'T' -
{ 0b0001110101, 10}, // 'U' ..-
{ 0b000111010101, 12}, // 'V' ...-
{ 0b000111011101, 12}, // 'W' .--
{ 0b00011101010111, 14}, // 'X' -..-
{0b0001110111010111, 16}, // 'Y' -.--
{ 0b00010101110111, 14}, // 'Z' --..
};
const char *morse_msg;
static const char * volatile morse_ptr;
static char morse_repeat;
void morse(const char *msg, char repeat)
{
morse_msg = morse_ptr = msg;
morse_repeat = repeat;
SET_ERROR_STATE(0);
}
static void morse_update(void)
{
static uint16_t code;
static uint8_t bits;
if(!morse_ptr) return;
if(!bits) {
char c = *morse_ptr++;
if(!c) {
if(morse_repeat) {
morse_ptr = morse_msg;
c = *morse_ptr++;
} else {
morse_ptr = 0;
return;
}
}
if((c >= 'A') && (c <= 'Z')) {
c -= 'A';
code = morse_letter[c].code;
bits = morse_letter[c].bits;
} else {
code = 0; bits = 4;
}
}
SET_ERROR_STATE(code & 1);
code >>= 1; bits--;
}
const char *platform_target_voltage(void)
{
return "ABSENT!";
}
void assert_boot_pin(void)
{
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
gpio_set(GPIOA, GPIO0);
}

View File

@ -0,0 +1,180 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2011 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
*
* 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/>.
*/
/* This file implements the platform specific functions for the STM32
* implementation.
*/
#ifndef __PLATFORM_H
#define __PLATFORM_H
#include <libopencm3/stm32/f1/gpio.h>
#include <libopencm3/usb/usbd.h>
#include <setjmp.h>
#include <alloca.h>
#include "gdb_packet.h"
#define INLINE_GPIO
#define CDCACM_PACKET_SIZE 64
/*#define PLATFORM_HAS_TRACESWO*/
extern usbd_device *usbdev;
#define CDCACM_GDB_ENDPOINT 1
#define CDCACM_UART_ENDPOINT 3
/* Important pin mappings for STM32 implementation:
*
* LED0 = PB2 (Yellow LED : Running)
* LED1 = PB10 (Yellow LED : Idle)
* LED2 = PB11 (Red LED : Error)
*
* TPWR = RB0 (input) -- analogue on mini design ADC1, ch8
* nTRST = PC9
* SRST_OUT = NA
* TDI = PC12
* TMS = PB14(input for SWDP)
* TCK = PC10
* TDO = PC11(input)
* nSRST = PA7 (input)
*
* Force DFU mode button: PA0 Read High for Bootloader Req
*/
/* Hardware definitions... */
#define TDI_PORT GPIOC
#define TMS_PORT GPIOB
#define TCK_PORT GPIOC
#define TDO_PORT GPIOC
#define TDI_PIN GPIO12
#define TMS_PIN GPIO14
#define TCK_PIN GPIO10
#define TDO_PIN GPIO11
#define SWDIO_PORT TMS_PORT
#define SWCLK_PORT TCK_PORT
#define SWDIO_PIN TMS_PIN
#define SWCLK_PIN TCK_PIN
//#define TRST_PORT GPIOC
//#define TRST_PIN GPIO9
#define LED_PORT GPIOB
#define LED_PORT_UART GPIOB
#define LED_UART GPIO0
#define LED_IDLE_RUN GPIO1
#define LED_ERROR GPIO12
#define USB_DRIVER stm32f107_usb_driver
#define USB_IRQ NVIC_OTG_FS_IRQ
#define USB_ISR otg_fs_isr
/* Interrupt priorities. Low numbers are high priority.
* For now USART1 preempts USB which may spin while buffer is drained.
* TIM3 is used for traceswo capture and must be highest priority.
*/
#define IRQ_PRI_USB (2 << 4)
#define IRQ_PRI_USBUSART (1 << 4)
#define IRQ_PRI_TIM3 (0 << 4)
#define USBUSART USART2
#define USBUSART_CR1 USART2_CR1
#define USBUSART_IRQ NVIC_USART2_IRQ
#define USBUSART_APB_ENR RCC_APB1ENR
#define USBUSART_CLK_ENABLE RCC_APB1ENR_USART2EN
#define USBUSART_PORT GPIOA
#define USBUSART_TX_PIN GPIO2
#define USBUSART_ISR usart2_isr
#define DEBUG(...)
extern uint8_t running_status;
extern volatile uint32_t timeout_counter;
extern jmp_buf fatal_error_jmpbuf;
extern const char *morse_msg;
#define gpio_set_val(port, pin, val) do { \
if(val) \
gpio_set((port), (pin)); \
else \
gpio_clear((port), (pin)); \
} while(0)
#define SET_RUN_STATE(state) {running_status = (state);}
#define SET_IDLE_STATE(state) {gpio_set_val(LED_PORT, LED_IDLE_RUN, state);}
#define SET_ERROR_STATE(state) {gpio_set_val(LED_PORT, LED_ERROR, state);}
#define PLATFORM_SET_FATAL_ERROR_RECOVERY() {setjmp(fatal_error_jmpbuf);}
#define PLATFORM_FATAL_ERROR(error) { \
if(running_status) gdb_putpacketz("X1D"); \
else gdb_putpacketz("EFF"); \
running_status = 0; \
target_list_free(); \
morse("TARGET LOST.", 1); \
longjmp(fatal_error_jmpbuf, (error)); \
}
int platform_init(void);
void morse(const char *msg, char repeat);
const char *platform_target_voltage(void);
int platform_hwversion(void);
void platform_delay(uint32_t delay);
/* <cdcacm.c> */
void cdcacm_init(void);
/* Returns current usb configuration, or 0 if not configured. */
int cdcacm_get_config(void);
int cdcacm_get_dtr(void);
/* <platform.h> */
void uart_usb_buf_drain(uint8_t ep);
/* Use newlib provided integer only stdio functions */
#define sscanf siscanf
#define sprintf siprintf
#define vasprintf vasiprintf
#ifdef INLINE_GPIO
static inline void _gpio_set(u32 gpioport, u16 gpios)
{
GPIO_BSRR(gpioport) = gpios;
}
#define gpio_set _gpio_set
static inline void _gpio_clear(u32 gpioport, u16 gpios)
{
GPIO_BRR(gpioport) = gpios;
}
#define gpio_clear _gpio_clear
static inline u16 _gpio_get(u32 gpioport, u16 gpios)
{
return (u16)GPIO_IDR(gpioport) & gpios;
}
#define gpio_get _gpio_get
#endif
#endif
#define disconnect_usb() usbd_disconnect(usbdev,1)
void assert_boot_pin(void);
#define setup_vbus_irq()