main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. typedef enum {
  28. Tube_A = 3,
  29. Tube_B = 2,
  30. Tube_D = 1,
  31. Tube_E = 0
  32. } tube_pos_t;
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. #define SPI_BUFFER_SIZE 5
  37. /* Display timeout, sec */
  38. #define DISP_WDT_TIME 10
  39. /* USER CODE END PD */
  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */
  42. /* USER CODE END PM */
  43. /* Private variables ---------------------------------------------------------*/
  44. /* USER CODE BEGIN PV */
  45. volatile flag_t Flag = {0};
  46. /**
  47. * Nixi Tube cathodes map in Byte Array:
  48. * {E0 E9 E8 E7 E6 E5 E4 E3}
  49. * {E2 E1 D0 D9 D8 D7 D6 D5}
  50. * {D4 D3 D2 D1 B0 B9 B8 B7}
  51. * {B6 B5 B4 B3 B2 B1 A0 A9}
  52. * {A8 A7 A6 A5 A4 A3 A2 A1}
  53. *
  54. * Shift register bit map in Tube cathodes (from 0 to 1):
  55. * {5.7 5.6 5.5 5.4 5.3 5.2 5.1 5.0 4.7 4.6} VL5/E
  56. * {4.5 4.4 4.3 4.2 4.1 4.0 3.7 3.6 3.5 3.4} VL4/D
  57. * {3.3 3.2 3.1 3.0 2.7 2.6 2.5 2.4 2.3 2.2} VL2/B
  58. * {2.1 2.0 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1.0} VL1/A
  59. */
  60. static const uint16_t nixieCathodeMap[4][10] = {
  61. {0x8000, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000},
  62. {0x2000, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000},
  63. {0x0800, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400},
  64. {0x0200, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100}
  65. };
  66. //static const uint8_t nixieCathodeMask[4][2] = {{0x00, 0x3f}, {0xc0, 0x0f}, {0xf0, 0x03}, {0xc0, 0x00}};
  67. static uint8_t tubesBuffer[SPI_BUFFER_SIZE] = {0};
  68. static rtc_t Clock;
  69. static struct bme280_dev SensorDev;
  70. static struct bme280_data SensorData;
  71. static int8_t Humidity, Temperature;
  72. static nt16_t Pressure;
  73. static btn_t Button[BTN_NUM] = {
  74. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  75. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  76. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  77. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  78. };
  79. static volatile uint8_t dispWDT = 0;
  80. /* USER CODE END PV */
  81. /* Private function prototypes -----------------------------------------------*/
  82. /* USER CODE BEGIN PFP */
  83. static void showDigits(uint8_t * dig);
  84. static void sensor_Init(void);
  85. static void sensorStartMeasure(void);
  86. static void sensorGetData(void);
  87. static void btnProcess(void);
  88. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b);
  89. /* USER CODE END PFP */
  90. /* Private user code ---------------------------------------------------------*/
  91. /* USER CODE BEGIN 0 */
  92. /* USER CODE END 0 */
  93. /**
  94. * @brief The application entry point.
  95. * @retval int
  96. */
  97. int main(void)
  98. {
  99. /* Initialize onBoard Hardware */
  100. Board_Init();
  101. /* Initialize Scheduler */
  102. RTOS_Init();
  103. /* tdelay_ms() work now, I2C can work too */
  104. RTC_Init();
  105. RTC_ReadAll(&Clock);
  106. sensor_Init();
  107. /* Initialize Event State Machine */
  108. ES_Init(stShowTime);
  109. es_event_t event = eventNull;
  110. /* Enable tube power */
  111. TUBE_PWR_ON;
  112. /** Star SPI transfer to shift registers */
  113. /* Set DMA source and destination addresses. */
  114. /* Source: Address of the SPI buffer. */
  115. DMA1_Channel1->CMAR = (uint32_t)&tubesBuffer;
  116. /* Destination: SPI1 data register. */
  117. DMA1_Channel1->CPAR = (uint32_t)&(SPI1->DR);
  118. /* Set DMA data transfer length (SPI buffer length). */
  119. DMA1_Channel1->CNDTR = SPI_BUFFER_SIZE;
  120. /* Enable SPI transfer */
  121. SPI1->CR1 |= SPI_CR1_SPE;
  122. Flag.SPI_TX_End = 1;
  123. /* display work now */
  124. /** Set tasks for Sheduler */
  125. RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
  126. /* USER CODE BEGIN WHILE */
  127. Color_RGB(0xFF, 0x12, 0x0); // Nixie color. FF1200 or FF7E00 or FFBF00
  128. showTime();
  129. /* Infinite loop */
  130. while (1)
  131. {
  132. /* new second interrupt from RTC */
  133. if (Flag.RTC_IRQ != 0) {
  134. Flag.RTC_IRQ = 0;
  135. Blink_Start(); // !!! TODO
  136. RTC_ReadAll(&Clock);
  137. if (dispWDT != 0) {
  138. dispWDT --;
  139. if (dispWDT == 0) {
  140. ES_PlaceEvent(evDisplayWDT);
  141. }
  142. }
  143. } /* end of New second */
  144. /* USER CODE END WHILE */
  145. /* USER CODE BEGIN 3 */
  146. event = ES_GetEvent();
  147. if (event) {
  148. ES_Dispatch(event);
  149. }
  150. RTOS_DispatchTask();
  151. __WFI();
  152. }
  153. /* USER CODE END 3 */
  154. } /* End of mine() */
  155. /**
  156. * Sensor
  157. */
  158. static void sensor_Init(void) {
  159. int8_t rsltSensor;
  160. Flag.BME280 = 0;
  161. SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
  162. SensorDev.intf = BME280_I2C_INTF;
  163. SensorDev.read = user_i2c_read;
  164. SensorDev.write = user_i2c_write;
  165. SensorDev.delay_ms = tdelay_ms;
  166. rsltSensor = bme280_init(&SensorDev);
  167. if (rsltSensor == BME280_OK) {
  168. Flag.BME280 = 1;
  169. /* BME280 Recommended mode of operation: Indoor navigation */
  170. SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
  171. SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
  172. SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
  173. SensorDev.settings.filter = BME280_FILTER_COEFF_16;
  174. rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
  175. RTOS_SetTask(sensorStartMeasure, 103, 1000);
  176. RTOS_SetTask(sensorGetData, 603, 1000);
  177. }
  178. }
  179. static void sensorStartMeasure(void) {
  180. bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
  181. }
  182. static void sensorGetData(void) {
  183. bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
  184. int32_t tmp;
  185. tmp = SensorData.humidity + 512;
  186. Humidity = (int8_t)(tmp / 1024);
  187. tmp = SensorData.temperature + 50;
  188. Temperature = (int8_t)(tmp / 100);
  189. /* in 32-bit arithmetics pressure in Pa */
  190. tmp = SensorData.pressure * 1000;
  191. tmp += 66661;
  192. tmp /= 133322;
  193. /* pressure in mmHg */
  194. Pressure.s16.u8H = (uint8_t)(tmp / 100);
  195. Pressure.s16.u8L = (uint8_t)(tmp % 100);
  196. }
  197. /* USER CODE BEGIN 4 */
  198. /*************************
  199. * S U B R O U T I N E S *
  200. *************************/
  201. /**
  202. * @brief Out digits to SPI buffer. ON/off tube power.
  203. * @param : array with four BCD digits
  204. * @retval : None
  205. */
  206. static void showDigits(uint8_t * dig)
  207. {
  208. /* Clear buffer */
  209. tubesBuffer[0] = 0;
  210. tubesBuffer[1] = 0;
  211. tubesBuffer[2] = 0;
  212. tubesBuffer[3] = 0;
  213. tubesBuffer[4] = 0;
  214. /* check values range */
  215. int i;
  216. for (i=0; i<4; i++) {
  217. if (dig[i] > 9) {
  218. if (dig[i] != 0xf) {
  219. dig[i] = 0;
  220. }
  221. }
  222. }
  223. /* Wait for SPI */
  224. while (Flag.SPI_TX_End == 0) {};
  225. Flag.SPI_TX_End = 0;
  226. /* Feel buffer */
  227. tubesBuffer[0] = (uint8_t)(nixieCathodeMap[Tube_E][dig[Tube_E]] >> 8);
  228. tubesBuffer[1] = (uint8_t)((nixieCathodeMap[Tube_E][dig[Tube_E]]) | (nixieCathodeMap[Tube_D][dig[Tube_D]] >> 8));
  229. tubesBuffer[2] = (uint8_t)((nixieCathodeMap[Tube_D][dig[Tube_D]]) | (nixieCathodeMap[Tube_B][dig[Tube_B]] >> 8));
  230. tubesBuffer[3] = (uint8_t)((nixieCathodeMap[Tube_B][dig[Tube_B]]) | (nixieCathodeMap[Tube_A][dig[Tube_A]] >> 8));
  231. tubesBuffer[4] = (uint8_t)(nixieCathodeMap[Tube_A][dig[Tube_A]]);
  232. /* Start DMA transfer to SPI */
  233. DMA1_Channel1->CCR |= DMA_CCR_EN;
  234. /* On/Off tube power */
  235. if (dig[Tube_A] == 0xf) {
  236. TUBE_A_OFF;
  237. } else {
  238. TUBE_A_ON;
  239. }
  240. if (dig[Tube_B] == 0xf) {
  241. TUBE_B_OFF;
  242. } else {
  243. TUBE_B_ON;
  244. }
  245. if (dig[Tube_D] == 0xf) {
  246. TUBE_D_OFF;
  247. } else {
  248. TUBE_D_ON;
  249. }
  250. if (dig[Tube_E] == 0xf) {
  251. TUBE_E_OFF;
  252. } else {
  253. TUBE_E_ON;
  254. }
  255. }
  256. /**
  257. * @brief Вывод HEX значений цвета в таймер.
  258. * @param : RGB value in range 0x00-0xFF
  259. * @retval : None
  260. */
  261. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  262. /* Более быстрый вариант, на пробу. */
  263. COLOR_R(r * 4);
  264. COLOR_G(g * 4);
  265. COLOR_B(b * 4);
  266. /* Предварительный обсчёт в переменные сделан для того,
  267. что-бы вывести значения в таймер максимально одновременно. */
  268. /*
  269. uint32_t val_r, val_g, val_b;
  270. // * 999 + 127 / 255 ???
  271. val_r = ((uint32_t)(r * 1000) + 128) / 256;
  272. val_g = ((uint32_t)(g * 1000) + 128) / 256;
  273. val_b = ((uint32_t)(b * 1000) + 128) / 256;
  274. COLOR_R((uint16_t)val_r);
  275. COLOR_G((uint16_t)val_g);
  276. COLOR_B((uint16_t)val_b);
  277. */
  278. }
  279. /**
  280. * @brief Обработка кнопок.
  281. * @param : None
  282. * @retval : None
  283. */
  284. static void btnProcess(void) {
  285. /* get pin state */
  286. uint32_t pins = BTNS_STATE;
  287. int i;
  288. for (i=0; i<BTN_NUM; i++) {
  289. if ((pins & Button[i].pin) == 0) {
  290. /* button pressed */
  291. Button[i].time ++;
  292. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  293. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  294. if (Button[i].holded == Button[i].pressed) {
  295. /* if pressed and holded - same function, then button pressed auto repeat */
  296. ES_PlaceEvent(Button[i].pressed);
  297. }
  298. }
  299. } else if (Button[i].time != 0) {
  300. /* button released */
  301. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  302. /* process long press */
  303. ES_PlaceEvent(Button[i].holded);
  304. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  305. /* process short press */
  306. ES_PlaceEvent(Button[i].pressed);
  307. }
  308. Button[i].time = 0;
  309. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  310. }
  311. } /* end FOR */
  312. }
  313. /**
  314. * On/off symbols on IN-15 tube.
  315. */
  316. void in15Off(void) {
  317. IN15_OFF;
  318. TUBE_C_OFF;
  319. }
  320. void in15Minus(void) {
  321. IN15_OFF;
  322. IN15_Minus;
  323. TUBE_C_ON;
  324. }
  325. void in15Plus(void) {
  326. IN15_OFF;
  327. IN15_Plus;
  328. TUBE_C_ON;
  329. }
  330. void in15Percent(void) {
  331. IN15_OFF;
  332. IN15_Percent;
  333. TUBE_C_ON;
  334. }
  335. void in15P(void) {
  336. IN15_OFF;
  337. IN15_P;
  338. TUBE_C_ON;
  339. }
  340. void showTime(void) {
  341. in15Minus();
  342. RTOS_SetTask(in15Off, 500, 0);
  343. uint8_t buf[4];
  344. buf[Tube_A] = Clock.Hr >> 4;
  345. buf[Tube_B] = Clock.Hr & 0xf;
  346. buf[Tube_D] = Clock.Min >> 4;
  347. buf[Tube_E] = Clock.Min & 0xf;
  348. showDigits(buf);
  349. }
  350. /**
  351. * Show info on tubes.
  352. */
  353. void showWD(void) {
  354. dispWDT = DISP_WDT_TIME;
  355. IN15_OFF;
  356. uint8_t buf[4];
  357. buf[Tube_A] = 0xf;
  358. buf[Tube_B] = Clock.WD & 0xf;
  359. buf[Tube_D] = 0xf;
  360. buf[Tube_E] = 0xf;
  361. showDigits(buf);
  362. }
  363. void showDay(void) {
  364. dispWDT = DISP_WDT_TIME;
  365. IN15_OFF;
  366. uint8_t buf[4];
  367. buf[Tube_A] = Clock.Day >> 4;
  368. buf[Tube_B] = Clock.Day & 0xf;
  369. buf[Tube_D] = 0xf;
  370. buf[Tube_E] = 0xf;
  371. showDigits(buf);
  372. }
  373. void showMonth(void) {
  374. dispWDT = DISP_WDT_TIME;
  375. IN15_OFF;
  376. uint8_t buf[4];
  377. buf[Tube_A] = 0xf;
  378. buf[Tube_B] = 0xf;
  379. buf[Tube_D] = Clock.Mon >> 4;
  380. buf[Tube_E] = Clock.Mon & 0xf;
  381. showDigits(buf);
  382. }
  383. void showDayMon(void) {
  384. dispWDT = DISP_WDT_TIME;
  385. IN15_OFF;
  386. uint8_t buf[4];
  387. buf[Tube_A] = Clock.Day >> 4;
  388. buf[Tube_B] = Clock.Day & 0xf;
  389. buf[Tube_D] = Clock.Mon >> 4;
  390. buf[Tube_E] = Clock.Mon & 0xf;
  391. showDigits(buf);
  392. }
  393. void showYear(void) {
  394. dispWDT = DISP_WDT_TIME;
  395. IN15_OFF;
  396. uint8_t buf[4];
  397. buf[Tube_A] = 2;
  398. buf[Tube_B] = 0;
  399. buf[Tube_D] = Clock.Year >> 4;
  400. buf[Tube_E] = Clock.Year & 0xf;
  401. showDigits(buf);
  402. }
  403. void showHumidity(void) {
  404. dispWDT = DISP_WDT_TIME;
  405. in15Percent();
  406. uint8_t buf[4];
  407. buf[Tube_A] = Humidity / 10;
  408. buf[Tube_B] = Humidity % 10;
  409. buf[Tube_D] = 0xf;
  410. buf[Tube_E] = 0xf;
  411. showDigits(buf);
  412. }
  413. void showTemperature(void) {
  414. dispWDT = DISP_WDT_TIME;
  415. in15Plus();
  416. uint8_t buf[4];
  417. buf[Tube_A] = 0xf;
  418. buf[Tube_B] = 0xf;
  419. buf[Tube_D] = Temperature / 10;
  420. buf[Tube_E] = Temperature % 10;
  421. showDigits(buf);
  422. }
  423. void showPressure(void) {
  424. dispWDT = DISP_WDT_TIME;
  425. in15P();
  426. uint8_t buf[4];
  427. buf[Tube_A] = 0xf;
  428. buf[Tube_B] = Pressure.s16.u8H & 0xf;
  429. buf[Tube_D] = Pressure.s16.u8L >> 4;
  430. buf[Tube_E] = Pressure.s16.u8L & 0xf;
  431. showDigits(buf);
  432. }
  433. /* Simple function for cyclic show all sensor data */
  434. void showSensorData(void) {
  435. ES_SetState(stShowSensorData);
  436. showTemperature();
  437. tdelay_ms(3000);
  438. showHumidity();
  439. tdelay_ms(3000);
  440. showPressure();
  441. tdelay_ms(3000);
  442. ES_SetState(stShowTime);
  443. showTime();
  444. }
  445. /* USER CODE END 4 */
  446. /**
  447. * @brief This function is executed in case of error occurrence.
  448. * @retval None
  449. */
  450. void Error_Handler(void)
  451. {
  452. /* USER CODE BEGIN Error_Handler_Debug */
  453. /* User can add his own implementation to report the HAL error return state */
  454. __disable_irq();
  455. while (1)
  456. {
  457. }
  458. /* USER CODE END Error_Handler_Debug */
  459. }
  460. #ifdef USE_FULL_ASSERT
  461. /**
  462. * @brief Reports the name of the source file and the source line number
  463. * where the assert_param error has occurred.
  464. * @param file: pointer to the source file name
  465. * @param line: assert_param error line source number
  466. * @retval None
  467. */
  468. void assert_failed(uint8_t *file, uint32_t line)
  469. {
  470. /* USER CODE BEGIN 6 */
  471. /* User can add his own implementation to report the file name and line number,
  472. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  473. /* USER CODE END 6 */
  474. }
  475. #endif /* USE_FULL_ASSERT */
  476. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/