Boot, UART working

This commit is contained in:
jaseg 2017-07-31 16:39:37 +02:00
parent 6482cf2a69
commit 155a29ce08
7 changed files with 367 additions and 115 deletions

View file

@ -3,6 +3,10 @@ CUBE_PATH ?= $(wildcard ~)/resource/STM32CubeF4
CMSIS_PATH ?= $(CUBE_PATH)/Drivers/CMSIS
CMSIS_DEV_PATH ?= $(CMSIS_PATH)/Device/ST/STM32F4xx
HAL_PATH ?= $(CUBE_PATH)/Drivers/STM32F4xx_HAL_Driver
USB_PATH ?= $(CUBE_PATH)/Middlewares/ST/STM32_USB_Host_Library
OUT ?= out
VPATH = .:$(HAL_PATH)/Src:$(USB_PATH)/Core/Src:$(USB_PATH)/Class/HID/Src
CC := arm-none-eabi-gcc
LD := arm-none-eabi-ld
@ -10,22 +14,35 @@ OBJCOPY := arm-none-eabi-objcopy
OBJDUMP := arm-none-eabi-objdump
SIZE := arm-none-eabi-size
CFLAGS = -Wall -std=gnu11 -Os -fdump-rtl-expand
CFLAGS = -g -Wall -std=gnu11 -Os -fdump-rtl-expand
CFLAGS += -mlittle-endian -mcpu=cortex-m4 -mthumb
CFLAGS += -ffunction-sections -fdata-sections
#CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -nostartfiles
LDFLAGS += -Wl,-Map=main.map -nostdlib
LDFLAGS += -Wl,--gc-sections
LDFLAGS += -g -Wl,-Map=main.map -nostdlib
#LDFLAGS += -Wl,--gc-sections
LIBS = -lgcc
#LIBS += -lrdimon
CFLAGS += -DSTM32F407xx
LDFLAGS += -Tstm32_flash.ld
CFLAGS += -I$(CMSIS_DEV_PATH)/Include -I$(CMSIS_PATH)/Include -I$(HAL_PATH)/Inc -Iconfig
CFLAGS += -I$(CMSIS_DEV_PATH)/Include -I$(CMSIS_PATH)/Include -I$(HAL_PATH)/Inc -I. -Iconfig
LDFLAGS += -L$(CMSIS_PATH)/Lib/GCC
###################################################
SOURCES = main.c keyboard.c menu.c usbh_conf.c startup_stm32f407xx.s system_stm32f4xx.c syscalls.c stm32f4xx_it.c
SOURCES_C = $(filter %.c,$(SOURCES))
SOURCES_ASM = $(filter %.s,$(SOURCES))
CFLAGS += -I$(USB_PATH)/Core/Inc -I$(USB_PATH)/Class/HID/Inc
USB_SOURCES := $(notdir $(wildcard $(USB_PATH)/Core/Src/*.c)) $(notdir $(wildcard $(USB_PATH)/Class/HID/Src/*.c))
USB_SOURCES := $(filter-out usbh_conf_template.c,$(USB_SOURCES))
HAL_SOURCES = stm32f4xx_hal.c
HAL_SOURCES += stm32f4xx_hal_rcc.c stm32f4xx_hal_flash.c stm32f4xx_hal_flash_ex.c stm32f4xx_hal_dma.c
HAL_SOURCES += stm32f4xx_hal_usart.c stm32f4xx_hal_pwr.c
HAL_SOURCES += stm32f4xx_hal_hcd.c stm32f4xx_hal_gpio.c stm32f4xx_hal_cortex.c
HAL_SOURCES += stm32f4xx_ll_usb.c
################################################################################
.PHONY: program clean
@ -40,7 +57,8 @@ all: main.elf main.pdf
%.dot: %.elf
r2 -a arm -qc 'aa;agC' $< 2>/dev/null >$@
main.elf: main.o startup_stm32f407xx.o system_stm32f4xx.c $(HAL_PATH)/Src/stm32f4xx_ll_utils.o
#$(HAL_PATH)/Src/stm32f4xx_ll_utils.o
main.elf: $(SOURCES_ASM:.s=.o) $(SOURCES_C:.c=.o) $(USB_SOURCES:.c=.o) $(HAL_SOURCES:.c=.o)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
$(OBJCOPY) -O ihex $@ $(@:.elf=.hex)
$(OBJCOPY) -O binary $@ $(@:.elf=.bin)

0
build/.git_keep Normal file
View file

193
main.c
View file

@ -44,40 +44,115 @@
*
******************************************************************************
*/
#include "main.h"
SCB_Type *scb = SCB;
GPIO_TypeDef *gpioa = GPIOA;
USBH_HandleTypeDef hUSBHost;
HID_ApplicationTypeDef Appli_state = APPLICATION_IDLE;
USART_HandleTypeDef console_uart;
static void SystemClock_Config(void);
static void USBH_UserProcess(USBH_HandleTypeDef* phost, uint8_t id);
static void HID_InitApplication(void);
static void Error_Handler(void);
void WWDG_IRQHandler()
{
while (1);
}
void uart_print(char *s)
{
/* Like, there is millions of lines of perfectly useless pseudo-comments in the HAL driver. But don't think they'd
* say what unit that timeout parameter has in any obvious place. I'll just go with milliseconds here -.- */
HAL_USART_Transmit(&console_uart, (uint8_t*)s, strlen(s), 1000);
}
void uart_putc(char c)
{
/* See above. */
HAL_USART_Transmit(&console_uart, (uint8_t*)&c, 1, 1000);
}
void HAL_USART_MspInit(USART_HandleTypeDef *narf)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/* TX */
GPIO_InitTypeDef pa9 = {
.Pin = GPIO_PIN_9,
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_PULLUP,
.Speed = GPIO_SPEED_FREQ_LOW,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &pa9);
/* RX */
GPIO_InitTypeDef pa10 = {
.Pin = GPIO_PIN_10,
.Mode = GPIO_MODE_AF_PP,
.Pull = GPIO_PULLUP,
.Speed = GPIO_SPEED_FREQ_LOW,
.Alternate = GPIO_AF7_USART1
};
HAL_GPIO_Init(GPIOA, &pa10);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
/* FIXME
HID_InitApplication();
USBH_Init(&hUSBHost, USBH_UserProcess, 0);
USBH_RegisterClass(&hUSBHost, USBH_HID_CLASS);
USBH_Start(&hUSBHost);
*/
/* LEDs */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER |=
(1<<GPIO_MODER_MODER6_Pos)|
(1<<GPIO_MODER_MODER7_Pos);
GPIOA->OSPEEDR |=
(2<<GPIO_OSPEEDR_OSPEED6_Pos)|
(2<<GPIO_OSPEEDR_OSPEED7_Pos);
USART_HandleTypeDef foo = {
.Instance = USART1,
.Init = {
.BaudRate = 115200,
.WordLength = USART_WORDLENGTH_8B,
.StopBits = USART_STOPBITS_1,
.Parity = USART_PARITY_NONE,
.Mode = USART_MODE_TX_RX
}
};
console_uart = foo;
HAL_USART_Init(&console_uart);
static int ticks = 0;
while (1) {
USBH_Process(&hUSBHost);
HID_MenuProcess();
/* FIXME USBH_Process(&hUSBHost); */
/* FIXME HID_MenuProcess(); */
Toggle_Leds();
if (ticks++ == 1000000) {
uart_print("This is a test\r\n");
ticks = 0;
}
}
}
static void HID_InitApplication(void)
{
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
BSP_PB_Init(BUTTON_WAKEUP, BUTTON_MODE_GPIO);
HID_MenuInit();
}
@ -104,97 +179,53 @@ static void USBH_UserProcess(USBH_HandleTypeDef* phost, uint8_t id)
}
}
void Toggle_Leds(void)
{
void Toggle_Leds(void) {
static uint32_t ticks;
if (ticks++ == 100) {
BSP_LED_Toggle(LED1);
BSP_LED_Toggle(LED2);
BSP_LED_Toggle(LED3);
BSP_LED_Toggle(LED4);
if (ticks++ == 1000000) {
GPIOA->ODR ^= GPIO_ODR_OD6;
GPIOA->ODR ^= GPIO_ODR_OD7;
ticks = 0;
}
}
/**
* @brief System Clock Configuration
* The system Clock is configured as follow :
* System Clock source = PLL (HSE)
* SYSCLK(Hz) = 180000000
* HCLK(Hz) = 180000000
* AHB Prescaler = 1
* APB1 Prescaler = 4
* APB2 Prescaler = 2
* HSE Frequency(Hz) = 8000000
* PLL_M = 8
* PLL_N = 360
* PLL_P = 2
* PLL_Q = 7
* PLL_R = 2
* VDD(V) = 3.3
* Main regulator output voltage = Scale1 mode
* Flash Latency(WS) = 5
* The USB clock configuration from PLLSAI:
* PLLSAIM = 8
* PLLSAIN = 384
* PLLSAIP = 8
* @param None
* @retval None
*/
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* The voltage scaling allows optimizing the power consumption when the device
is
clocked below the maximum system frequency, to update the voltage scaling
value
regarding system frequency refer to product datasheet. */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
#if defined(USE_STM32469I_DISCO_REVA)
RCC_OscInitStruct.PLL.PLLM = 25;
#else
RCC_OscInitStruct.PLL.PLLM = 8;
#endif /* USE_STM32469I_DISCO_REVA */
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
RCC_OscInitTypeDef foo = {
.OscillatorType = RCC_OSCILLATORTYPE_HSE,
.HSEState = RCC_HSE_ON,
.PLL.PLLState = RCC_PLL_ON,
.PLL.PLLSource = RCC_PLLSOURCE_HSE,
/* HSE input: 8MHz */
.PLL.PLLM = 8, /* VCO in: 1MHz = 8MHz / 8 */
.PLL.PLLN = 336, /* VCO out: 336MHz = 1MHz * 336 */
.PLL.PLLP = RCC_PLLP_DIV2, /* System: 168MHz = 336Mhz / 2 */
.PLL.PLLQ = 7 /* USB: 48MHz = 336MHz / 7 */
};
if (HAL_RCC_OscConfig(&foo) != HAL_OK)
Error_Handler();
/* Enable the OverDrive to reach the 180 Mhz Frequency */
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
Error_Handler();
/* Select PLLSAI output as USB clock source */
PeriphClkInitStruct.PLLSAI.PLLSAIQ = 7;
PeriphClkInitStruct.PLLSAI.PLLSAIN = 384;
PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV8;
/* FIXME does this require configuration?
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CK48;
PeriphClkInitStruct.Clk48ClockSelection = RCC_CK48CLKSOURCE_PLLSAIP;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
*/
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
RCC_ClkInitTypeDef bar = {
.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2),
.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK, /* See above */
.AHBCLKDivider = RCC_SYSCLK_DIV1, /* 168MHz = 168MHz / 1 */
.APB1CLKDivider = RCC_HCLK_DIV4, /* 42MHz = 168MHz / 4 */
.APB2CLKDivider = RCC_HCLK_DIV2 /* 84Mhz = 168MHz / 2 */
};
if (HAL_RCC_ClockConfig(&bar, FLASH_LATENCY_5) != HAL_OK)
Error_Handler();
}
@ -212,3 +243,5 @@ static void Error_Handler(void)
;
}
void _init(void) {}

7
main.h
View file

@ -47,11 +47,12 @@
#ifndef __MAIN_H
#define __MAIN_H
#include "stdio.h"
#include "usbh_core.h"
#include "usbh_hid.h"
#include "usbh_hid_parser.h"
#include "stdio.h"
typedef enum {
HID_DEMO_START = 0,
HID_DEMO_KEYBOARD,
@ -81,6 +82,7 @@ typedef enum {
extern USBH_HandleTypeDef hUSBHost;
extern HID_ApplicationTypeDef Appli_state;
extern HID_DEMO_StateMachine hid_demo;
extern USART_HandleTypeDef console_uart;
void Toggle_Leds(void);
void HID_SelectItem(uint8_t** menu, uint8_t item);
@ -89,4 +91,7 @@ void HID_MenuProcess(void);
void HID_KeyboardMenuProcess(void);
void USR_KEYBRD_ProcessData(uint8_t data);
void uart_print(char *s);
void uart_putc(char c);
#endif

View file

@ -47,24 +47,45 @@
#define HAL_MODULE_ENABLED
#define HAL_DMA_MODULE_ENABLED
#include "stm32f4xx_hal_dma.h"
#include "stm32f4xx_hal_def.h"
#define HAL_DMA2D_MODULE_ENABLED
#include "stm32f4xx_hal_dma2d.h"
#define HAL_RCC_MODULE_ENABLED
#include "stm32f4xx_hal_rcc.h"
#define HAL_FLASH_MODULE_ENABLED
#include "stm32f4xx_hal_flash.h"
#define HAL_DMA_MODULE_ENABLED
#include "stm32f4xx_hal_dma.h"
#define HAL_USART_MODULE_ENABLED
#include "stm32f4xx_hal_usart.h"
#define HAL_PWR_MODULE_ENABLED
#include "stm32f4xx_hal_pwr.h"
/* USB host controller foo */
#define HAL_HCD_MODULE_ENABLED
#include "stm32f4xx_hal_hcd.h"
/* Whoever needs a HAL for GPIOs... */
#define HAL_GPIO_MODULE_ENABLED
#include "stm32f4xx_hal_gpio.h"
/* Why not wrap the wrapper? */
#define HAL_CORTEX_MODULE_ENABLED
#include "stm32f4xx_hal_cortex.h"
/*
#define HAL_DMA2D_MODULE_ENABLED
#include "stm32f4xx_hal_dma2d.h"
#define HAL_SRAM_MODULE_ENABLED
#include "stm32f4xx_hal_sram.h"
#define HAL_SDRAM_MODULE_ENABLED
#include "stm32f4xx_hal_sdram.h"
#define HAL_GPIO_MODULE_ENABLED
#include "stm32f4xx_hal_gpio.h"
#define HAL_I2C_MODULE_ENABLED
#include "stm32f4xx_hal_i2c.h"
@ -74,23 +95,9 @@
#define HAL_DSI_MODULE_ENABLED
#include "stm32f4xx_hal_dsi.h"
#define HAL_PWR_MODULE_ENABLED
#include "stm32f4xx_hal_pwr.h"
#define HAL_RCC_MODULE_ENABLED
#include "stm32f4xx_hal_rcc.h"
#define HAL_UART_MODULE_ENABLED
#include "stm32f4xx_hal_uart.h"
#define HAL_CORTEX_MODULE_ENABLED
#include "stm32f4xx_hal_cortex.h"
#define HAL_HCD_MODULE_ENABLED
#include "stm32f4xx_hal_hcd.h"
#define HAL_FMPI2C_MODULE_ENABLED
#include "stm32f4xx_hal_fmpi2c.h"
*/
#if !defined(HSE_VALUE)
#if defined(USE_STM32469I_DISCO_REVA)

190
syscalls.c Normal file
View file

@ -0,0 +1,190 @@
/* Support files for GNU libc. Files in the system namespace go here.
Files in the C namespace (ie those that do not start with an
underscore) go in .c. */
#include <_ansi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/errno.h>
#include <reent.h>
#include <unistd.h>
#include <sys/wait.h>
#define FreeRTOS
#define MAX_STACK_SIZE 0x200
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
#ifndef FreeRTOS
register char * stack_ptr asm("sp");
#endif
caddr_t _sbrk(int incr)
{
extern char end asm("end");
static char *heap_end;
char *prev_heap_end,*min_stack_ptr;
if (heap_end == 0)
heap_end = &end;
prev_heap_end = heap_end;
#ifdef FreeRTOS
/* Use the NVIC offset register to locate the main stack pointer. */
min_stack_ptr = (char*)(*(unsigned int *)*(unsigned int *)0xE000ED08);
/* Locate the STACK bottom address */
min_stack_ptr -= MAX_STACK_SIZE;
if (heap_end + incr > min_stack_ptr)
#else
if (heap_end + incr > stack_ptr)
#endif
{
// write(1, "Heap and stack collision\n", 25);
// abort();
errno = ENOMEM;
return (caddr_t) -1;
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
/*
* _gettimeofday primitive (Stub function)
* */
int _gettimeofday (struct timeval * tp, struct timezone * tzp)
{
/* Return fixed data for the timezone. */
if (tzp)
{
tzp->tz_minuteswest = 0;
tzp->tz_dsttime = 0;
}
return 0;
}
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {}
}
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar( *ptr++ );
}
return len;
}
int _close(int file)
{
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
return 1;
}
int _lseek(int file, int ptr, int dir)
{
return 0;
}
int _read(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
int _open(char *path, int flags, ...)
{
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
return -1;
}
int _stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}

View file

@ -119,7 +119,7 @@ void SystemCoreClockUpdate(void)
case 0x04: /* HSE */
SystemCoreClock = HSE_VALUE;
break;
case 0x08: /* PLL */
case 0x08: {/* PLL */
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N
SYSCLK = PLL_VCO / PLL_P */
uint32_t pllvco;
@ -132,13 +132,12 @@ void SystemCoreClockUpdate(void)
uint32_t pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> 16) + 1) * 2;
SystemCoreClock = pllvco / pllp;
break;
break; }
default:
SystemCoreClock = HSI_VALUE;
break;
}
/* Compute HCLK frequency */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
SystemCoreClock >>= tmp;
SystemCoreClock >>= AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
}