凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-03 32806da6f5647ac637fa7d48aa9c221b091ab35e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * 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);
}