凌云物联网实验室ISKBoard(IoT Starter Kits Board)开发板项目源码
guowenxue
2023-04-07 6ddecc5e53def103eec427a61f2de308552735f5
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "usart.h"
#include "spi.h"
#include "tim.h"
#include "gpio.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "miscdev.h"
#include "devsn.h"
#include "ds18b20.h"
#include "sht20.h"
#include "isl1208.h"
#include "ws2812b.h"
#include "w25q32.h"
#include "comport.h"
#include "hal_oled.h"
#include "esp32.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define POS_X       5
#define POS_Y       1
#define SHOWTIME    1000
 
static inline void oled_show(char *msg)
{
    OLED_Init();
    OLED_ShowString(POS_X, POS_Y, msg, OLED_FONT16);
    HAL_Delay(SHOWTIME);
}
 
int check_devsn(void);
int check_flash(void);
int check_wifi(void);
 
int show_rtc(void);
int show_adc(void);
int show_ds18b20(void);
int show_sht20(void);
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  rtc_time_t  tm = { .tm_year=2023, .tm_mon=4, .tm_mday=8, .tm_hour=8, .tm_min=8, .tm_sec=8 };
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_USART3_UART_Init();
  MX_USART1_UART_Init();
  MX_LPUART1_UART_Init();
  MX_ADC1_Init();
  MX_TIM6_Init();
  MX_TIM1_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */
 
  printf("Welcome to ISKBoard v1.0, compiled on %s %s\r\n", __DATE__, __TIME__);
  beep_start(2, 200);
 
  /* Check device S/N, it will hang when failed */
  check_devsn();
 
  OLED_Init();
  ws2812b_init();
  set_rtc_time(tm);
 
  check_flash();
  check_wifi();
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      ws2812b_blink();
 
      show_rtc();
      show_adc();
      show_ds18b20();
      show_sht20();
 
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/* USER CODE BEGIN 4 */
 
void system_hanges(void)
{
    while(1)
    {
        blink_led(LedRed, 500);
        //beep_start(1, 300);
    }
}
 
int check_devsn(void)
{
    comport_t       *comport = NULL;
    char            *ptr;
    char             buf[32] = {0};
    char             devsn[DEVSN_LEN+1];
    char             oledstr[32] = {0};
    int              bytes;
 
    if( !read_devsn(devsn, sizeof(devsn)) )
    {
        printf("Read Serial Number okay\r\n");
        goto SHOW_SN;
    }
 
    /* AT+SN=ISK102300x */
    printf("ISKBoard S/N not written, please send SN by AT command:\r\n");
    memset(buf, 0, sizeof(buf));
 
    comport = comport_open(&huart1);
    if( !comport )
    {
        printf("ERROR: Console serial port open failed!\r\n");
        goto ERROR;
    }
 
    while( 1 )
    {
        bytes = comport_recv(comport, buf, sizeof(buf), 10000);
        if(bytes > DEVSN_LEN)
            break;
    }
 
    if( strncmp(buf, "AT+SN=ISK", 9) )
    {
        printf("ERROR: Input SN with wrong AT command.\r\n");
        goto ERROR;
    }
 
    ptr = strstr(buf, "ISK");
    if( !ptr || strlen(ptr)!=DEVSN_LEN )
    {
        printf("ERROR: Input invalid Serial Number!\r\n");
        goto ERROR;
    }
 
    if( write_devsn(ptr, DEVSN_LEN) < 0 )
    {
        printf("ERROR: Write Serial Number failed\r\n");
        goto ERROR;
    }
 
    if( read_devsn(devsn, sizeof(devsn)) )
    {
        printf("ERROR: Read Serial Number after write failed\r\n");
        goto ERROR;
    }
 
SHOW_SN:
    printf("ISKBoard S/N:%s\r\n", devsn);
    snprintf(oledstr, sizeof(oledstr), "S/N: %s", devsn);
    oled_show(oledstr);
    comport_term(comport);
    return 0;
 
ERROR:
    system_hanges();
    return -2;
}
 
