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.c172
1 files changed, 37 insertions, 135 deletions
diff --git a/src/platforms/f4discovery/platform.c b/src/platforms/f4discovery/platform.c
index 0e81ba4..4aa14a6 100644
--- a/src/platforms/f4discovery/platform.c
+++ b/src/platforms/f4discovery/platform.c
@@ -22,14 +22,14 @@
* implementation.
*/
-#include <libopencm3/stm32/f1/rcc.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>
#include <libopencm3/stm32/usart.h>
+#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/usb/usbd.h>
-#include <libopencm3/stm32/f1/adc.h>
#include "platform.h"
#include "jtag_scan.h"
@@ -44,64 +44,39 @@ jmp_buf fatal_error_jmpbuf;
static void morse_update(void);
-static void adc_init(void);
-
-/* Pins PB[7:5] are used to detect hardware revision.
- * 000 - Original production build.
- * 001 - Mini production build.
- */
-int platform_hwversion(void)
-{
- static int hwversion = -1;
- if (hwversion == -1) {
- gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
- GPIO_CNF_INPUT_PULL_UPDOWN,
- GPIO7 | GPIO6 | GPIO5);
- gpio_clear(GPIOB, GPIO7 | GPIO6 | GPIO5);
- hwversion = gpio_get(GPIOB, GPIO7 | GPIO6 | GPIO5) >> 5;
- }
- return hwversion;
-}
-
int platform_init(void)
{
- rcc_clock_setup_in_hse_8mhz_out_72mhz();
+ rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
/* Enable peripherals */
- 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_IOPBEN);
- rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
-
- /* Setup GPIO ports */
- gpio_clear(USB_PU_PORT, USB_PU_PIN);
- gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
- USB_PU_PIN);
-
- gpio_set_mode(JTAG_PORT, GPIO_MODE_OUTPUT_50_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL,
- TMS_PIN | TCK_PIN | TDI_PIN);
+ 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_IOPDEN);
- /* This needs some fixing... */
- /* Toggle required to sort out line drivers... */
- gpio_port_write(GPIOA, 0x8100);
- gpio_port_write(GPIOB, 0x2000);
- gpio_port_write(GPIOA, 0x8180);
- gpio_port_write(GPIOB, 0x2002);
+ /* 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;
+ gpio_mode_setup(JTAG_PORT, GPIO_MODE_OUTPUT,
+ GPIO_PUPD_NONE,
+ TMS_PIN | TCK_PIN | TDI_PIN);
- gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL,
- LED_UART | LED_IDLE_RUN | LED_ERROR);
+ gpio_mode_setup(TDO_PORT, GPIO_MODE_INPUT,
+ GPIO_PUPD_NONE,
+ TDO_PIN);
- /* FIXME: This pin in intended to be input, but the TXS0108 fails
- * to release the device from reset if this floats. */
- gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL, GPIO7);
+ 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(900000); /* Interrupt us at 10 Hz */
+ 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();
@@ -109,15 +84,7 @@ int platform_init(void)
usbuart_init();
- if (platform_hwversion() > 0) {
- adc_init();
- } else {
- gpio_clear(GPIOB, GPIO0);
- gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
- GPIO_CNF_INPUT_PULL_UPDOWN, GPIO0);
- }
-
- SCB_VTOR = 0x2000; // Relocate interrupt vector table here
+ SCB_VTOR = 0x10000; // Relocate interrupt vector table here
cdcacm_init();
@@ -219,88 +186,23 @@ static void morse_update(void)
code >>= 1; bits--;
}
-static void adc_init(void)
-{
- rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_ADC1EN);
-
- gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
- GPIO_CNF_INPUT_ANALOG, GPIO0);
-
- adc_off(ADC1);
- adc_disable_scan_mode(ADC1);
- adc_set_single_conversion_mode(ADC1);
- adc_disable_external_trigger_regular(ADC1);
- adc_set_right_aligned(ADC1);
- adc_set_conversion_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC);
-
- adc_power_on(ADC1);
-
- /* Wait for ADC starting up. */
- for (int i = 0; i < 800000; i++) /* Wait a bit. */
- __asm__("nop");
-
- adc_reset_calibration(ADC1);
- adc_calibration(ADC1);
-}
-
const char *platform_target_voltage(void)
{
- if (platform_hwversion() == 0)
- return gpio_get(GPIOB, GPIO0) ? "OK" : "ABSENT!";
-
- static char ret[] = "0.0V";
- const u8 channel = 8;
- adc_set_regular_sequence(ADC1, 1, (u8*)&channel);
-
- adc_on(ADC1);
-
- /* Wait for end of conversion. */
- while (!(ADC_SR(ADC1) & ADC_SR_EOC));
-
- u32 val = ADC_DR(ADC1) * 99; /* 0-4095 */
- ret[0] = '0' + val / 81910;
- ret[2] = '0' + (val / 8191) % 10;
-
- return ret;
+ return "ABSENT!";
}
void assert_boot_pin(void)
{
- gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
- gpio_clear(GPIOB, GPIO12);
-}
-
-void exti15_10_isr(void)
-{
- if (gpio_get(USB_VBUS_PORT, USB_VBUS_PIN)) {
- /* Drive pull-up high if VBUS connected */
- gpio_set_mode(USB_PU_PORT, GPIO_MODE_OUTPUT_10_MHZ,
- GPIO_CNF_OUTPUT_PUSHPULL, USB_PU_PIN);
- } else {
- /* Allow pull-up to float if VBUS disconnected */
- gpio_set_mode(USB_PU_PORT, GPIO_MODE_INPUT,
- GPIO_CNF_INPUT_FLOAT, USB_PU_PIN);
+ 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);
}
-
- exti_reset_request(USB_VBUS_PIN);
-}
-
-void setup_vbus_irq(void)
-{
- nvic_set_priority(USB_VBUS_IRQ, IRQ_PRI_USB_VBUS);
- nvic_enable_irq(USB_VBUS_IRQ);
-
- gpio_set(USB_VBUS_PORT, USB_VBUS_PIN);
- gpio_set(USB_PU_PORT, USB_PU_PIN);
-
- gpio_set_mode(USB_VBUS_PORT, GPIO_MODE_INPUT,
- GPIO_CNF_INPUT_PULL_UPDOWN, USB_VBUS_PIN);
-
- /* Configure EXTI for USB VBUS monitor */
- exti_select_source(USB_VBUS_PIN, USB_VBUS_PORT);
- exti_set_trigger(USB_VBUS_PIN, EXTI_TRIGGER_BOTH);
- exti_enable_request(USB_VBUS_PIN);
-
- exti15_10_isr();
}