diff --git a/targets/stm32l432/bootloader/bootloader.h b/targets/stm32l432/bootloader/bootloader.h index 22e5db3..4cd23be 100644 --- a/targets/stm32l432/bootloader/bootloader.h +++ b/targets/stm32l432/bootloader/bootloader.h @@ -1,21 +1,21 @@ /* * Copyright (C) 2018 SoloKeys, Inc. - * + * * This file is part of Solo. - * + * * Solo 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. - * + * * Solo 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 Solo. If not, see - * + * * This code is available under licenses for commercial use. * Please contact SoloKeys for more information. */ @@ -66,7 +66,7 @@ #define SKIP_BUTTON_CHECK_FAST 1 void printing_init(); -void hw_init(void); +void hw_init(int lf); // Trigger software reset void device_reboot(); diff --git a/targets/stm32l432/bootloader/main.c b/targets/stm32l432/bootloader/main.c index afdc15f..d1325af 100644 --- a/targets/stm32l432/bootloader/main.c +++ b/targets/stm32l432/bootloader/main.c @@ -23,6 +23,9 @@ #include #include +#include "stm32l4xx_ll_rcc.h" +#include "stm32l4xx.h" + #include "cbor.h" #include "device.h" #include "ctaphid.h" @@ -32,9 +35,8 @@ #include "ctap.h" #include "app.h" #include "memory_layout.h" -#include "stm32l4xx_ll_rcc.h" +#include "init.h" -#include "stm32l4xx.h" uint8_t REBOOT_FLAG = 0; @@ -82,7 +84,15 @@ int main(int argc, char * argv[]) TAG_ERR ); - device_init(); + // device_init(); + SystemClock_Config_LF(); + init_gpio(); + init_millisecond_timer(1); + +#if DEBUG_LEVEL > 0 + init_debug_uart(); +#endif + printf1(TAG_GEN,"init device\n"); t1 = millis(); @@ -118,7 +128,14 @@ int main(int argc, char * argv[]) printf1(TAG_RED,"Not authorized to boot (%08x == %08lx)\r\n", AUTH_WORD_ADDR, *(uint32_t*)AUTH_WORD_ADDR); } start_bootloader: + + SystemClock_Config(); + init_gpio(); + init_millisecond_timer(0); + init_pwm(); + init_rng(); usbhid_init(); + printf1(TAG_GEN,"init usb\n"); ctaphid_init(); diff --git a/targets/stm32l432/src/init.c b/targets/stm32l432/src/init.c index 056a7ba..3c33c56 100644 --- a/targets/stm32l432/src/init.c +++ b/targets/stm32l432/src/init.c @@ -45,31 +45,20 @@ #include "usbd_composite.h" #include "usbd_cdc_if.h" #include "device.h" +#include "init.h" #include APP_CONFIG -/* USER CODE BEGIN Includes */ -/* USER CODE END Includes */ - -/* Private variables ---------------------------------------------------------*/ USBD_HandleTypeDef Solo_USBD_Device; -/* Private function prototypes -----------------------------------------------*/ static void LL_Init(void); -void SystemClock_Config(void); -static void MX_GPIO_Init(void); -static void MX_USART1_UART_Init(void); -static void MX_TIM2_Init(void); -static void MX_TIM6_Init(void); -static void MX_RNG_Init(void); -static void MX_SPI1_Init(void); #define Error_Handler() _Error_Handler(__FILE__,__LINE__) void _Error_Handler(char *file, int line); -void hw_init(void) +void hw_init(int lowfreq) { #ifdef IS_BOOTLOADER SCB->VTOR = FLASH_BASE; @@ -77,25 +66,33 @@ void hw_init(void) #endif LL_Init(); - SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); + if (lowfreq) + { + SystemClock_Config_LF(); + } + else + { + SystemClock_Config(); + } - SystemClock_Config(); // TODO bootloader should not change clk freq. - MX_GPIO_Init(); - MX_TIM2_Init(); // PWM for LEDs + init_gpio(); - MX_TIM6_Init(); // ~1 ms timer + if (!lowfreq) + { + init_pwm(); + } + + init_millisecond_timer(lowfreq); #if DEBUG_LEVEL > 0 - MX_USART1_UART_Init();// debug uart + init_debug_uart(); #endif - MX_RNG_Init(); - MX_SPI1_Init(); - TIM6->SR = 0; - __enable_irq(); - NVIC_EnableIRQ(TIM6_IRQn); + init_rng(); + init_spi(); + } static void LL_Init(void) @@ -122,259 +119,253 @@ static void LL_Init(void) NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); } -static int NFC = 0; + /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { - if (!NFC) - { + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); - LL_FLASH_SetLatency(LL_FLASH_LATENCY_2); + LL_FLASH_SetLatency(LL_FLASH_LATENCY_2); - if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2) - { - Error_Handler(); - } - LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2) + { + Error_Handler(); + } + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); - LL_RCC_HSI48_Enable(); + LL_RCC_HSI48_Enable(); - /* Wait till HSI48 is ready */ - while(LL_RCC_HSI48_IsReady() != 1) - { + /* Wait till HSI48 is ready */ + while(LL_RCC_HSI48_IsReady() != 1) + { - } + } - LL_RCC_LSI_Enable(); + LL_RCC_LSI_Enable(); - /* Wait till LSI is ready */ - while(LL_RCC_LSI_IsReady() != 1) - { + /* Wait till LSI is ready */ + while(LL_RCC_LSI_IsReady() != 1) + { - } - LL_RCC_MSI_Enable(); - /* Wait till MSI is ready */ - while(LL_RCC_MSI_IsReady() != 1) - { + } + LL_RCC_MSI_Enable(); + /* Wait till MSI is ready */ + while(LL_RCC_MSI_IsReady() != 1) + { - } - LL_RCC_MSI_EnableRangeSelection(); + } + LL_RCC_MSI_EnableRangeSelection(); - LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_11); + LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_11); - LL_RCC_MSI_SetCalibTrimming(0); + LL_RCC_MSI_SetCalibTrimming(0); - LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI); + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI); - /* Wait till System clock is ready */ - while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI) - { + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI) + { - } - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + } + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); - LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); - LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16); + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16); - LL_Init1msTick(48000000); + LL_Init1msTick(48000000); - LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); + LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); - LL_SetSystemCoreClock(48000000); + LL_SetSystemCoreClock(48000000); - LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); + LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); - LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_HSI48); + LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_HSI48); - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CRS); + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CRS); - LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_CRS); + LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_CRS); - LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_CRS); + LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_CRS); - LL_CRS_SetSyncDivider(LL_CRS_SYNC_DIV_1); + LL_CRS_SetSyncDivider(LL_CRS_SYNC_DIV_1); - LL_CRS_SetSyncPolarity(LL_CRS_SYNC_POLARITY_RISING); + LL_CRS_SetSyncPolarity(LL_CRS_SYNC_POLARITY_RISING); - LL_CRS_SetSyncSignalSource(LL_CRS_SYNC_SOURCE_USB); + LL_CRS_SetSyncSignalSource(LL_CRS_SYNC_SOURCE_USB); - LL_CRS_SetReloadCounter(__LL_CRS_CALC_CALCULATE_RELOADVALUE(48000000,1000)); + LL_CRS_SetReloadCounter(__LL_CRS_CALC_CALCULATE_RELOADVALUE(48000000,1000)); - LL_CRS_SetFreqErrorLimit(34); + LL_CRS_SetFreqErrorLimit(34); - LL_CRS_SetHSI48SmoothTrimming(32); - - /* SysTick_IRQn interrupt configuration */ - NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); - } - else - { - LL_FLASH_SetLatency(LL_FLASH_LATENCY_0); - - if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_0) - { - Error_Handler(); - } - LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); - - LL_RCC_LSI_Enable(); - - /* Wait till LSI is ready */ - while(LL_RCC_LSI_IsReady() != 1) - { - - } - LL_RCC_MSI_Enable(); - - /* Wait till MSI is ready */ - while(LL_RCC_MSI_IsReady() != 1) - { - - } - LL_RCC_MSI_EnableRangeSelection(); - - LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6); - - LL_RCC_MSI_SetCalibTrimming(0); - - LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI); - - /* Wait till System clock is ready */ - while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI) - { - - } - LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); - - LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); - - LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); - - LL_Init1msTick(4000000); - - LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); - - LL_SetSystemCoreClock(4000000); - - LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); - - LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_MSI); - - /* SysTick_IRQn interrupt configuration */ - NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); - } + LL_CRS_SetHSI48SmoothTrimming(32); + /* SysTick_IRQn interrupt configuration */ + NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); } -void usb_init() +void SystemClock_Config_LF(void) { - if (!NFC) + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_PWREN); + + LL_FLASH_SetLatency(LL_FLASH_LATENCY_0); + + if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_0) { - // enable USB power - SET_BIT(PWR->CR2, PWR_CR2_USV); - - // Enable USB Clock - SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); - - - USBD_Composite_Set_Classes(&USBD_HID, &USBD_CDC); - in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; - out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; - - in_endpoint_to_class[CDC_IN_EP & 0x7F] = 1; - out_endpoint_to_class[CDC_OUT_EP & 0x7F] = 1; - - USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); - USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); - // - // USBD_RegisterClass(&Solo_USBD_Device, &USBD_CDC); - USBD_CDC_RegisterInterface(&Solo_USBD_Device, &USBD_Interface_fops_FS); - - USBD_Start(&Solo_USBD_Device); + Error_Handler(); } + LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE1); + + LL_RCC_LSI_Enable(); + + /* Wait till LSI is ready */ + while(LL_RCC_LSI_IsReady() != 1) + { + + } + LL_RCC_MSI_Enable(); + + /* Wait till MSI is ready */ + while(LL_RCC_MSI_IsReady() != 1) + { + + } + LL_RCC_MSI_EnableRangeSelection(); + + LL_RCC_MSI_SetRange(LL_RCC_MSIRANGE_6); + + LL_RCC_MSI_SetCalibTrimming(0); + + LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_MSI); + + /* Wait till System clock is ready */ + while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_MSI) + { + + } + LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1); + + LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1); + + LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1); + + LL_Init1msTick(4000000); + + LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK); + + LL_SetSystemCoreClock(4000000); + + LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2); + + LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_MSI); + + /* SysTick_IRQn interrupt configuration */ + NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0)); + } -/* TIM2 init function */ -static void MX_TIM2_Init(void) +void init_usb() { - // if(!NFC) - { - LL_TIM_InitTypeDef TIM_InitStruct; - LL_TIM_OC_InitTypeDef TIM_OC_InitStruct; + // enable USB power + SET_BIT(PWR->CR2, PWR_CR2_USV); - LL_GPIO_InitTypeDef GPIO_InitStruct; + // Enable USB Clock + SET_BIT(RCC->APB1ENR1, RCC_APB1ENR1_USBFSEN); - /* Peripheral clock enable */ - LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); - TIM2->SR = 0 ; + USBD_Composite_Set_Classes(&USBD_HID, &USBD_CDC); + in_endpoint_to_class[HID_EPIN_ADDR & 0x7F] = 0; + out_endpoint_to_class[HID_EPOUT_ADDR & 0x7F] = 0; - TIM_InitStruct.Prescaler = 0; - TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 1000; - TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; - LL_TIM_Init(TIM2, &TIM_InitStruct); + in_endpoint_to_class[CDC_IN_EP & 0x7F] = 1; + out_endpoint_to_class[CDC_OUT_EP & 0x7F] = 1; - LL_TIM_EnableARRPreload(TIM2); + USBD_Init(&Solo_USBD_Device, &Solo_Desc, 0); + USBD_RegisterClass(&Solo_USBD_Device, &USBD_Composite); + // USBD_RegisterClass(&Solo_USBD_Device, &USBD_HID); + // + // USBD_RegisterClass(&Solo_USBD_Device, &USBD_CDC); + USBD_CDC_RegisterInterface(&Solo_USBD_Device, &USBD_Interface_fops_FS); - LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL); + USBD_Start(&Solo_USBD_Device); +} - TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1; - TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; - TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; - TIM_OC_InitStruct.CompareValue = 1000; - TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; - LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct); +void init_pwm(void) +{ - LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2); + LL_TIM_InitTypeDef TIM_InitStruct; + LL_TIM_OC_InitTypeDef TIM_OC_InitStruct; - TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; - TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; - LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH3, &TIM_OC_InitStruct); + LL_GPIO_InitTypeDef GPIO_InitStruct; - LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH3); + /* Peripheral clock enable */ + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); - TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; - TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; - LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH4, &TIM_OC_InitStruct); + TIM2->SR = 0 ; - LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH4); + TIM_InitStruct.Prescaler = 0; + TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; + TIM_InitStruct.Autoreload = 1000; + TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; + LL_TIM_Init(TIM2, &TIM_InitStruct); - LL_TIM_SetOCRefClearInputSource(TIM2, LL_TIM_OCREF_CLR_INT_NC); + LL_TIM_EnableARRPreload(TIM2); - LL_TIM_DisableExternalClock(TIM2); + LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL); - LL_TIM_ConfigETR(TIM2, LL_TIM_ETR_POLARITY_NONINVERTED, LL_TIM_ETR_PRESCALER_DIV1, LL_TIM_ETR_FILTER_FDIV1); + TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1; + TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; + TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; + TIM_OC_InitStruct.CompareValue = 1000; + TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; + LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct); - LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET); + LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2); - LL_TIM_DisableMasterSlaveMode(TIM2); + TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; + TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; + LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH3, &TIM_OC_InitStruct); - /**TIM2 GPIO Configuration - PA1 ------> TIM2_CH2 - PA2 ------> TIM2_CH3 - PA3 ------> TIM2_CH4 - */ - GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3; - GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; - GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; - GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; - GPIO_InitStruct.Alternate = LL_GPIO_AF_1; - LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH3); - LL_TIM_EnableCounter(TIM2); - } + TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE; + TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_ENABLE; + LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH4, &TIM_OC_InitStruct); + + LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH4); + + LL_TIM_SetOCRefClearInputSource(TIM2, LL_TIM_OCREF_CLR_INT_NC); + + LL_TIM_DisableExternalClock(TIM2); + + LL_TIM_ConfigETR(TIM2, LL_TIM_ETR_POLARITY_NONINVERTED, LL_TIM_ETR_PRESCALER_DIV1, LL_TIM_ETR_FILTER_FDIV1); + + LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET); + + LL_TIM_DisableMasterSlaveMode(TIM2); + + /**TIM2 GPIO Configuration + PA1 ------> TIM2_CH2 + PA2 ------> TIM2_CH3 + PA3 ------> TIM2_CH4 + */ + GPIO_InitStruct.Pin = LL_GPIO_PIN_1|LL_GPIO_PIN_2|LL_GPIO_PIN_3; + GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE; + GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + GPIO_InitStruct.Pull = LL_GPIO_PULL_NO; + GPIO_InitStruct.Alternate = LL_GPIO_AF_1; + LL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + LL_TIM_EnableCounter(TIM2); } -/* USART1 init function */ -static void MX_USART1_UART_Init(void) +void init_debug_uart(void) { LL_USART_InitTypeDef USART_InitStruct; @@ -411,20 +402,18 @@ static void MX_USART1_UART_Init(void) } -/** Pinout Configuration -*/ -static void MX_GPIO_Init(void) +void init_gpio(void) { /* GPIO Ports Clock Enable */ LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA); LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB); + LL_GPIO_SetPinMode(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_MODE_INPUT); + LL_GPIO_SetPinPull(SOLO_BUTTON_PORT,SOLO_BUTTON_PIN,LL_GPIO_PULL_UP); } - -/* TIM6 init function */ -static void MX_TIM6_Init(void) +void init_millisecond_timer(int lf) { LL_TIM_InitTypeDef TIM_InitStruct; @@ -434,7 +423,7 @@ static void MX_TIM6_Init(void) // 48 MHz sys clock --> 6 MHz timer clock // 48 MHz / 48000 == 1000 Hz - if (!NFC) + if (!lf) TIM_InitStruct.Prescaler = 48000; else TIM_InitStruct.Prescaler = 4000; @@ -454,39 +443,14 @@ static void MX_TIM6_Init(void) // Start immediately LL_TIM_EnableCounter(TIM6); + + TIM6->SR = 0; + __enable_irq(); + NVIC_EnableIRQ(TIM6_IRQn); } -/* TIM7 init function */ -// static void MX_TIM7_Init(void) -// { -// -// LL_TIM_InitTypeDef TIM_InitStruct; -// -// /* Peripheral clock enable */ -// LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM7); -// -// // 48 MHz sys clock --> 6 MHz timer clock -// // 6 MHz / 6000 == 1000 Hz -// TIM_InitStruct.Prescaler = 48000; -// TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; -// TIM_InitStruct.Autoreload = 0xffff; -// LL_TIM_Init(TIM6, &TIM_InitStruct); -// -// LL_TIM_DisableARRPreload(TIM7); -// -// LL_TIM_SetTriggerOutput(TIM7, LL_TIM_TRGO_RESET); -// -// LL_TIM_DisableMasterSlaveMode(TIM7); -// -// // enable interrupt -// TIM7->DIER |= 1; -// -// // Start immediately -// LL_TIM_EnableCounter(TIM7); -// } -/* RNG init function */ -static void MX_RNG_Init(void) +void init_rng(void) { /* Peripheral clock enable */ @@ -497,7 +461,7 @@ static void MX_RNG_Init(void) } /* SPI1 init function */ -static void MX_SPI1_Init(void) +void init_spi(void) { LL_SPI_InitTypeDef SPI_InitStruct; @@ -527,10 +491,7 @@ static void MX_SPI1_Init(void) SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW; SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE; SPI_InitStruct.NSS = LL_SPI_NSS_SOFT; - // if (!NFC) - // SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV64; - // else - SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2; + SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV2; SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST; SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE; SPI_InitStruct.CRCPoly = 7; @@ -538,7 +499,5 @@ static void MX_SPI1_Init(void) LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA); - // LL_SPI_EnableNSSPulseMgt(SPI1); - } diff --git a/targets/stm32l432/src/init.h b/targets/stm32l432/src/init.h new file mode 100644 index 0000000..1319a1e --- /dev/null +++ b/targets/stm32l432/src/init.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 SoloKeys, Inc. + * + * This file is part of Solo. + * + * Solo 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. + * + * Solo 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 Solo. If not, see + * + * This code is available under licenses for commercial use. + * Please contact SoloKeys for more information. + */ +#ifndef _INIT_H_ +#define _INIT_H_ + +void SystemClock_Config(void); +void SystemClock_Config_LF(void); + +void init_usb(); +void init_gpio(void); +void init_debug_uart(void); +void init_pwm(void); +void init_millisecond_timer(int lf); +void init_rng(void); +void init_spi(void); + + +#endif