main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. static LL_RCC_ClocksTypeDef rcc_clocks;
  47. /**
  48. * Nixi Tube cathodes map in Byte Array:
  49. * {E0 E9 E8 E7 E6 E5 E4 E3}
  50. * {E2 E1 D0 D9 D8 D7 D6 D5}
  51. * {D4 D3 D2 D1 B0 B9 B8 B7}
  52. * {B6 B5 B4 B3 B2 B1 A0 A9}
  53. * {A8 A7 A6 A5 A4 A3 A2 A1}
  54. *
  55. * Shift register bit map in Tube cathodes (from 0 to 1):
  56. * {5.7 5.6 5.5 5.4 5.3 5.2 5.1 5.0 4.7 4.6} VL5/E
  57. * {4.5 4.4 4.3 4.2 4.1 4.0 3.7 3.6 3.5 3.4} VL4/D
  58. * {3.3 3.2 3.1 3.0 2.7 2.6 2.5 2.4 2.3 2.2} VL2/B
  59. * {2.1 2.0 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1.0} VL1/A
  60. */
  61. static const uint16_t nixieCathodeMap[4][10] = {
  62. {0x8000, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000},
  63. {0x2000, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000},
  64. {0x0800, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400},
  65. {0x0200, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100}
  66. };
  67. //static const uint8_t nixieCathodeMask[4][2] = {{0x00, 0x3f}, {0xc0, 0x0f}, {0xf0, 0x03}, {0xc0, 0x00}};
  68. static uint8_t tubesBuffer[SPI_BUFFER_SIZE] = {0};
  69. static rtc_t Clock;
  70. static struct bme280_dev SensorDev;
  71. static struct bme280_data SensorData;
  72. static int8_t Humidity, Temperature;
  73. static nt16_t Pressure;
  74. static btn_t Button[BTN_NUM] = {
  75. {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
  76. {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
  77. {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
  78. {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
  79. };
  80. static volatile uint8_t dispWDT = 0;
  81. /* USER CODE END PV */
  82. /* Private function prototypes -----------------------------------------------*/
  83. static void MX_GPIO_Init(void);
  84. static void MX_DMA_Init(void);
  85. static void MX_I2C1_Init(void);
  86. static void MX_SPI1_Init(void);
  87. /* USER CODE BEGIN PFP */
  88. static void showDigits(uint8_t * dig);
  89. static void sensor_Init(void);
  90. static void sensorStartMeasure(void);
  91. static void sensorGetData(void);
  92. static void btnProcess(void);
  93. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b);
  94. /* USER CODE END PFP */
  95. /* Private user code ---------------------------------------------------------*/
  96. /* USER CODE BEGIN 0 */
  97. /* USER CODE END 0 */
  98. /**
  99. * @brief The application entry point.
  100. * @retval int
  101. */
  102. int main(void)
  103. {
  104. /* USER CODE BEGIN 1 */
  105. /* USER CODE END 1 */
  106. /* MCU Configuration--------------------------------------------------------*/
  107. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  108. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
  109. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  110. /* System interrupt init*/
  111. /* Peripheral interrupt init*/
  112. /* RCC_IRQn interrupt configuration */
  113. NVIC_SetPriority(RCC_IRQn, 0);
  114. NVIC_EnableIRQ(RCC_IRQn);
  115. /* USER CODE BEGIN Init */
  116. /* USER CODE END Init */
  117. /* Configure the system clock */
  118. SystemClock_Config();
  119. /* USER CODE BEGIN SysInit */
  120. LL_LPM_EnableSleep();
  121. LL_LPM_DisableSleepOnExit();
  122. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  123. /* USER CODE END SysInit */
  124. /* Initialize all configured peripherals */
  125. MX_GPIO_Init();
  126. MX_DMA_Init();
  127. MX_I2C1_Init();
  128. MX_SPI1_Init();
  129. /* USER CODE BEGIN 2 */
  130. /* Initialize onBoard Hardware */
  131. Board_Init();
  132. /* Initialize Scheduler */
  133. RTOS_Init();
  134. /* Initialize Event State Machine */
  135. ES_Init(stShowTime);
  136. /* Enable tube power */
  137. TUBE_PWR_ON;
  138. RTC_Init();
  139. sensor_Init();
  140. /** Star SPI transfer to shift registers */
  141. /* Set DMA source and destination addresses. */
  142. /* Source: Address of the SPI buffer. */
  143. DMA1_Channel1->CMAR = (uint32_t)&tubesBuffer;
  144. /* Destination: SPI1 data register. */
  145. DMA1_Channel1->CPAR = (uint32_t)&(SPI1->DR);
  146. /* Set DMA data transfer length (SPI buffer length). */
  147. DMA1_Channel1->CNDTR = SPI_BUFFER_SIZE;
  148. /* Enable SPI+DMA transfer */
  149. SPI1->CR2 |= SPI_CR2_TXDMAEN;
  150. SPI1->CR1 |= SPI_CR1_SPE;
  151. Flag.SPI_TX_End = 1;
  152. /** Set tasks for Sheduler */
  153. RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
  154. /* USER CODE END 2 */
  155. /* USER CODE BEGIN WHILE */
  156. RTC_ReadAll(&Clock);
  157. es_event_t event = eventNull;
  158. Color_RGB(0xFF, 0x12, 0x0); // Nixie color. FF1200 or FF7E00 or FFBF00
  159. showTime();
  160. /* Infinite loop */
  161. while (1)
  162. {
  163. /* new second interrupt from RTC */
  164. if (Flag.RTC_IRQ != 0) {
  165. Flag.RTC_IRQ = 0;
  166. Blink_Start(); // !!! TODO
  167. RTC_ReadAll(&Clock);
  168. if (dispWDT != 0) {
  169. dispWDT --;
  170. if (dispWDT == 0) {
  171. ES_PlaceEvent(evDisplayWDT);
  172. }
  173. }
  174. } /* end of New second */
  175. /* USER CODE END WHILE */
  176. /* USER CODE BEGIN 3 */
  177. event = ES_GetEvent();
  178. if (event) {
  179. ES_Dispatch(event);
  180. }
  181. RTOS_DispatchTask();
  182. __WFI();
  183. }
  184. /* USER CODE END 3 */
  185. } /* End of mine() */
  186. /**
  187. * Sensor
  188. */
  189. static void sensor_Init(void) {
  190. int8_t rsltSensor;
  191. Flag.BME280 = 0;
  192. SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
  193. SensorDev.intf = BME280_I2C_INTF;
  194. SensorDev.read = user_i2c_read;
  195. SensorDev.write = user_i2c_write;
  196. SensorDev.delay_ms = tdelay_ms;
  197. rsltSensor = bme280_init(&SensorDev);
  198. if (rsltSensor == BME280_OK) {
  199. Flag.BME280 = 1;
  200. /* BME280 Recommended mode of operation: Indoor navigation */
  201. SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
  202. SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
  203. SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
  204. SensorDev.settings.filter = BME280_FILTER_COEFF_16;
  205. rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
  206. RTOS_SetTask(sensorStartMeasure, 103, 1000);
  207. RTOS_SetTask(sensorGetData, 603, 1000);
  208. }
  209. }
  210. static void sensorStartMeasure(void) {
  211. bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
  212. }
  213. static void sensorGetData(void) {
  214. bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
  215. int32_t tmp;
  216. tmp = SensorData.humidity + 512;
  217. Humidity = (int8_t)(tmp / 1024);
  218. tmp = SensorData.temperature + 50;
  219. Temperature = (int8_t)(tmp / 100);
  220. /* in 32-bit arithmetics pressure in Pa */
  221. tmp = SensorData.pressure * 1000;
  222. tmp += 66661;
  223. tmp /= 133322;
  224. /* pressure in mmHg */
  225. Pressure.s16.u8H = (uint8_t)(tmp / 100);
  226. Pressure.s16.u8L = (uint8_t)(tmp % 100);
  227. }
  228. /**
  229. * @brief I2C1 Initialization Function
  230. * @param None
  231. * @retval None
  232. */
  233. static void MX_I2C1_Init(void)
  234. {
  235. /* USER CODE BEGIN I2C1_Init 0 */
  236. /* USER CODE END I2C1_Init 0 */
  237. LL_I2C_InitTypeDef I2C_InitStruct = {0};
  238. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  239. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  240. /**I2C1 GPIO Configuration
  241. PB8 ------> I2C1_SCL
  242. PB9 ------> I2C1_SDA
  243. */
  244. GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
  245. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  246. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  247. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  248. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  249. GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
  250. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  251. GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
  252. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  253. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  254. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  255. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  256. GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
  257. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  258. /* Peripheral clock enable */
  259. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
  260. /* I2C1 DMA Init */
  261. /* I2C1_RX Init */
  262. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_2, LL_DMAMUX_REQ_I2C1_RX);
  263. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
  264. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_MEDIUM);
  265. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);
  266. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);
  267. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);
  268. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
  269. /* I2C1_TX Init */
  270. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_3, LL_DMAMUX_REQ_I2C1_TX);
  271. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_3, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
  272. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PRIORITY_MEDIUM);
  273. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PERIPH_NOINCREMENT);
  274. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MEMORY_INCREMENT);
  275. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PDATAALIGN_BYTE);
  276. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MDATAALIGN_BYTE);
  277. /* I2C1 interrupt Init */
  278. /* USER CODE BEGIN I2C1_Init 1 */
  279. /* Enable DMA transfer complete/error interrupts */
  280. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  281. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
  282. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
  283. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
  284. /* USER CODE END I2C1_Init 1 */
  285. /** I2C Initialization
  286. */
  287. I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
  288. I2C_InitStruct.Timing = 0x0010061A;
  289. I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  290. I2C_InitStruct.DigitalFilter = 0;
  291. I2C_InitStruct.OwnAddress1 = 0;
  292. I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
  293. I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  294. LL_I2C_EnableAutoEndMode(I2C1);
  295. LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
  296. LL_I2C_DisableOwnAddress2(I2C1);
  297. LL_I2C_DisableGeneralCall(I2C1);
  298. LL_I2C_DisableClockStretching(I2C1);
  299. LL_I2C_Init(I2C1, &I2C_InitStruct);
  300. /* USER CODE BEGIN I2C1_Init 2 */
  301. /* USER CODE END I2C1_Init 2 */
  302. }
  303. /**
  304. * @brief SPI1 Initialization Function
  305. * @param None
  306. * @retval None
  307. */
  308. static void MX_SPI1_Init(void)
  309. {
  310. /* USER CODE BEGIN SPI1_Init 0 */
  311. /* USER CODE END SPI1_Init 0 */
  312. LL_SPI_InitTypeDef SPI_InitStruct = {0};
  313. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  314. /* Peripheral clock enable */
  315. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  316. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  317. /**SPI1 GPIO Configuration
  318. PB3 ------> SPI1_SCK
  319. PB5 ------> SPI1_MOSI
  320. */
  321. GPIO_InitStruct.Pin = LL_GPIO_PIN_3;
  322. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  323. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  324. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  325. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  326. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  327. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  328. GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
  329. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  330. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  331. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  332. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  333. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  334. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  335. /* SPI1 DMA Init */
  336. /* SPI1_TX Init */
  337. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_SPI1_TX);
  338. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
  339. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
  340. LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
  341. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
  342. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
  343. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_BYTE);
  344. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_BYTE);
  345. /* SPI1 interrupt Init */
  346. NVIC_SetPriority(SPI1_IRQn, 0);
  347. NVIC_EnableIRQ(SPI1_IRQn);
  348. /* USER CODE BEGIN SPI1_Init 1 */
  349. /* Enable DMA transfer complete/error interrupts */
  350. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  351. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  352. /* USER CODE END SPI1_Init 1 */
  353. /* SPI1 parameter configuration*/
  354. SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  355. SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  356. SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  357. SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
  358. SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  359. SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  360. SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
  361. SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  362. SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  363. SPI_InitStruct.CRCPoly = 7;
  364. LL_SPI_Init(SPI1, &SPI_InitStruct);
  365. LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
  366. LL_SPI_DisableNSSPulseMgt(SPI1);
  367. /* USER CODE BEGIN SPI1_Init 2 */
  368. /* USER CODE END SPI1_Init 2 */
  369. }
  370. /**
  371. * Enable DMA controller clock
  372. */
  373. static void MX_DMA_Init(void)
  374. {
  375. /* Init with LL driver */
  376. /* DMA controller clock enable */
  377. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
  378. /* DMA interrupt init */
  379. /* DMA1_Channel1_IRQn interrupt configuration */
  380. NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
  381. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  382. /* DMA1_Channel2_3_IRQn interrupt configuration */
  383. NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0);
  384. NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
  385. }
  386. /**
  387. * @brief GPIO Initialization Function
  388. * @param None
  389. * @retval None
  390. */
  391. static void MX_GPIO_Init(void)
  392. {
  393. LL_EXTI_InitTypeDef EXTI_InitStruct = {0};
  394. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  395. /* GPIO Ports Clock Enable */
  396. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  397. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
  398. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  399. /**/
  400. LL_GPIO_ResetOutputPin(UART_EN_GPIO_Port, UART_EN_Pin);
  401. /**/
  402. LL_GPIO_ResetOutputPin(LC0_GPIO_Port, LC0_Pin);
  403. /**/
  404. LL_GPIO_ResetOutputPin(LC1_GPIO_Port, LC1_Pin);
  405. /**/
  406. LL_GPIO_ResetOutputPin(LC2_GPIO_Port, LC2_Pin);
  407. /**/
  408. LL_GPIO_ResetOutputPin(LC3_GPIO_Port, LC3_Pin);
  409. /**/
  410. LL_GPIO_ResetOutputPin(SHDN_GPIO_Port, SHDN_Pin);
  411. /**/
  412. LL_GPIO_ResetOutputPin(Latch_GPIO_Port, Latch_Pin);
  413. /**/
  414. LL_EXTI_SetEXTISource(LL_EXTI_CONFIG_PORTC, LL_EXTI_CONFIG_LINE14);
  415. /**/
  416. EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_14;
  417. EXTI_InitStruct.LineCommand = ENABLE;
  418. EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
  419. EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING;
  420. LL_EXTI_Init(&EXTI_InitStruct);
  421. /**/
  422. LL_GPIO_SetPinPull(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_PULL_UP);
  423. /**/
  424. LL_GPIO_SetPinMode(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_MODE_INPUT);
  425. /**/
  426. GPIO_InitStruct.Pin = UART_EN_Pin;
  427. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  428. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  429. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  430. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  431. LL_GPIO_Init(UART_EN_GPIO_Port, &GPIO_InitStruct);
  432. /**/
  433. GPIO_InitStruct.Pin = LC0_Pin;
  434. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  435. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  436. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  437. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  438. LL_GPIO_Init(LC0_GPIO_Port, &GPIO_InitStruct);
  439. /**/
  440. GPIO_InitStruct.Pin = LC1_Pin;
  441. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  442. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  443. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  444. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  445. LL_GPIO_Init(LC1_GPIO_Port, &GPIO_InitStruct);
  446. /**/
  447. GPIO_InitStruct.Pin = LC2_Pin;
  448. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  449. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  450. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  451. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  452. LL_GPIO_Init(LC2_GPIO_Port, &GPIO_InitStruct);
  453. /**/
  454. GPIO_InitStruct.Pin = LC3_Pin;
  455. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  456. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  457. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  458. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  459. LL_GPIO_Init(LC3_GPIO_Port, &GPIO_InitStruct);
  460. /**/
  461. GPIO_InitStruct.Pin = SHDN_Pin;
  462. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  463. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  464. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  465. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  466. LL_GPIO_Init(SHDN_GPIO_Port, &GPIO_InitStruct);
  467. /**/
  468. GPIO_InitStruct.Pin = BTN4_Pin;
  469. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  470. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  471. LL_GPIO_Init(BTN4_GPIO_Port, &GPIO_InitStruct);
  472. /**/
  473. GPIO_InitStruct.Pin = BTN1_Pin;
  474. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  475. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  476. LL_GPIO_Init(BTN1_GPIO_Port, &GPIO_InitStruct);
  477. /**/
  478. GPIO_InitStruct.Pin = Latch_Pin;
  479. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  480. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  481. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  482. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  483. LL_GPIO_Init(Latch_GPIO_Port, &GPIO_InitStruct);
  484. /**/
  485. GPIO_InitStruct.Pin = BTN2_Pin;
  486. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  487. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  488. LL_GPIO_Init(BTN2_GPIO_Port, &GPIO_InitStruct);
  489. /**/
  490. GPIO_InitStruct.Pin = UART_ST_Pin;
  491. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  492. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  493. LL_GPIO_Init(UART_ST_GPIO_Port, &GPIO_InitStruct);
  494. /**/
  495. GPIO_InitStruct.Pin = BTN3_Pin;
  496. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  497. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  498. LL_GPIO_Init(BTN3_GPIO_Port, &GPIO_InitStruct);
  499. /* EXTI interrupt init*/
  500. NVIC_SetPriority(EXTI4_15_IRQn, 0);
  501. NVIC_EnableIRQ(EXTI4_15_IRQn);
  502. }
  503. /* USER CODE BEGIN 4 */
  504. /*************************
  505. * S U B R O U T I N E S *
  506. *************************/
  507. /**
  508. * @brief Out digits to SPI buffer. ON/off tube power.
  509. * @param : array with four BCD digits
  510. * @retval : None
  511. */
  512. static void showDigits(uint8_t * dig)
  513. {
  514. /* Clear buffer */
  515. tubesBuffer[0] = 0;
  516. tubesBuffer[1] = 0;
  517. tubesBuffer[2] = 0;
  518. tubesBuffer[3] = 0;
  519. tubesBuffer[4] = 0;
  520. /* check values range */
  521. int i;
  522. for (i=0; i<4; i++) {
  523. if (dig[i] > 9) {
  524. if (dig[i] != 0xf) {
  525. dig[i] = 0;
  526. }
  527. }
  528. }
  529. /* Wait for SPI */
  530. while (Flag.SPI_TX_End == 0) {};
  531. Flag.SPI_TX_End = 0;
  532. /* Feel buffer */
  533. tubesBuffer[0] = (uint8_t)(nixieCathodeMap[Tube_E][dig[Tube_E]] >> 8);
  534. tubesBuffer[1] = (uint8_t)((nixieCathodeMap[Tube_E][dig[Tube_E]]) | (nixieCathodeMap[Tube_D][dig[Tube_D]] >> 8));
  535. tubesBuffer[2] = (uint8_t)((nixieCathodeMap[Tube_D][dig[Tube_D]]) | (nixieCathodeMap[Tube_B][dig[Tube_B]] >> 8));
  536. tubesBuffer[3] = (uint8_t)((nixieCathodeMap[Tube_B][dig[Tube_B]]) | (nixieCathodeMap[Tube_A][dig[Tube_A]] >> 8));
  537. tubesBuffer[4] = (uint8_t)(nixieCathodeMap[Tube_A][dig[Tube_A]]);
  538. /* Start DMA transfer to SPI */
  539. DMA1_Channel1->CCR |= DMA_CCR_EN;
  540. /* On/Off tube power */
  541. if (dig[Tube_A] == 0xf) {
  542. TUBE_A_OFF;
  543. } else {
  544. TUBE_A_ON;
  545. }
  546. if (dig[Tube_B] == 0xf) {
  547. TUBE_B_OFF;
  548. } else {
  549. TUBE_B_ON;
  550. }
  551. if (dig[Tube_D] == 0xf) {
  552. TUBE_D_OFF;
  553. } else {
  554. TUBE_D_ON;
  555. }
  556. if (dig[Tube_E] == 0xf) {
  557. TUBE_E_OFF;
  558. } else {
  559. TUBE_E_ON;
  560. }
  561. }
  562. /**
  563. * @brief Вывод HEX значений цвета в таймер.
  564. * @param : RGB value in range 0x00-0xFF
  565. * @retval : None
  566. */
  567. static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
  568. /* Более быстрый вариант, на пробу. */
  569. COLOR_R(r * 4);
  570. COLOR_G(g * 4);
  571. COLOR_B(b * 4);
  572. /* Предварительный обсчёт в переменные сделан для того,
  573. что-бы вывести значения в таймер максимально одновременно. */
  574. /*
  575. uint32_t val_r, val_g, val_b;
  576. // * 999 + 127 / 255 ???
  577. val_r = ((uint32_t)(r * 1000) + 128) / 256;
  578. val_g = ((uint32_t)(g * 1000) + 128) / 256;
  579. val_b = ((uint32_t)(b * 1000) + 128) / 256;
  580. COLOR_R((uint16_t)val_r);
  581. COLOR_G((uint16_t)val_g);
  582. COLOR_B((uint16_t)val_b);
  583. */
  584. }
  585. /**
  586. * @brief Обработка кнопок.
  587. * @param : None
  588. * @retval : None
  589. */
  590. static void btnProcess(void) {
  591. /* get pin state */
  592. uint32_t pins = BTNS_STATE;
  593. int i;
  594. for (i=0; i<BTN_NUM; i++) {
  595. if ((pins & Button[i].pin) == 0) {
  596. /* button pressed */
  597. Button[i].time ++;
  598. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  599. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  600. if (Button[i].holded == Button[i].pressed) {
  601. /* if pressed and holded - same function, then button pressed auto repeat */
  602. ES_PlaceEvent(Button[i].pressed);
  603. }
  604. }
  605. } else if (Button[i].time != 0) {
  606. /* button released */
  607. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  608. /* process long press */
  609. ES_PlaceEvent(Button[i].holded);
  610. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  611. /* process short press */
  612. ES_PlaceEvent(Button[i].pressed);
  613. }
  614. Button[i].time = 0;
  615. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  616. }
  617. } /* end FOR */
  618. }
  619. /**
  620. * On/off symbols on IN-15 tube.
  621. */
  622. void in15Off(void) {
  623. IN15_OFF;
  624. TUBE_C_OFF;
  625. }
  626. void in15Minus(void) {
  627. IN15_OFF;
  628. IN15_Minus;
  629. TUBE_C_ON;
  630. }
  631. void in15Plus(void) {
  632. IN15_OFF;
  633. IN15_Plus;
  634. TUBE_C_ON;
  635. }
  636. void in15Percent(void) {
  637. IN15_OFF;
  638. IN15_Percent;
  639. TUBE_C_ON;
  640. }
  641. void in15P(void) {
  642. IN15_OFF;
  643. IN15_P;
  644. TUBE_C_ON;
  645. }
  646. void showTime(void) {
  647. in15Minus();
  648. RTOS_SetTask(in15Off, 500, 0);
  649. uint8_t buf[4];
  650. buf[Tube_A] = Clock.Hr >> 4;
  651. buf[Tube_B] = Clock.Hr & 0xf;
  652. buf[Tube_D] = Clock.Min >> 4;
  653. buf[Tube_E] = Clock.Min & 0xf;
  654. showDigits(buf);
  655. }
  656. /**
  657. * Show info on tubes.
  658. */
  659. void showWD(void) {
  660. dispWDT = DISP_WDT_TIME;
  661. IN15_OFF;
  662. uint8_t buf[4];
  663. buf[Tube_A] = 0xf;
  664. buf[Tube_B] = Clock.WD & 0xf;
  665. buf[Tube_D] = 0xf;
  666. buf[Tube_E] = 0xf;
  667. showDigits(buf);
  668. }
  669. void showDay(void) {
  670. dispWDT = DISP_WDT_TIME;
  671. IN15_OFF;
  672. uint8_t buf[4];
  673. buf[Tube_A] = Clock.Day >> 4;
  674. buf[Tube_B] = Clock.Day & 0xf;
  675. buf[Tube_D] = 0xf;
  676. buf[Tube_E] = 0xf;
  677. showDigits(buf);
  678. }
  679. void showMonth(void) {
  680. dispWDT = DISP_WDT_TIME;
  681. IN15_OFF;
  682. uint8_t buf[4];
  683. buf[Tube_A] = 0xf;
  684. buf[Tube_B] = 0xf;
  685. buf[Tube_D] = Clock.Mon >> 4;
  686. buf[Tube_E] = Clock.Mon & 0xf;
  687. showDigits(buf);
  688. }
  689. void showDayMon(void) {
  690. dispWDT = DISP_WDT_TIME;
  691. IN15_OFF;
  692. uint8_t buf[4];
  693. buf[Tube_A] = Clock.Day >> 4;
  694. buf[Tube_B] = Clock.Day & 0xf;
  695. buf[Tube_D] = Clock.Mon >> 4;
  696. buf[Tube_E] = Clock.Mon & 0xf;
  697. showDigits(buf);
  698. }
  699. void showYear(void) {
  700. dispWDT = DISP_WDT_TIME;
  701. IN15_OFF;
  702. uint8_t buf[4];
  703. buf[Tube_A] = 2;
  704. buf[Tube_B] = 0;
  705. buf[Tube_D] = Clock.Year >> 4;
  706. buf[Tube_E] = Clock.Year & 0xf;
  707. showDigits(buf);
  708. }
  709. void showHumidity(void) {
  710. dispWDT = DISP_WDT_TIME;
  711. in15Percent();
  712. uint8_t buf[4];
  713. buf[Tube_A] = Humidity / 10;
  714. buf[Tube_B] = Humidity % 10;
  715. buf[Tube_D] = 0xf;
  716. buf[Tube_E] = 0xf;
  717. showDigits(buf);
  718. }
  719. void showTemperature(void) {
  720. dispWDT = DISP_WDT_TIME;
  721. in15Plus();
  722. uint8_t buf[4];
  723. buf[Tube_A] = 0xf;
  724. buf[Tube_B] = 0xf;
  725. buf[Tube_D] = Temperature / 10;
  726. buf[Tube_E] = Temperature % 10;
  727. showDigits(buf);
  728. }
  729. void showPressure(void) {
  730. dispWDT = DISP_WDT_TIME;
  731. in15P();
  732. uint8_t buf[4];
  733. buf[Tube_A] = 0xf;
  734. buf[Tube_B] = Pressure.s16.u8H & 0xf;
  735. buf[Tube_D] = Pressure.s16.u8L >> 4;
  736. buf[Tube_E] = Pressure.s16.u8L & 0xf;
  737. showDigits(buf);
  738. }
  739. /* Simple function for cyclic show all sensor data */
  740. void showSensorData(void) {
  741. ES_SetState(stShowSensorData);
  742. showTemperature();
  743. tdelay_ms(3000);
  744. showHumidity();
  745. tdelay_ms(3000);
  746. showPressure();
  747. tdelay_ms(3000);
  748. ES_SetState(stShowTime);
  749. showTime();
  750. }
  751. /* USER CODE END 4 */
  752. /**
  753. * @brief This function is executed in case of error occurrence.
  754. * @retval None
  755. */
  756. void Error_Handler(void)
  757. {
  758. /* USER CODE BEGIN Error_Handler_Debug */
  759. /* User can add his own implementation to report the HAL error return state */
  760. __disable_irq();
  761. while (1)
  762. {
  763. }
  764. /* USER CODE END Error_Handler_Debug */
  765. }
  766. #ifdef USE_FULL_ASSERT
  767. /**
  768. * @brief Reports the name of the source file and the source line number
  769. * where the assert_param error has occurred.
  770. * @param file: pointer to the source file name
  771. * @param line: assert_param error line source number
  772. * @retval None
  773. */
  774. void assert_failed(uint8_t *file, uint32_t line)
  775. {
  776. /* USER CODE BEGIN 6 */
  777. /* User can add his own implementation to report the file name and line number,
  778. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  779. /* USER CODE END 6 */
  780. }
  781. #endif /* USE_FULL_ASSERT */
  782. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/