deleted the led driver (it wad using HAL).
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* led.h
|
||||
*
|
||||
* Created on: Jun 6, 2021
|
||||
* Author: user
|
||||
*/
|
||||
|
||||
#ifndef INC_LED_H_
|
||||
#define INC_LED_H_
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stm32l0xx_hal.h"
|
||||
#include "stm32l0xx_ll_gpio.h"
|
||||
|
||||
/*
|
||||
* Context struct
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
GPIO_TypeDef *red_led_port;
|
||||
int red_led_pin;
|
||||
GPIO_TypeDef *green_led_port;
|
||||
int green_led_pin;
|
||||
GPIO_TypeDef *blue_led_port;
|
||||
int blue_led_pin;
|
||||
|
||||
} led_context_t;
|
||||
|
||||
/*
|
||||
* Externally defined variables
|
||||
*/
|
||||
|
||||
extern int led_pwm_max;
|
||||
extern int led_pwm_counter;
|
||||
extern int led_red_intensity;
|
||||
extern int led_green_intensity;
|
||||
extern int led_blue_intensity;
|
||||
|
||||
/*
|
||||
* Function prototypes
|
||||
*/
|
||||
|
||||
void led_set_color(uint8_t red, uint8_t green, uint8_t blue);
|
||||
void led_off();
|
||||
void led_pwm_handler();
|
||||
void led_init(led_context_t *context, int pwm_freq, int pwm_handler_freq);
|
||||
|
||||
#endif /* INC_LED_H_ */
|
||||
@@ -48,7 +48,6 @@ extern "C" {
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "led.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* led.c
|
||||
*
|
||||
* Created on: Jun 6, 2021
|
||||
* Author: user
|
||||
*/
|
||||
|
||||
#include "led.h"
|
||||
|
||||
/*
|
||||
* Global variables
|
||||
*/
|
||||
|
||||
int led_pwm_max;
|
||||
int led_pwm_counter;
|
||||
int led_red_intensity;
|
||||
int led_green_intensity;
|
||||
int led_blue_intensity;
|
||||
int led_red_state;
|
||||
int led_green_state;
|
||||
int led_blue_state;
|
||||
led_context_t *led_context = NULL;
|
||||
|
||||
/*
|
||||
* Functions
|
||||
*/
|
||||
|
||||
/*
|
||||
* led_set_color():
|
||||
* Set LED color
|
||||
* Arguments:
|
||||
* uint8_t red: red color intensity, possible values 0 ... 255
|
||||
* uint8_t green: green color intensity, possible values 0 ... 255
|
||||
* uint8_t blue: blue color intensity, possible values 0 ... 255
|
||||
* NOTE: if (color * led_pwm_max) < 255, LED will be off
|
||||
*/
|
||||
void led_set_color(uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
led_red_intensity = (int)red * led_pwm_max / 255;
|
||||
led_green_intensity = (int)green * led_pwm_max / 255;
|
||||
led_blue_intensity = (int)blue * led_pwm_max / 255;
|
||||
}
|
||||
|
||||
/*
|
||||
* led_off():
|
||||
* Set LED intensity to 0 for each color
|
||||
*/
|
||||
void led_off()
|
||||
{
|
||||
led_red_intensity = 0;
|
||||
led_green_intensity = 0;
|
||||
led_blue_intensity = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* led_pwm_handler():
|
||||
* handles switching LEDs on/off according to desired intensity;
|
||||
* should be regularly called in timer routine, preferably in SysTick_Handler()
|
||||
*/
|
||||
void led_pwm_handler()
|
||||
{
|
||||
int new_red_state, new_green_state, new_blue_state;
|
||||
|
||||
if (led_context == NULL) {
|
||||
// led_pwm_handler() may be called before led_init() was called;
|
||||
// this would result in a crash
|
||||
return;
|
||||
}
|
||||
|
||||
new_red_state = led_pwm_counter >= led_red_intensity ? 1 : 0;
|
||||
new_green_state = led_pwm_counter >= led_green_intensity ? 1 : 0;
|
||||
new_blue_state = led_pwm_counter >= led_blue_intensity ? 1 : 0;
|
||||
// SysTick() is called at 1 kHz frequency, we don't want to call HAL_GPIO_WritePin() every time
|
||||
if (led_red_state != new_red_state) {
|
||||
LL_GPIO_TogglePin(led_context->red_led_port, led_context->red_led_pin);
|
||||
led_red_state = new_red_state;
|
||||
}
|
||||
if (led_green_state != new_green_state) {
|
||||
LL_GPIO_TogglePin(led_context->green_led_port, led_context->green_led_pin);
|
||||
led_green_state = new_green_state;
|
||||
}
|
||||
if (led_blue_state != new_blue_state) {
|
||||
LL_GPIO_TogglePin(led_context->blue_led_port, led_context->blue_led_pin);
|
||||
led_blue_state = new_blue_state;
|
||||
}
|
||||
if (++led_pwm_counter >= led_pwm_max) {
|
||||
led_pwm_counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* led_init():
|
||||
* Saves context and calculates max pwm value. Note that is PWM frequency is low (< 25 Hz),
|
||||
* flickering might be visible. If pwm_handler_freq is low (less than several kHz),
|
||||
* resolution of PWM will be limited
|
||||
* Arguments:
|
||||
* led_context_t *context:
|
||||
* Pointer to LED context struct, which contains port and pin for each LED
|
||||
* int pwm_freq:
|
||||
* Desired frequency of PWM in Hz (e.g. 25 Hz)
|
||||
* int pwm_handler_freq:
|
||||
* Frequency of led_pwm_handler() calls. Eequal to timer frequency if timer callback is used,
|
||||
* e.g. if led_pwm_handler() is called within SysTick_Handler(), then frequency is
|
||||
* HAL_TICK_FREQ_1KHZ
|
||||
*/
|
||||
void led_init(led_context_t *context, int pwm_freq, int pwm_handler_freq)
|
||||
{
|
||||
// save context
|
||||
led_context = context;
|
||||
// Initial values
|
||||
led_red_intensity = 0;
|
||||
led_red_state = 1; // state is inverted (LEDs are sinking current into MCU)
|
||||
led_green_intensity = 0;
|
||||
led_green_state = 1;
|
||||
led_blue_intensity = 0;
|
||||
led_blue_state = 1;
|
||||
// calculate PWM counter overflow value (max value)
|
||||
// e.g. for 1 kHz handler freq and 25 Hz PWM freq, we only have
|
||||
// resolution of 40 steps for pwm
|
||||
led_pwm_max = pwm_handler_freq / pwm_freq;
|
||||
}
|
||||
@@ -99,22 +99,21 @@ int main(void)
|
||||
LL_I2C_Enable(I2C1);
|
||||
LL_LPUART_Enable(LPUART1);
|
||||
/* LED PWM context */
|
||||
led_context_t led_context;
|
||||
led_context.red_led_port = LED_R_GPIO_Port;
|
||||
led_context.green_led_port = LED_G_GPIO_Port;
|
||||
led_context.blue_led_port = LED_B_GPIO_Port;
|
||||
led_context.red_led_pin = LED_R_Pin;
|
||||
led_context.green_led_pin = LED_G_Pin;
|
||||
led_context.blue_led_pin = LED_B_Pin;
|
||||
led_init(&led_context, 50, 1000);
|
||||
led_set_color(0, 20, 0);
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
uint8_t uart_data_dummy = 0;
|
||||
while (1)
|
||||
{
|
||||
LL_LPUART_SetDESignalPolarity(LPUART1, 1);
|
||||
LL_LPUART_TransmitData8(LPUART1, uart_data_dummy);
|
||||
uart_data_dummy++;
|
||||
|
||||
LL_mDelay(1000);
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
@@ -242,17 +241,15 @@ static void MX_LPUART1_UART_Init(void)
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_LPUART1);
|
||||
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
/**LPUART1 GPIO Configuration
|
||||
PA0-CK_IN ------> LPUART1_RX
|
||||
PA1 ------> LPUART1_TX
|
||||
PB1 ------> LPUART1_DE
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
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.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
@@ -260,18 +257,10 @@ static void MX_LPUART1_UART_Init(void)
|
||||
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.Pull = LL_GPIO_PULL_UP;
|
||||
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
|
||||
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
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_4;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* LPUART1 DMA Init */
|
||||
|
||||
/* LPUART1_RX Init */
|
||||
@@ -301,10 +290,6 @@ static void MX_LPUART1_UART_Init(void)
|
||||
LPUART_InitStruct.TransferDirection = LL_LPUART_DIRECTION_TX_RX;
|
||||
LPUART_InitStruct.HardwareFlowControl = LL_LPUART_HWCONTROL_NONE;
|
||||
LL_LPUART_Init(LPUART1, &LPUART_InitStruct);
|
||||
LL_LPUART_EnableDEMode(LPUART1);
|
||||
LL_LPUART_SetDESignalPolarity(LPUART1, LL_LPUART_DE_POLARITY_HIGH);
|
||||
LL_LPUART_SetDEAssertionTime(LPUART1, 20);
|
||||
LL_LPUART_SetDEDeassertionTime(LPUART1, 20);
|
||||
/* USER CODE BEGIN LPUART1_Init 2 */
|
||||
|
||||
/* USER CODE END LPUART1_Init 2 */
|
||||
@@ -339,7 +324,6 @@ static void MX_GPIO_Init(void)
|
||||
|
||||
/* GPIO Ports Clock Enable */
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
|
||||
/**/
|
||||
LL_GPIO_SetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "stm32l0xx_it.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "led.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -131,7 +130,6 @@ void SysTick_Handler(void)
|
||||
/* USER CODE END SysTick_IRQn 0 */
|
||||
|
||||
/* USER CODE BEGIN SysTick_IRQn 1 */
|
||||
led_pwm_handler();
|
||||
/* USER CODE END SysTick_IRQn 1 */
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user