timers, pwm, printf
This commit is contained in:
parent
014a18acbd
commit
2bb4a9fe16
@ -2,7 +2,7 @@ CC=arm-none-eabi-gcc
|
||||
CP=arm-none-eabi-objcopy
|
||||
SZ=arm-none-eabi-size
|
||||
|
||||
SRC=src/main.c src/startup_stm32l432xx.s src/system_stm32l4xx.c $(wildcard lib/*c)
|
||||
SRC=src/main.c src/init.c src/redirect.c src/startup_stm32l432xx.s src/system_stm32l4xx.c $(wildcard lib/*c)
|
||||
OBJ=$(SRC:.c=.o)
|
||||
INC=-Isrc/ -Isrc/cmsis -Ilib/
|
||||
LDSCRIPT=stm32l432xx.ld
|
||||
@ -18,25 +18,30 @@ HW=-mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb
|
||||
CHIP=STM32L442xx
|
||||
|
||||
CFLAGS=$(INC) -c -D$(CHIP) -DUSE_FULL_LL_DRIVER -O0 -Wall -fdata-sections -ffunction-sections
|
||||
LDFLAGS=$(HW) -specs=nano.specs -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
|
||||
LDFLAGS=$(HW) -specs=nano.specs -specs=nosys.specs -T$(LDSCRIPT) -Wl,-Map=$(TARGET).map,--cref -Wl,--gc-sections
|
||||
|
||||
.PRECIOUS: %.o
|
||||
|
||||
all: $(TARGET).elf
|
||||
$(SZ) $^
|
||||
$(CP) -O ihex $^ $(TARGET).hex
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $^ $(HW) $(CFLAGS) -o $@
|
||||
|
||||
|
||||
%.o: %.s
|
||||
$(CC) $^ $(HW) $(CFLAGS) -o $@
|
||||
|
||||
%.elf: $(OBJ)
|
||||
$(CC) $^ $(HW) $(LDFLAGS) -o $@
|
||||
|
||||
%.hex: %.elf
|
||||
$(CP) -O ihex $^ $(TARGET).hex
|
||||
|
||||
clean:
|
||||
rm -f *.o src/*.o src/*.elf *.elf
|
||||
|
||||
flash:
|
||||
flash: $(TARGET).hex
|
||||
STM32_Programmer_CLI -c port=SWD -halt -d $(TARGET).hex -rst
|
||||
sleep 0.5
|
||||
python dfuse-tool/dfuse-tool.py --leave
|
||||
|
1367
targets/stm32l442/lib/stm32l4xx_ll_tim.c
Normal file
1367
targets/stm32l442/lib/stm32l4xx_ll_tim.c
Normal file
File diff suppressed because it is too large
Load Diff
5037
targets/stm32l442/lib/stm32l4xx_ll_tim.h
Normal file
5037
targets/stm32l442/lib/stm32l4xx_ll_tim.h
Normal file
File diff suppressed because it is too large
Load Diff
11
targets/stm32l442/src/app.h
Normal file
11
targets/stm32l442/src/app.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef _APP_H_
|
||||
#define _APP_H_
|
||||
#include <stdint.h>
|
||||
|
||||
#define DEBUG_UART USART1
|
||||
|
||||
extern uint32_t __65_seconds;
|
||||
|
||||
#define millis() (((uint32_t)TIM6->CNT) | (__65_seconds<<16))
|
||||
|
||||
#endif
|
301
targets/stm32l442/src/init.c
Normal file
301
targets/stm32l442/src/init.c
Normal file
@ -0,0 +1,301 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include "stm32l4xx.h"
|
||||
#include "stm32l4xx_ll_gpio.h"
|
||||
#include "stm32l4xx_ll_rcc.h"
|
||||
#include "stm32l4xx_ll_system.h"
|
||||
#include "stm32l4xx_ll_pwr.h"
|
||||
#include "stm32l4xx_ll_utils.h"
|
||||
#include "stm32l4xx_ll_cortex.h"
|
||||
#include "stm32l4xx_ll_gpio.h"
|
||||
#include "stm32l4xx_ll_usart.h"
|
||||
#include "stm32l4xx_ll_bus.h"
|
||||
#include "stm32l4xx_ll_tim.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* 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);
|
||||
|
||||
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
|
||||
void _Error_Handler(char *file, int line);
|
||||
|
||||
|
||||
void hw_init(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration----------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
LL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_TIM2_Init();
|
||||
|
||||
MX_TIM6_Init();
|
||||
|
||||
TIM6->SR = 0;
|
||||
__enable_irq();
|
||||
NVIC_EnableIRQ(TIM6_IRQn);
|
||||
|
||||
}
|
||||
static void LL_Init(void)
|
||||
{
|
||||
|
||||
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
|
||||
|
||||
NVIC_SetPriorityGrouping(4);
|
||||
|
||||
/* System interrupt init*/
|
||||
/* MemoryManagement_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(MemoryManagement_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* BusFault_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(BusFault_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* UsageFault_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(UsageFault_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* SVCall_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(SVCall_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* DebugMonitor_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(DebugMonitor_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* PendSV_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(PendSV_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
/* SysTick_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
|
||||
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);
|
||||
|
||||
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_11);
|
||||
|
||||
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_16);
|
||||
|
||||
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16);
|
||||
|
||||
LL_Init1msTick(48000000);
|
||||
|
||||
LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
|
||||
|
||||
LL_SetSystemCoreClock(48000000);
|
||||
|
||||
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
|
||||
|
||||
/* SysTick_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
}
|
||||
|
||||
/* TIM2 init function */
|
||||
static void MX_TIM2_Init(void)
|
||||
{
|
||||
|
||||
LL_TIM_InitTypeDef TIM_InitStruct;
|
||||
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct;
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
||||
|
||||
TIM2->SR = 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);
|
||||
|
||||
LL_TIM_EnableARRPreload(TIM2);
|
||||
|
||||
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
||||
|
||||
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_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH2);
|
||||
|
||||
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_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH3);
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
LL_USART_InitTypeDef USART_InitStruct;
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct;
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
|
||||
|
||||
/**USART1 GPIO Configuration
|
||||
PB6 ------> USART1_TX
|
||||
PB7 ------> USART1_RX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
USART_InitStruct.BaudRate = 115200;
|
||||
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
||||
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
||||
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
||||
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
||||
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
||||
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
||||
LL_USART_Init(USART1, &USART_InitStruct);
|
||||
|
||||
LL_USART_ConfigAsyncMode(USART1);
|
||||
|
||||
LL_USART_Enable(USART1);
|
||||
|
||||
}
|
||||
|
||||
/** Pinout Configuration
|
||||
*/
|
||||
static void MX_GPIO_Init(void)
|
||||
{
|
||||
|
||||
/* GPIO Ports Clock Enable */
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
|
||||
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* TIM6 init function */
|
||||
static void MX_TIM6_Init(void)
|
||||
{
|
||||
|
||||
LL_TIM_InitTypeDef TIM_InitStruct;
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM6);
|
||||
|
||||
// 48 MHz sys clock --> 6 MHz timer clock
|
||||
// 6 MHz / 6000 == 1000 Hz
|
||||
TIM_InitStruct.Prescaler = 6000;
|
||||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||||
TIM_InitStruct.Autoreload = 0xffff;
|
||||
LL_TIM_Init(TIM6, &TIM_InitStruct);
|
||||
|
||||
LL_TIM_DisableARRPreload(TIM6);
|
||||
|
||||
LL_TIM_SetTriggerOutput(TIM6, LL_TIM_TRGO_RESET);
|
||||
|
||||
LL_TIM_DisableMasterSlaveMode(TIM6);
|
||||
|
||||
// enable interrupt
|
||||
TIM6->DIER |= 1;
|
||||
|
||||
// Start immediately
|
||||
LL_TIM_EnableCounter(TIM6);
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "stm32l4xx.h"
|
||||
#include "stm32l4xx_ll_gpio.h"
|
||||
#include "stm32l4xx_ll_rcc.h"
|
||||
@ -9,174 +10,164 @@
|
||||
#include "stm32l4xx_ll_gpio.h"
|
||||
#include "stm32l4xx_ll_usart.h"
|
||||
#include "stm32l4xx_ll_bus.h"
|
||||
#include "stm32l4xx_ll_tim.h"
|
||||
|
||||
#include "app.h"
|
||||
|
||||
#define Error_Handler() _Error_Handler(__FILE__,__LINE__)
|
||||
|
||||
#define VCP_TX_Pin LL_GPIO_PIN_6
|
||||
#define VCP_RX_Pin LL_GPIO_PIN_7
|
||||
#define VCP_TX_GPIO_Port GPIOB
|
||||
#define VCP_RX_GPIO_Port GPIOB
|
||||
|
||||
void _Error_Handler(char *file, int line);
|
||||
|
||||
void delay(uint32_t ms)
|
||||
{
|
||||
volatile int x,y;
|
||||
for (x=0; x < 100; x++)
|
||||
for (y=0; y < ms * 20; y++)
|
||||
;
|
||||
}
|
||||
|
||||
void uart_write(USART_TypeDef *uart, uint8_t * data, uint32_t len)
|
||||
{
|
||||
while(len--)
|
||||
{
|
||||
while (! LL_USART_IsActiveFlag_TXE(uart))
|
||||
;
|
||||
LL_USART_TransmitData8(uart,*data++);
|
||||
}
|
||||
}
|
||||
|
||||
void MX_USART1_UART_Init(void)
|
||||
{
|
||||
|
||||
LL_USART_InitTypeDef USART_InitStruct;
|
||||
|
||||
LL_GPIO_InitTypeDef GPIO_InitStruct;
|
||||
|
||||
/* Peripheral clock enable */
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
|
||||
|
||||
/**USART1 GPIO Configuration
|
||||
PB6 ------> USART1_TX
|
||||
PB7 ------> USART1_RX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
USART_InitStruct.BaudRate = 115200;
|
||||
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
|
||||
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
|
||||
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
|
||||
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
|
||||
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
|
||||
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
|
||||
LL_USART_Init(USART1, &USART_InitStruct);
|
||||
|
||||
LL_USART_ConfigAsyncMode(USART1);
|
||||
|
||||
LL_USART_Enable(USART1);
|
||||
|
||||
}
|
||||
|
||||
// Generated via cube
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
|
||||
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);
|
||||
|
||||
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_11);
|
||||
|
||||
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_16);
|
||||
|
||||
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_16);
|
||||
|
||||
LL_Init1msTick(48000000);
|
||||
|
||||
LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
|
||||
|
||||
LL_SetSystemCoreClock(48000000);
|
||||
|
||||
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
|
||||
|
||||
/* SysTick_IRQn interrupt configuration */
|
||||
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||||
}
|
||||
|
||||
#define LED_PIN_G LL_GPIO_PIN_0
|
||||
#define LED_PIN_B LL_GPIO_PIN_1
|
||||
#define LED_PIN_R LL_GPIO_PIN_2
|
||||
#define LED_PORT GPIOA
|
||||
void hw_init()
|
||||
|
||||
void hw_init(void);
|
||||
|
||||
void delay(uint32_t ms)
|
||||
{
|
||||
uint32_t time = millis();
|
||||
while ((millis() - time) < ms)
|
||||
;
|
||||
}
|
||||
|
||||
void rgb(uint32_t hex)
|
||||
{
|
||||
uint32_t r = hex >> 16;
|
||||
uint32_t g = (hex >> 8)&0xff;
|
||||
uint32_t b = hex & 0xff;
|
||||
|
||||
// CCR2 == blue
|
||||
// CCR3 == red
|
||||
// CCR4 == green
|
||||
|
||||
// map and scale colors
|
||||
TIM2->CCR2 = 1000 - (b * 1000)/(256);
|
||||
TIM2->CCR3 = 1000 - (r * 1000)/(256*6);
|
||||
TIM2->CCR4 = 1000 - (g * 1000)/(256);
|
||||
}
|
||||
|
||||
void test_colors()
|
||||
{
|
||||
// Should produce pulsing of various colors
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int inc = 1;
|
||||
uint32_t time = 0;
|
||||
#define update() do {\
|
||||
i += inc;\
|
||||
if (i > 254)\
|
||||
{\
|
||||
inc *= -1;\
|
||||
}\
|
||||
else if (i == 0)\
|
||||
{\
|
||||
inc *= -1;\
|
||||
}\
|
||||
delay(2);\
|
||||
}while(0);
|
||||
|
||||
while(1)
|
||||
{
|
||||
|
||||
/*SystemClock_Config();*/
|
||||
// initialize GPIO
|
||||
// Enable clock to A,B,C
|
||||
SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOAEN);
|
||||
SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOBEN);
|
||||
SET_BIT(RCC->AHB2ENR, RCC_AHB2ENR_GPIOCEN);
|
||||
LL_GPIO_SetPinMode(LED_PORT, LED_PIN_R, LL_GPIO_MODE_OUTPUT);
|
||||
LL_GPIO_SetPinMode(LED_PORT, LED_PIN_G, LL_GPIO_MODE_OUTPUT);
|
||||
LL_GPIO_SetPinMode(LED_PORT, LED_PIN_B, LL_GPIO_MODE_OUTPUT);
|
||||
/* NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);*/
|
||||
/*SystemClock_Config();*/
|
||||
printf("%d: %d\r\n", j++, millis());
|
||||
|
||||
printf("white pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb(i | (i << 8) | (i << 16));
|
||||
}
|
||||
|
||||
printf("blue pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb(i);
|
||||
}
|
||||
|
||||
printf("green pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb(i<<8);
|
||||
}
|
||||
|
||||
printf("red pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb(i<<16);
|
||||
}
|
||||
|
||||
printf("purple pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb((i<<16) | i);
|
||||
}
|
||||
|
||||
printf("orange pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb((i<<16) | (i<<8));
|
||||
}
|
||||
|
||||
printf("yellow pulse\r\n");
|
||||
time = millis();
|
||||
while((millis() - time) < 5000)
|
||||
{
|
||||
update();
|
||||
rgb((i<<8) | (i<<0));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*MX_USART1_UART_Init();*/
|
||||
|
||||
}
|
||||
|
||||
uint32_t __65_seconds = 0;
|
||||
void TIM6_DAC_IRQHandler()
|
||||
{
|
||||
// timer is only 16 bits, so roll it over here
|
||||
TIM6->SR = 0;
|
||||
__65_seconds += 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint8_t msg[] = "hi !\r\n";
|
||||
int i = 'A';
|
||||
uint32_t i = 0;
|
||||
int inc = 1;
|
||||
hw_init();
|
||||
|
||||
|
||||
/*LL_GPIO_SetPinMode(LED_PORT, LED_PIN_R, LL_GPIO_MODE_OUTPUT);*/
|
||||
/*LL_GPIO_SetPinMode(LED_PORT, LED_PIN_G, LL_GPIO_MODE_OUTPUT);*/
|
||||
/*LL_GPIO_SetPinMode(LED_PORT, LED_PIN_B, LL_GPIO_MODE_OUTPUT);*/
|
||||
|
||||
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_R);*/
|
||||
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_G);*/
|
||||
/*LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_B);*/
|
||||
test_colors();
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
/*LL_GPIO_TogglePin(LED_PORT, LED_PIN_R);*/
|
||||
/*LL_GPIO_TogglePin(LED_PORT, LED_PIN_R);*/
|
||||
/*LL_GPIO_TogglePin(LED_PORT, LED_PIN_G);*/
|
||||
/*LL_GPIO_TogglePin(LED_PORT, LED_PIN_B);*/
|
||||
rgb(i | (i << 8) | (i << 16));
|
||||
|
||||
LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_R);
|
||||
LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_G);
|
||||
LL_GPIO_SetOutputPin(LED_PORT, LED_PIN_B);
|
||||
|
||||
LL_GPIO_ResetOutputPin(LED_PORT, LED_PIN_R);
|
||||
/*LL_GPIO_ResetOutputPin(LED_PORT, LED_PIN_G);*/
|
||||
/*LL_GPIO_ResetOutputPin(LED_PORT, LED_PIN_B);*/
|
||||
|
||||
|
||||
/*delay(10);*/
|
||||
msg[3] = i++;
|
||||
while (1)
|
||||
;
|
||||
/*uart_write(USART2, msg, sizeof(msg));*/
|
||||
delay(100);
|
||||
printf("%d: %d\r\n", i++, millis());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void _Error_Handler(char *file, int line)
|
||||
{
|
||||
|
||||
@ -185,3 +176,4 @@ void _Error_Handler(char *file, int line)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
21
targets/stm32l442/src/redirect.c
Normal file
21
targets/stm32l442/src/redirect.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include "stm32l4xx_ll_usart.h"
|
||||
|
||||
#include "app.h"
|
||||
|
||||
void _putchar(char c)
|
||||
{
|
||||
while (! LL_USART_IsActiveFlag_TXE(DEBUG_UART))
|
||||
;
|
||||
LL_USART_TransmitData8(DEBUG_UART,c);
|
||||
}
|
||||
|
||||
int _write (int fd, const void *buf, long int len)
|
||||
{
|
||||
uint8_t * data = (uint8_t *) buf;
|
||||
while(len--)
|
||||
{
|
||||
_putchar(*data++);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user