aboutsummaryrefslogtreecommitdiff
path: root/src/platforms/f4discovery/platform.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/platforms/f4discovery/platform.c')
-rw-r--r--src/platforms/f4discovery/platform.c176
1 files changed, 35 insertions, 141 deletions
diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c
index 4aa14a6..e85ebd5 100644
--- a/src/platforms/f4discovery/platform.c
+++ b/src/platforms/f4discovery/platform.c
@@ -22,8 +22,12 @@
* implementation.
*/
+#include "general.h"
+#include "cdcacm.h"
+#include "usbuart.h"
+#include "morse.h"
+
#include <libopencm3/stm32/f4/rcc.h>
-#include <libopencm3/cm3/systick.h>
#include <libopencm3/cm3/scb.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
@@ -31,37 +35,32 @@
#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/usb/usbd.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)
+void platform_init(void)
{
- rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
+ /* Check the USER button*/
+ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
+ if(gpio_get(GPIOA, GPIO0)) {
+ platform_request_boot();
+ scb_reset_core();
+ }
+
+ rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_48MHZ]);
/* Enable peripherals */
rcc_peripheral_enable_clock(&RCC_AHB2ENR, RCC_AHB2ENR_OTGFSEN);
- rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
- rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN);
+ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN);
rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
-
+ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_CRCEN);
/* Set up USB Pins and alternate function*/
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,
GPIO9 | GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);
- GPIOA_OSPEEDR &=~0xfc;
- GPIOA_OSPEEDR |= 0xa8;
+ GPIOC_OSPEEDR &=~0xF30;
+ GPIOC_OSPEEDR |= 0xA20;
gpio_mode_setup(JTAG_PORT, GPIO_MODE_OUTPUT,
GPIO_PUPD_NONE,
TMS_PIN | TCK_PIN | TDI_PIN);
@@ -72,118 +71,11 @@ int platform_init(void)
gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT,
GPIO_PUPD_NONE,
- LED_UART | LED_IDLE_RUN | LED_ERROR | LED_SPARE1);
-
- /* Setup heartbeat timer */
- systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
- systick_set_reload(168000000/(10*8)); /* Interrupt us at 10 Hz */
- SCB_SHPR(11) &= ~((15 << 4) & 0xff);
- SCB_SHPR(11) |= ((14 << 4) & 0xff);
- systick_interrupt_enable();
- systick_counter_enable();
+ LED_UART | LED_IDLE_RUN | LED_ERROR | LED_BOOTLOADER);
+ platform_timing_init();
usbuart_init();
-
- SCB_VTOR = 0x10000; // 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)
@@ -191,18 +83,20 @@ const char *platform_target_voltage(void)
return "ABSENT!";
}
-void assert_boot_pin(void)
+void platform_request_boot(void)
{
- if (gpio_get(GPIOA, GPIO0)) {
- /* Jump to the built in bootloader by mapping System flash */
- rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SYSCFGEN);
- SYSCFG_MEMRM &= ~3;
- SYSCFG_MEMRM |= 1;
- }
- else {
- /* Flag Bootloader Request by mimicing a pushed USER button*/
- gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT,
- GPIO_PUPD_NONE, GPIO0);
- gpio_set(GPIOA, GPIO0);
- }
+ /* Disconnect USB cable */
+ usbd_disconnect(usbdev, 1);
+ nvic_disable_irq(USB_IRQ);
+
+ /* Assert blue LED as indicator we are in the bootloader */
+ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
+ gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT,
+ GPIO_PUPD_NONE, LED_BOOTLOADER);
+ gpio_set(LED_PORT, LED_BOOTLOADER);
+
+ /* Jump to the built in bootloader by mapping System flash */
+ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SYSCFGEN);
+ SYSCFG_MEMRM &= ~3;
+ SYSCFG_MEMRM |= 1;
}