/*
|
* miscdev.c
|
*
|
* Created on: Jan 1, 2023
|
* Author: Think
|
*/
|
#include <miscdev.h>
|
#include "stm32l4xx_hal.h"
|
#include "main.h"
|
#include "adc.h"
|
#include "tim.h"
|
#include "miscdev.h"
|
|
|
/*
|
*+----------------------+
|
*| GPIO Led API |
|
*+----------------------+
|
*/
|
|
gpio_t leds[LedMax] =
|
{
|
{ "RedLed", LedRed_GPIO_Port, LedRed_Pin},
|
{ "GreenLed", LedGreen_GPIO_Port, LedGreen_Pin},
|
{ "BlueLed", LedBlue_GPIO_Port, LedBlue_Pin},
|
};
|
|
void turn_led(lednum_t which, status_t status)
|
{
|
GPIO_PinState level;
|
|
if( which >= LedMax )
|
return ;
|
|
level = status==OFF ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
|
HAL_GPIO_WritePin(leds[which].group, leds[which].pin, level);
|
}
|
|
void blink_led(lednum_t which, uint32_t interval)
|
{
|
turn_led(which, ON);
|
HAL_Delay(interval);
|
|
turn_led(which, OFF);
|
HAL_Delay(interval);
|
}
|
|
/*
|
*+----------------------+
|
*| GPIO Relay API |
|
*+----------------------+
|
*/
|
|
void turn_relay(status_t status)
|
{
|
GPIO_PinState level;
|
|
level = status==OFF ? GPIO_PIN_RESET : GPIO_PIN_SET;
|
HAL_GPIO_WritePin(Relay_GPIO_Port, Relay_Pin, level);
|
}
|
|
|
/*
|
*+----------------------+
|
*| GPIO Key API |
|
*+----------------------+
|
*/
|
|
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
{
|
static unsigned char status[3];
|
|
if( Key1_Pin == GPIO_Pin )
|
{
|
status[0] ^= 1;
|
turn_led(LedRed, status[0]);
|
}
|
else if( Key2_Pin == GPIO_Pin )
|
{
|
status[1] ^= 1;
|
turn_led(LedGreen, status[1]);
|
}
|
if( Key3_Pin == GPIO_Pin )
|
{
|
status[2] ^= 1;
|
turn_led(LedBlue, status[2]);
|
}
|
}
|
|
|
/*
|
*+----------------------------+
|
*| ADC noisy & lux sensor API |
|
*+----------------------------+
|
*/
|
|
int adc_sample_lux_noisy(uint32_t *lux, uint32_t *noisy)
|
{
|
uint8_t i;
|
uint32_t timeout = 0xffffff;
|
|
for(i=0; i<ADCCHN_MAX; i++)
|
{
|
HAL_ADC_Start(&hadc1);
|
|
HAL_ADC_PollForConversion(&hadc1, timeout);
|
|
if( ADCCHN_NOISY == i )
|
{
|
*noisy = HAL_ADC_GetValue(&hadc1);
|
}
|
else if( ADCCHN_LUX == i )
|
{
|
*lux = HAL_ADC_GetValue(&hadc1);
|
}
|
|
HAL_Delay(10);
|
}
|
|
HAL_ADC_Stop(&hadc1);
|
|
return 0;
|
}
|
|
|
/*
|
*+----------------------------+
|
* Timer Buzzer/delay API |
|
*+----------------------------+
|
*/
|
|
void beep_start(uint16_t times, uint16_t interval)
|
{
|
while( times-- )
|
{
|
/* Start buzzer */
|
if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4) != HAL_OK)
|
{
|
/* Starting Error */
|
Error_Handler();
|
}
|
|
HAL_Delay(interval);
|
|
/* Stop buzzer */
|
if (HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_4) != HAL_OK)
|
{
|
/* Starting Error */
|
Error_Handler();
|
}
|
|
HAL_Delay(interval);
|
}
|
}
|
|
/* Max to 60000 us*/
|
void delay_us(uint16_t us)
|
{
|
uint16_t differ = 60000-us;
|
|
HAL_TIM_Base_Start(&htim6);
|
|
__HAL_TIM_SET_COUNTER(&htim6, differ);
|
|
while( differ < 60000 )
|
{
|
differ=__HAL_TIM_GET_COUNTER(&htim6);
|
}
|
|
HAL_TIM_Base_Stop(&htim6);
|
}
|