int check_flash(void)
{
    int              rv;
    char             oledstr[32] = {0};
 
    rv = w25q_Init();
    printf("SPI Flash W25Q32 detected\r\n");
 
    snprintf(oledstr, sizeof(oledstr), "W25Q: %s", rv?"[FAIL]":"[OK]");
    oled_show(oledstr);
 
    return 0;
}
 
int check_wifi(void)
{
    comport_t         *comport = NULL;
    int                rv = 0;
    char               oledstr[32] = {0};
    char               version[256] = {0};
 
    comport = comport_open(&huart2);
    if( !comport )
    {
        rv = -2;
        goto cleanup;
    }
 
    esp32_hwreset(comport);
    esp32_set_echo(comport, DISABLE);
 
    rv = esp32_get_firmware(comport, version, sizeof(version));
    if( rv < 0)
    {
        printf("Check ESP32 WiFi module failed, rv=%d\r\n", rv);
        goto cleanup;
    }
    printf("ESP32 firmware version:\r\n%s\r\n", version);
 
cleanup:
    snprintf(oledstr, sizeof(oledstr), "WiFi: %s", rv?"[FAIL]":"[OK]");
    oled_show(oledstr);
 
    return 0;
}
 
int show_rtc(void)
{
    char             oledstr[32] = {0};
    rtc_time_t       tm;
    int              rv = 0;
 
    if( (rv=get_rtc_time(&tm)) < 0 )
    {
        printf("RTC get date & time failed, rv=%d\r\n", rv);
          oled_show("RTC: [FAIL]");
        return -1;
    }
 
    printf("RTC: %04d-%02d-%02d %02d:%02d:%02d %s\r\n", tm.tm_year, tm.tm_mon, tm.tm_mday,
        tm.tm_hour, tm.tm_min, tm.tm_sec, weekday[tm.tm_wday]);
 
    snprintf(oledstr, sizeof(oledstr), "RTC: %04d-%02d-%02d", tm.tm_year, tm.tm_mon, tm.tm_mday);
      oled_show(oledstr);
 
    snprintf(oledstr, sizeof(oledstr), "RTC: %02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec);
      oled_show(oledstr);
 
    return 0;
}
 
int show_adc(void)
{
    char             oledstr[32] = {0};
    uint32_t         lux, noisy;
 
      adc_sample_lux_noisy(&lux, &noisy);
      printf("Lux: %lu Noisy: %lu\r\n", lux, noisy);
 
      snprintf(oledstr, sizeof(oledstr), "lux:%lu db:%lu", lux, noisy);
      oled_show(oledstr);
 
      return 0;
}
 
int show_ds18b20(void)
{
    int              rv = 0;
    float            temperature_f = 0.0;
    char             oledstr[32] = {0};
 
    rv = ds18b20_sample(&temperature_f);
    if( rv < 0 )
    {
        printf("DS18B20 sample temperature failed, rv=%d\r\n", rv);
          oled_show("DS18B20: [FAIL]");
        return -1;
    }
 
    printf("DS18B20 sample temperature: %.3f\'C\r\n", temperature_f);
      snprintf(oledstr, sizeof(oledstr), "DS18B20: %.3f", temperature_f);
      oled_show(oledstr);
 
      return 0;
}
 
int show_sht20(void)
{
    uint32_t         TrH, temperature, humdity;
    char             oledstr[32] = {0};
 
    TrH = sht20_sample_TrH(&temperature, &humdity);
    if( TRH_FAIL_VAL == TrH)
    {
        printf("SHT20 sample temperature & humidity failed!\r\n");
          oled_show("DS18B20: [FAIL]");
        return -1;
    }
 
    printf("SHT20 sample temperature: %lu.%02lu\'C humidity: %lu.%02lu%%\r\n",
            temperature/100, temperature%100, humdity/100, humdity%100);
 
    snprintf(oledstr, sizeof(oledstr), "SHT20: %lu.%02lu\'C", temperature/100, temperature%100);
      oled_show(oledstr);
 
    snprintf(oledstr, sizeof(oledstr), "SHT20: %lu.%02lu%%", humdity/100, humdity%100);
      oled_show(oledstr);
 
      return 0;
}
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */