Enabled SysTick interrupt, fixed LED toggle bug

This commit is contained in:
dooku 2021-06-09 12:10:51 +02:00
parent 7e48b82c9d
commit a3bfe95c57
5 changed files with 24 additions and 5 deletions

View File

@ -46,6 +46,22 @@ void led_set_color(uint8_t red, uint8_t green, uint8_t blue)
led_blue_intensity = (int)blue * led_pwm_max / 255;
}
void led_gpio_off(GPIO_TypeDef *port, uint32_t pin_mask)
{
uint32_t port_value;
port_value = LL_GPIO_ReadInputPort(port);
port_value &= ~pin_mask;
LL_GPIO_WriteOutputPort(port, port_value);
}
void led_gpio_on(GPIO_TypeDef *port, uint32_t pin_mask)
{
uint32_t port_value;
port_value = LL_GPIO_ReadInputPort(port);
port_value |= pin_mask;
LL_GPIO_WriteOutputPort(port, port_value);
}
/*
* led_off():
* Set LED intensity to 0 for each color
@ -123,4 +139,8 @@ void led_init(led_context_t *context, int pwm_freq, int pwm_handler_freq)
// 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;
// turn off all LEDs (inverted)
led_gpio_on(led_context->red_led_port, led_context->red_led_pin);
led_gpio_on(led_context->green_led_port, led_context->green_led_pin);
led_gpio_on(led_context->blue_led_port, led_context->blue_led_pin);
}

View File

@ -107,7 +107,7 @@ int main(void)
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(80, 0, 0);
led_set_color(0, 20, 0);
/* I2C context init (for SHT4x and SCD4x) */
i2c_context_t i2c_context;
i2c_context.i2c = I2C1;
@ -120,7 +120,6 @@ int main(void)
sht4x_measure(&T, &RH);
while (1)
{
led_pwm_handler(); // TODO temporary, should be placed in timer interrupt (SysTick)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}

View File

@ -28,8 +28,7 @@ int sht4x_measure(int *temperature, int *relative_humidity)
if (result != I2C_OK) {
return SHT4X_ERROR;
}
// busy delay (LL_mDelay uses SysTick which doesn't work at the moment) TODO
for (volatile int j = 0; j < 100000; j++);
LL_mDelay(100); // 10 ms should be enough
// read out
result = i2c_receive(SHT4X_I2C_ADDRESS, buffer, 6);
if (result != I2C_OK) {

View File

@ -131,7 +131,7 @@ void SysTick_Handler(void)
/* USER CODE END SysTick_IRQn 0 */
/* USER CODE BEGIN SysTick_IRQn 1 */
// led_pwm_handler();
led_pwm_handler();
/* USER CODE END SysTick_IRQn 1 */
}

View File

@ -223,6 +223,7 @@ __STATIC_INLINE void LL_InitTick(uint32_t HCLKFrequency, uint32_t Ticks)
SysTick->LOAD = (uint32_t)((HCLKFrequency / Ticks) - 1UL); /* set reload register */
SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */
}