-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bracz-lib-clean
* master: Ensures that 'make clean' empties lib directory of symlinks. (#753) Add an input/output GPIO type. This helps support bit banged I2C. (#752) Adds TinyUsb based CDC virtual Com port driver with an STM32 port. (#748) Limit the amount of input we read into memory in arduino sketches. (#746) Updates to send_datagram cmdline utility (#740)
- Loading branch information
Showing
25 changed files
with
1,339 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
281 changes: 281 additions & 0 deletions
281
applications/usb_can/targets/freertos.armv6m.stm32f072cb-gol/HwInit.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,281 @@ | ||
/** \copyright | ||
* Copyright (c) 2012, Stuart W Baker | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* \file HwInit.cxx | ||
* This file represents the hardware initialization for the TI Tiva MCU. | ||
* | ||
* @author Stuart W. Baker | ||
* @date 5 January 2013 | ||
*/ | ||
|
||
#include <new> | ||
#include <cstdint> | ||
|
||
#include "freertos_drivers/st/stm32f_hal_conf.hxx" | ||
#include "stm32f0xx_hal_rcc.h" | ||
#include "stm32f0xx_hal_flash.h" | ||
#include "stm32f0xx_hal_gpio.h" | ||
#include "stm32f0xx_hal_gpio_ex.h" | ||
#include "stm32f0xx_hal_dma.h" | ||
#include "stm32f0xx_hal_tim.h" | ||
|
||
#include "os/OS.hxx" | ||
#include "Stm32Uart.hxx" | ||
#include "Stm32Can.hxx" | ||
#include "Stm32UsbCdc.hxx" | ||
#include "Stm32EEPROMEmulation.hxx" | ||
#include "hardware.hxx" | ||
|
||
/** override stdin */ | ||
const char *STDIN_DEVICE = "/dev/ser0"; | ||
|
||
/** override stdout */ | ||
const char *STDOUT_DEVICE = "/dev/ser0"; | ||
|
||
/** override stderr */ | ||
const char *STDERR_DEVICE = "/dev/ser0"; | ||
|
||
/** UART 0 serial driver instance */ | ||
// static Stm32Uart uart0("/dev/ser0", USART1, USART1_IRQn); | ||
|
||
/** CAN 0 CAN driver instance */ | ||
static Stm32Can can0("/dev/can0"); | ||
|
||
/** EEPROM emulation driver. The file size might be made bigger. */ | ||
static Stm32EEPROMEmulation eeprom0("/dev/eeprom", 512); | ||
|
||
const size_t EEPROMEmulation::SECTOR_SIZE = 2048; | ||
|
||
/** USB-Serial instance */ | ||
static Stm32UsbCdc serUSB0("/dev/serUSB0"); | ||
|
||
extern "C" { | ||
|
||
/** Blink LED */ | ||
uint32_t blinker_pattern = 0; | ||
static uint32_t rest_pattern = 0; | ||
|
||
void hw_set_to_safe(void) | ||
{ | ||
} | ||
|
||
void resetblink(uint32_t pattern) | ||
{ | ||
BLINKER_RAW_Pin::set(pattern ? true : false); | ||
blinker_pattern = pattern; | ||
/* make a timer event trigger immediately */ | ||
} | ||
|
||
void setblink(uint32_t pattern) | ||
{ | ||
resetblink(pattern); | ||
} | ||
|
||
void timer14_interrupt_handler(void) | ||
{ | ||
// | ||
// Clear the timer interrupt. | ||
// | ||
TIM14->SR = ~TIM_IT_UPDATE; | ||
|
||
// Set output LED. | ||
BLINKER_RAW_Pin::set(rest_pattern & 1); | ||
|
||
// Shift and maybe reset pattern. | ||
rest_pattern >>= 1; | ||
if (!rest_pattern) | ||
{ | ||
rest_pattern = blinker_pattern; | ||
} | ||
} | ||
|
||
void diewith(uint32_t pattern) | ||
{ | ||
// vPortClearInterruptMask(0x20); | ||
asm("cpsie i\n"); | ||
|
||
resetblink(pattern); | ||
while (1) | ||
; | ||
} | ||
|
||
/** Setup the system clock */ | ||
static void clock_setup(void) | ||
{ | ||
/* reset clock configuration to default state */ | ||
RCC->CR = RCC_CR_HSITRIM_4 | RCC_CR_HSION; | ||
while (!(RCC->CR & RCC_CR_HSIRDY)) | ||
; | ||
|
||
#define USE_EXTERNAL_8_MHz_CLOCK_SOURCE 0 | ||
/* configure PLL: 8 MHz * 6 = 48 MHz */ | ||
#if USE_EXTERNAL_8_MHz_CLOCK_SOURCE | ||
RCC->CR |= RCC_CR_HSEON; | ||
while (!(RCC->CR & RCC_CR_HSERDY)) | ||
; | ||
RCC->CFGR = RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLSRC_HSE_PREDIV | RCC_CFGR_SW_HSE; | ||
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_HSE)) | ||
; | ||
#else | ||
RCC->CFGR = RCC_CFGR_PLLMUL6 | RCC_CFGR_PLLSRC_HSI_PREDIV | RCC_CFGR_SW_HSI; | ||
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_HSI)) | ||
; | ||
#endif | ||
/* enable PLL */ | ||
RCC->CR |= RCC_CR_PLLON; | ||
while (!(RCC->CR & RCC_CR_PLLRDY)) | ||
; | ||
|
||
/* set PLL as system clock */ | ||
RCC->CFGR = (RCC->CFGR & (~RCC_CFGR_SW)) | RCC_CFGR_SW_PLL; | ||
while (!((RCC->CFGR & RCC_CFGR_SWS) == RCC_CFGR_SWS_PLL)) | ||
; | ||
|
||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; | ||
/* Select HSI48 as USB clock source */ | ||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB; | ||
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48; | ||
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); | ||
|
||
/*Configure the clock recovery system (CRS)********************************/ | ||
|
||
/*Enable CRS Clock*/ | ||
__HAL_RCC_CRS_CLK_ENABLE(); | ||
|
||
RCC_CRSInitTypeDef RCC_CRSInitStruct = {0}; | ||
|
||
/* Default Synchro Signal division factor (not divided) */ | ||
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1; | ||
|
||
/* Set the SYNCSRC[1:0] bits according to CRS_Source value */ | ||
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB; | ||
|
||
/* HSI48 is synchronized with USB SOF at 1KHz rate */ | ||
RCC_CRSInitStruct.ReloadValue = | ||
__HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000); | ||
RCC_CRSInitStruct.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT; | ||
|
||
/* Set the TRIM[5:0] to the default value*/ | ||
RCC_CRSInitStruct.HSI48CalibrationValue = 0x20; | ||
|
||
/* Start automatic synchronization */ | ||
HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct); | ||
} | ||
|
||
/** Initialize the processor hardware. | ||
*/ | ||
void hw_preinit(void) | ||
{ | ||
/* Globally disables interrupts until the FreeRTOS scheduler is up. */ | ||
asm("cpsid i\n"); | ||
|
||
/* these FLASH settings enable opertion at 48 MHz */ | ||
__HAL_FLASH_PREFETCH_BUFFER_ENABLE(); | ||
__HAL_FLASH_SET_LATENCY(FLASH_LATENCY_1); | ||
|
||
/* setup the system clock */ | ||
clock_setup(); | ||
|
||
/* enable peripheral clocks */ | ||
__HAL_RCC_GPIOA_CLK_ENABLE(); | ||
__HAL_RCC_GPIOB_CLK_ENABLE(); | ||
__HAL_RCC_GPIOC_CLK_ENABLE(); | ||
__HAL_RCC_USART1_CLK_ENABLE(); | ||
__HAL_RCC_CAN1_CLK_ENABLE(); | ||
__HAL_RCC_TIM14_CLK_ENABLE(); | ||
__HAL_RCC_USB_CLK_ENABLE(); | ||
|
||
/* setup pinmux */ | ||
GPIO_InitTypeDef gpio_init; | ||
memset(&gpio_init, 0, sizeof(gpio_init)); | ||
|
||
/* USART1 pinmux on PA9 and PA10 */ | ||
gpio_init.Mode = GPIO_MODE_AF_PP; | ||
gpio_init.Pull = GPIO_PULLUP; | ||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH; | ||
gpio_init.Alternate = GPIO_AF1_USART1; | ||
gpio_init.Pin = GPIO_PIN_9; | ||
HAL_GPIO_Init(GPIOA, &gpio_init); | ||
gpio_init.Pin = GPIO_PIN_10; | ||
HAL_GPIO_Init(GPIOA, &gpio_init); | ||
|
||
/* CAN pinmux !!OPEN-DRAIN!! on PB8 and PB9 */ | ||
gpio_init.Mode = GPIO_MODE_AF_OD; | ||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH; | ||
gpio_init.Pull = GPIO_PULLUP; | ||
gpio_init.Alternate = GPIO_AF4_CAN; | ||
gpio_init.Pin = GPIO_PIN_8; | ||
HAL_GPIO_Init(GPIOB, &gpio_init); | ||
gpio_init.Pin = GPIO_PIN_9; | ||
HAL_GPIO_Init(GPIOB, &gpio_init); | ||
|
||
// USB Pins | ||
// Configure USB DM and DP pins. This is optional, and maintained only for | ||
// user guidance. | ||
gpio_init.Pin = (GPIO_PIN_11 | GPIO_PIN_12); | ||
gpio_init.Mode = GPIO_MODE_INPUT; | ||
gpio_init.Pull = GPIO_NOPULL; | ||
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH; | ||
HAL_GPIO_Init(GPIOA, &gpio_init); | ||
|
||
GpioInit::hw_init(); | ||
|
||
/* Initializes the blinker timer. */ | ||
TIM_HandleTypeDef TimHandle; | ||
memset(&TimHandle, 0, sizeof(TimHandle)); | ||
TimHandle.Instance = TIM14; | ||
TimHandle.Init.Period = configCPU_CLOCK_HZ / 10000 / 8; | ||
TimHandle.Init.Prescaler = 10000; | ||
TimHandle.Init.ClockDivision = 0; | ||
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; | ||
TimHandle.Init.RepetitionCounter = 0; | ||
if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) | ||
{ | ||
/* Initialization Error */ | ||
HASSERT(0); | ||
} | ||
if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) | ||
{ | ||
/* Starting Error */ | ||
HASSERT(0); | ||
} | ||
SetInterruptPriority(TIM14_IRQn, 0); | ||
NVIC_EnableIRQ(TIM14_IRQn); | ||
} | ||
|
||
void hw_postinit(void) | ||
{ | ||
serUSB0.hw_postinit(); | ||
} | ||
|
||
extern void dcd_int_handler(int); | ||
|
||
void usb_interrupt_handler(void) | ||
{ | ||
dcd_int_handler(0); | ||
} | ||
|
||
} // extern "C" |
Oops, something went wrong.