Initial commit
This commit is contained in:
224
RTE/Hesso_pack/ext_led.c
Normal file
224
RTE/Hesso_pack/ext_led.c
Normal file
@ -0,0 +1,224 @@
|
||||
|
||||
#include "stm32f7xx_hal.h"
|
||||
#include "ext_led.h"
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static const uint8_t cie1931[101] =
|
||||
{
|
||||
0, 0, 1, 1, 1, 1, 2, 2, 2, 3,
|
||||
3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
|
||||
8, 8, 9, 10, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 22, 23, 24, 26, 27,
|
||||
29, 30, 32, 34, 35, 37, 39, 41, 43, 45,
|
||||
47, 49, 51, 54, 56, 58, 61, 64, 66, 69,
|
||||
72, 75, 78, 81, 84, 87, 90, 93, 97, 100,
|
||||
104, 108, 111, 115, 119, 123, 127, 131, 136, 140,
|
||||
145, 149, 154, 159, 163, 168, 173, 179, 184, 189,
|
||||
195, 200, 206, 212, 217, 223, 230, 236, 242, 248,
|
||||
255,
|
||||
};
|
||||
|
||||
#if LIGHTNESS_PWM_STEP != 100
|
||||
#error this cie1931 array supports only 100 steps, feel free to implement your own
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t lightness_to_pwm(uint8_t percentage)
|
||||
{
|
||||
if(percentage > (LIGHTNESS_PWM_STEP-1))
|
||||
percentage = (LIGHTNESS_PWM_STEP-1);
|
||||
|
||||
return cie1931[percentage];
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t Ext_LED_Init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
//----------------------------------------------------------------------------
|
||||
// Configure GPIO pin: PA15 (LED0)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE(); // enable GPIO timer
|
||||
__HAL_RCC_TIM2_CLK_ENABLE(); // enable timer 2 clock
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_15; // used pin is PA15
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;// timer 2 is used
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
TIM2->CCER = TIM_CCER_CC1E; // compare for PWM usage
|
||||
TIM2->PSC = 16; // timer prescaler
|
||||
TIM2->ARR = 255; // max count value
|
||||
TIM2->CCR1 = lightness_to_pwm(0); // duty cycle
|
||||
TIM2->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
TIM2->EGR |= TIM_EGR_UG; // update register now
|
||||
TIM2->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
|
||||
//----------------------------------------------------------------------------
|
||||
// Configure GPIO pin: PH6 (LED1)
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE(); // enable GPIO timer
|
||||
__HAL_RCC_TIM12_CLK_ENABLE(); // enable timer 12 clock
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6; // used pin is PH6
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
|
||||
GPIO_InitStruct.Alternate = GPIO_AF9_TIM12;// timer 12 is used
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
|
||||
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
|
||||
|
||||
TIM12->CCER = TIM_CCER_CC1E; // compare for PWM usage
|
||||
TIM12->PSC = 16; // timer prescaler
|
||||
TIM12->ARR = 255; // max count value
|
||||
TIM12->CCR1 = lightness_to_pwm(0); // duty cycle
|
||||
TIM12->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
TIM12->EGR |= TIM_EGR_UG; // update register now
|
||||
TIM12->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
|
||||
//----------------------------------------------------------------------------
|
||||
// Configure GPIO pin: PA8 (LED2)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE(); // enable GPIO timer
|
||||
__HAL_RCC_TIM1_CLK_ENABLE(); // enable timer 1 clock
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8; // used pin is PA8
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;// timer 5 is used
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
TIM1->CCER = TIM_CCER_CC1E; // compare for PWM usage
|
||||
TIM1->PSC = 16; // timer prescaler
|
||||
TIM1->ARR = 255; // max count value
|
||||
TIM1->CCR1 = lightness_to_pwm(0); // duty cycle
|
||||
TIM1->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
TIM1->EGR |= TIM_EGR_UG; // update register now
|
||||
TIM1->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
|
||||
TIM1->BDTR = TIM_BDTR_MOE; // master output enable
|
||||
//----------------------------------------------------------------------------
|
||||
// Configure GPIO pin: PB4 (LED3)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE(); // enable GPIO timer
|
||||
__HAL_RCC_TIM3_CLK_ENABLE(); // enable timer 3 clock
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_4; // used pin is PB4
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // alternate function use
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL; // no pullup
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;// timer 3 is used
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FAST; // speed is fast
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
TIM3->CCER = TIM_CCER_CC1E; // compare for PWM usage
|
||||
TIM3->PSC = 16; // timer prescaler
|
||||
TIM3->ARR = 255; // max count value
|
||||
TIM3->CCR1 = lightness_to_pwm(0); // duty cycle
|
||||
TIM3->CCMR1 = TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1PE;
|
||||
TIM3->EGR |= TIM_EGR_UG; // update register now
|
||||
TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN; // start the timer
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t Ext_LED_On (uint32_t num) {
|
||||
|
||||
if((num & 1) != 0)
|
||||
{
|
||||
TIM2->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
if((num & 2) != 0)
|
||||
{
|
||||
TIM12->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
if((num & 4) != 0)
|
||||
{
|
||||
TIM1->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
if((num & 8) != 0)
|
||||
{
|
||||
TIM3->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t Ext_LED_Off (uint32_t num) {
|
||||
|
||||
if((num & 1) != 0)
|
||||
{
|
||||
TIM2->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 2) != 0)
|
||||
{
|
||||
TIM12->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 4) != 0)
|
||||
{
|
||||
TIM1->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 8) != 0)
|
||||
{
|
||||
TIM3->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t Ext_LED_PWM (uint32_t num, uint32_t duty) {
|
||||
|
||||
if((num & 1) != 0)
|
||||
{
|
||||
TIM2->CCR1 = lightness_to_pwm(duty);
|
||||
}
|
||||
if((num & 2) != 0)
|
||||
{
|
||||
TIM12->CCR1 = lightness_to_pwm(duty);
|
||||
}
|
||||
if((num & 4) != 0)
|
||||
{
|
||||
TIM1->CCR1 = lightness_to_pwm(duty);
|
||||
}
|
||||
if((num & 8) != 0)
|
||||
{
|
||||
TIM3->CCR1 = lightness_to_pwm(duty);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t Ext_LEDs(uint32_t num) {
|
||||
|
||||
if((num & 1) != 0)
|
||||
{
|
||||
TIM2->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
else
|
||||
{
|
||||
TIM2->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 2) != 0)
|
||||
{
|
||||
TIM12->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
else
|
||||
{
|
||||
TIM12->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 4) != 0)
|
||||
{
|
||||
TIM1->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
else
|
||||
{
|
||||
TIM1->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
if((num & 8) != 0)
|
||||
{
|
||||
TIM3->CCR1 = lightness_to_pwm(LIGHTNESS_PWM_STEP);
|
||||
}
|
||||
else
|
||||
{
|
||||
TIM3->CCR1 = lightness_to_pwm(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
51
RTE/Hesso_pack/ext_led.h
Normal file
51
RTE/Hesso_pack/ext_led.h
Normal file
@ -0,0 +1,51 @@
|
||||
/************************************************************************//**
|
||||
* \file ext_led.h
|
||||
* \brief Function to use the extension LEDs
|
||||
* \author pascal (dot) sartoretti (at) hevs (dot) ch
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef __EXT_LED_H
|
||||
#define __EXT_LED_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LIGHTNESS_PWM_STEP 100
|
||||
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Inits the external Leds usage.
|
||||
* \return Always #0
|
||||
***************************************************************************/
|
||||
extern int32_t Ext_LED_Init (void);
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Turn on one led.
|
||||
* \param num The led to turn on (1,2,4,8)
|
||||
* \return Always 0
|
||||
***************************************************************************/
|
||||
extern int32_t Ext_LED_On (uint32_t num);
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Turn off one led.
|
||||
* \param num The led to turn off (1,2,4,8)
|
||||
* \return Always 0
|
||||
***************************************************************************/
|
||||
extern int32_t Ext_LED_Off (uint32_t num);
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Set a power on a led.
|
||||
* \param num The led to turn set the power (1,2,4,8)
|
||||
* \param duty The power of the led (0 to 255)
|
||||
* \return Always 0
|
||||
***************************************************************************/
|
||||
extern int32_t Ext_LED_PWM (uint32_t num, uint32_t duty);
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Set the state on all leds.
|
||||
* \param val The binary state of the four leds (example 0b1101).
|
||||
* \return Always 0
|
||||
***************************************************************************/
|
||||
extern int32_t Ext_LEDs(uint32_t val);
|
||||
|
||||
#endif /* __BOARD_LED_H */
|
57
RTE/Hesso_pack/ext_uart.c
Normal file
57
RTE/Hesso_pack/ext_uart.c
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
#include "stm32f7xx_hal.h"
|
||||
#include "ext_uart.h"
|
||||
|
||||
UART_HandleTypeDef ext_uart; // extension uart handler
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
if(huart->Instance==USART6)
|
||||
{
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
__HAL_RCC_USART6_CLK_ENABLE();
|
||||
|
||||
/**USART6 GPIO Configuration
|
||||
PC7 ------> USART6_RX
|
||||
PC6 ------> USART6_TX
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
void Ext_UART_Init(uint32_t speed)
|
||||
{
|
||||
ext_uart.Instance = USART6;
|
||||
ext_uart.Init.BaudRate = speed;
|
||||
ext_uart.Init.WordLength = UART_WORDLENGTH_8B;
|
||||
ext_uart.Init.StopBits = UART_STOPBITS_1;
|
||||
ext_uart.Init.Parity = UART_PARITY_NONE;
|
||||
ext_uart.Init.Mode = UART_MODE_TX_RX;
|
||||
ext_uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
||||
ext_uart.Init.OverSampling = UART_OVERSAMPLING_16;
|
||||
ext_uart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
||||
ext_uart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
||||
HAL_UART_Init(&ext_uart);
|
||||
/* USART6 interrupt Init */
|
||||
HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(USART6_IRQn);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
void USART6_IRQHandler(void)
|
||||
{
|
||||
HAL_UART_IRQHandler(&ext_uart);
|
||||
}
|
||||
|
||||
|
44
RTE/Hesso_pack/ext_uart.h
Normal file
44
RTE/Hesso_pack/ext_uart.h
Normal file
@ -0,0 +1,44 @@
|
||||
/************************************************************************//**
|
||||
* \file ext_uart.h
|
||||
* \brief Function to use the extension uart
|
||||
* \author pascal (dot) sartoretti (at) hevs (dot) ch
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef __EXT_UART_H
|
||||
#define __EXT_UART_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "stm32f7xx_hal.h"
|
||||
|
||||
extern UART_HandleTypeDef ext_uart; // extension uart handle
|
||||
|
||||
/************************************************************************//**
|
||||
* \brief Inits the extension uart
|
||||
* \param speed This si the uart speed selected for example 115200.
|
||||
* The extension uart could be use with or without interrupts.
|
||||
*
|
||||
* Without interrupts:
|
||||
* -------------------
|
||||
* To send something on the uart, you have to use HAL_UART_Transmit function
|
||||
* as the example below.
|
||||
* error = HAL_UART_Transmit(&ext_uart, msg, sizeof(msg),50);
|
||||
* To receive you have to use HAL_UART_Receive as example below.
|
||||
* error = HAL_UART_Receive(&ext_uart, msg, sizeof(msg),HAL_MAX_DELAY);
|
||||
* The HAL_MAX_DELAY waits until receive is finished.
|
||||
*
|
||||
* With interrupts:
|
||||
* ----------------
|
||||
* The functions below have to be used:
|
||||
* HAL_UART_Transmit_IT(&ext_uart," Welcome\n\r", 10);
|
||||
* HAL_UART_Receive_IT(&ext_uart,data,8);
|
||||
*
|
||||
* The callback functions above could be implemented for usage on interrupt
|
||||
* mode when the full size is transmitted (or received).
|
||||
* void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||
* void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
*
|
||||
***************************************************************************/
|
||||
extern void Ext_UART_Init(uint32_t speed);
|
||||
|
||||
#endif /* __BOARD_LED_H */
|
Reference in New Issue
Block a user