main.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. static LL_RCC_ClocksTypeDef rcc_clocks;
  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. void SystemClock_Config(void);
  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. static void MX_TIM3_Init(void);
  88. static void MX_TIM14_Init(void);
  89. static void MX_TIM16_Init(void);
  90. static void MX_TIM17_Init(void);
  91. /* USER CODE BEGIN PFP */
  92. static void showDigit(tube_pos_t pos, uint8_t dig);
  93. static void tubes_Refresh(void);
  94. int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
  95. int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
  96. int8_t i2c_check_err(void);
  97. static void sensorStartMeasure(void);
  98. static void sensorGetData(void);
  99. static void btnProcess(void);
  100. /* USER CODE END PFP */
  101. /* Private user code ---------------------------------------------------------*/
  102. /* USER CODE BEGIN 0 */
  103. /* USER CODE END 0 */
  104. /**
  105. * @brief The application entry point.
  106. * @retval int
  107. */
  108. int main(void)
  109. {
  110. /* USER CODE BEGIN 1 */
  111. /* USER CODE END 1 */
  112. /* MCU Configuration--------------------------------------------------------*/
  113. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  114. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
  115. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  116. /* System interrupt init*/
  117. /* Peripheral interrupt init*/
  118. /* RCC_IRQn interrupt configuration */
  119. NVIC_SetPriority(RCC_IRQn, 0);
  120. NVIC_EnableIRQ(RCC_IRQn);
  121. /* USER CODE BEGIN Init */
  122. /* USER CODE END Init */
  123. /* Configure the system clock */
  124. SystemClock_Config();
  125. /* USER CODE BEGIN SysInit */
  126. LL_LPM_EnableSleep();
  127. LL_LPM_DisableSleepOnExit();
  128. LL_RCC_GetSystemClocksFreq(&rcc_clocks);
  129. /* USER CODE END SysInit */
  130. /* Initialize all configured peripherals */
  131. MX_GPIO_Init();
  132. MX_DMA_Init();
  133. MX_I2C1_Init();
  134. MX_SPI1_Init();
  135. MX_TIM3_Init();
  136. MX_TIM14_Init();
  137. MX_TIM16_Init();
  138. MX_TIM17_Init();
  139. /* USER CODE BEGIN 2 */
  140. RTOS_Init();
  141. /* Initialize Event State Machine */
  142. ES_Init(stShowTime);
  143. /* Start RGB PWM */
  144. LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH1);
  145. LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH2);
  146. LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH3);
  147. LL_TIM_EnableCounter(TIM3);
  148. /* Start Tube PWR PWM */
  149. LL_TIM_CC_EnableChannel(TIM14, LL_TIM_CHANNEL_CH1);
  150. LL_TIM_EnableCounter(TIM14);
  151. /* Enable tube power */
  152. TUBE_PWR_ON;
  153. /* Set DMA source and destination addresses. */
  154. /* Source: Address of the SPI buffer. */
  155. DMA1_Channel1->CMAR = (uint32_t)&tubesBuffer;
  156. /* Destination: SPI1 data register. */
  157. DMA1_Channel1->CPAR = (uint32_t)&(SPI1->DR);
  158. /* Set DMA data transfer length (SPI buffer length). */
  159. DMA1_Channel1->CNDTR = SPI_BUFFER_SIZE;
  160. /* Enable SPI+DMA transfer */
  161. SPI1->CR2 |= SPI_CR2_TXDMAEN;
  162. SPI1->CR1 |= SPI_CR1_SPE;
  163. tubes_Refresh();
  164. IN15_OFF;
  165. RTC_Init();
  166. int8_t rsltSensor;
  167. SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
  168. SensorDev.intf = BME280_I2C_INTF;
  169. SensorDev.read = user_i2c_read;
  170. SensorDev.write = user_i2c_write;
  171. SensorDev.delay_ms = tdelay_ms;
  172. rsltSensor = bme280_init(&SensorDev);
  173. if (rsltSensor == BME280_OK) {
  174. Flag.BME280 = 1;
  175. }
  176. /* Set tasks for Sheduler */
  177. RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
  178. /* USER CODE END 2 */
  179. /* USER CODE BEGIN WHILE */
  180. RTC_ReadAll(&Clock);
  181. if (Flag.BME280 != 0) {
  182. /* BME280 Recommended mode of operation: Indoor navigation */
  183. SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
  184. SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
  185. SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
  186. SensorDev.settings.filter = BME280_FILTER_COEFF_16;
  187. rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
  188. RTOS_SetTask(sensorStartMeasure, 103, 1000);
  189. RTOS_SetTask(sensorGetData, 603, 1000);
  190. }
  191. es_event_t event = eventNull;
  192. COLOR_RGB(0xFF, 0x12, 0x0); // Nixie color. FF7E00 or FFBF00
  193. showTime();
  194. /* Infinite loop */
  195. while (1)
  196. {
  197. /* new second interrupt from RTC */
  198. if (Flag.RTC_IRQ != 0) {
  199. Flag.RTC_IRQ = 0;
  200. RTC_ReadAll(&Clock);
  201. ES_PlaceEvent(evNewSecond);
  202. if (dispWDT != 0) {
  203. dispWDT --;
  204. if (dispWDT == 0) {
  205. ES_PlaceEvent(evDisplayWDT);
  206. }
  207. }
  208. } /* end of New second */
  209. /* USER CODE END WHILE */
  210. /* USER CODE BEGIN 3 */
  211. event = ES_GetEvent();
  212. if (event) {
  213. ES_Dispatch(event);
  214. }
  215. RTOS_DispatchTask();
  216. __WFI();
  217. }
  218. /* USER CODE END 3 */
  219. }
  220. /**
  221. * @brief Launch SPI transaction.
  222. * @retval None
  223. */
  224. static void tubes_Refresh(void) {
  225. LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
  226. }
  227. /**
  228. * @brief Check I2C fjr errors.
  229. * @retval I2C return code
  230. */
  231. int8_t i2c_check_err(void) {
  232. int8_t r = I2C_RET_OK;
  233. if ((I2C1->ISR & I2C_ISR_NACKF) != 0) {
  234. /* device not present */
  235. r = I2C_RET_NACK;
  236. } else if ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR)) != 0) {
  237. /* other error */
  238. r = I2C_RET_ERR;
  239. }
  240. if (r != I2C_RET_OK) {
  241. /* restart I2C and clear flags */
  242. I2C1->CR1 &= ~I2C_CR1_PE;
  243. while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
  244. I2C1->CR1 |= I2C_CR1_PE;
  245. }
  246. return r;
  247. }
  248. /**
  249. * @brief Read len bytes from I2C bus to data by reg_addr.
  250. * @retval I2C return code
  251. */
  252. int8_t user_i2c_read(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
  253. int8_t r = I2C_RET_OK;
  254. Flag.I2C_RX_End = 0;
  255. Flag.I2C_RX_Err = 0;
  256. Flag.I2C_TX_Err = 0;
  257. /* wait for i2c */
  258. while ( I2C1->ISR & I2C_ISR_BUSY ) { __NOP(); };
  259. /* prepare i2c for sending reg addr */
  260. I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  261. I2C1->CR2 |= ( id | 1 << I2C_CR2_NBYTES_Pos );
  262. /* gen START */
  263. I2C1->CR2 |= ( I2C_CR2_START );
  264. /* wait for byte request or any error */
  265. while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
  266. if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
  267. /* device ok, send reg addr */
  268. I2C1->TXDR = reg_addr;
  269. } else {
  270. r = i2c_check_err();
  271. if (r != I2C_RET_OK) {
  272. Flag.I2C_TX_Err = 1;
  273. return r;
  274. }
  275. }
  276. /* wait for i2c or any error */
  277. while (((I2C1->ISR & I2C_ISR_BUSY) != 0) && ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF)) == 0)) { __NOP(); };
  278. r = i2c_check_err();
  279. if (r != I2C_RET_OK) {
  280. Flag.I2C_TX_Err = 1;
  281. return r;
  282. }
  283. /* prepare dma channel for receiving data */
  284. DMA1_Channel2->CMAR = (uint32_t)data;
  285. DMA1_Channel2->CPAR = (uint32_t)&(I2C1->RXDR);
  286. DMA1_Channel2->CNDTR = len;
  287. DMA1_Channel2->CCR |= DMA_CCR_EN;
  288. /* prepare i2c for receiving data */
  289. I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  290. I2C1->CR2 |= ( id | len << I2C_CR2_NBYTES_Pos | I2C_CR2_RD_WRN);
  291. /* launch receiving */
  292. I2C1->CR1 |= ( I2C_CR1_RXDMAEN );
  293. I2C1->CR2 |= ( I2C_CR2_START );
  294. /* wait for receiving data */
  295. while ((Flag.I2C_RX_End == 0) && (Flag.I2C_RX_Err == 0)) { __NOP(); };
  296. return r;
  297. }
  298. /**
  299. * @brief Write len bytes to I2C bus from data by reg_addr.
  300. * @retval I2C return code
  301. */
  302. int8_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, const uint16_t len) {
  303. int8_t r = I2C_RET_OK;
  304. Flag.I2C_TX_End = 0;
  305. Flag.I2C_TX_Err = 0;
  306. DMA1_Channel3->CMAR = (uint32_t)data;
  307. DMA1_Channel3->CPAR = (uint32_t)&(I2C1->TXDR);
  308. DMA1_Channel3->CNDTR = len;
  309. while ( I2C1->ISR & I2C_ISR_BUSY ) {};
  310. I2C1->CR2 &= ~( I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RD_WRN);
  311. I2C1->CR2 |= ( id | (len + 1) << I2C_CR2_NBYTES_Pos );
  312. I2C1->CR2 |= ( I2C_CR2_START );
  313. while ((I2C1->ISR & (I2C_ISR_ARLO | I2C_ISR_BERR | I2C_ISR_NACKF | I2C_ISR_TXE)) == 0) { __NOP(); };
  314. if ((I2C2->ISR & I2C_ISR_TXE) != 0) {
  315. I2C1->TXDR = reg_addr;
  316. } else {
  317. r = i2c_check_err();
  318. if (r != I2C_RET_OK) {
  319. Flag.I2C_TX_Err = 1;
  320. return r;
  321. }
  322. }
  323. DMA1_Channel3->CCR |= DMA_CCR_EN;
  324. I2C1->CR1 |= ( I2C_CR1_TXDMAEN );
  325. return r;
  326. }
  327. /**
  328. * Sensor
  329. */
  330. static void sensorStartMeasure(void) {
  331. bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
  332. }
  333. static void sensorGetData(void) {
  334. bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
  335. int32_t tmp;
  336. tmp = SensorData.humidity + 512;
  337. Humidity = (int8_t)(tmp / 1024);
  338. tmp = SensorData.temperature + 50;
  339. Temperature = (int8_t)(tmp / 100);
  340. /* in 32-bit arithmetics pressure in Pa */
  341. tmp = SensorData.pressure * 1000;
  342. tmp += 66661;
  343. tmp /= 133322;
  344. /* pressure in mmHg */
  345. Pressure.s16.u8H = (uint8_t)(tmp / 100);
  346. Pressure.s16.u8L = (uint8_t)(tmp % 100);
  347. }
  348. /**
  349. * @brief System Clock Configuration
  350. * @retval None
  351. */
  352. void SystemClock_Config(void)
  353. {
  354. /* HSI configuration and activation */
  355. LL_RCC_HSI_Enable();
  356. while(LL_RCC_HSI_IsReady() != 1)
  357. {
  358. }
  359. /* Main PLL configuration and activation */
  360. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, LL_RCC_PLLM_DIV_2, 9, LL_RCC_PLLR_DIV_3);
  361. LL_RCC_PLL_Enable();
  362. LL_RCC_PLL_EnableDomain_SYS();
  363. while(LL_RCC_PLL_IsReady() != 1)
  364. {
  365. }
  366. /* Set AHB prescaler*/
  367. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  368. /* Sysclk activation on the main PLL */
  369. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  370. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
  371. {
  372. }
  373. /* Set APB1 prescaler*/
  374. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  375. LL_Init1msTick(24000000);
  376. /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
  377. LL_SetSystemCoreClock(24000000);
  378. LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
  379. }
  380. /**
  381. * @brief I2C1 Initialization Function
  382. * @param None
  383. * @retval None
  384. */
  385. static void MX_I2C1_Init(void)
  386. {
  387. /* USER CODE BEGIN I2C1_Init 0 */
  388. /* USER CODE END I2C1_Init 0 */
  389. LL_I2C_InitTypeDef I2C_InitStruct = {0};
  390. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  391. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  392. /**I2C1 GPIO Configuration
  393. PB6 ------> I2C1_SCL
  394. PB7 ------> I2C1_SDA
  395. */
  396. GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
  397. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  398. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  399. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  400. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  401. GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
  402. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  403. GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
  404. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  405. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  406. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  407. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  408. GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
  409. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  410. /* Peripheral clock enable */
  411. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
  412. /* I2C1 DMA Init */
  413. /* I2C1_RX Init */
  414. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_2, LL_DMAMUX_REQ_I2C1_RX);
  415. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
  416. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_MEDIUM);
  417. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);
  418. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);
  419. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);
  420. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
  421. /* I2C1_TX Init */
  422. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_3, LL_DMAMUX_REQ_I2C1_TX);
  423. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_3, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
  424. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PRIORITY_MEDIUM);
  425. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PERIPH_NOINCREMENT);
  426. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MEMORY_INCREMENT);
  427. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PDATAALIGN_BYTE);
  428. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MDATAALIGN_BYTE);
  429. /* I2C1 interrupt Init */
  430. /* USER CODE BEGIN I2C1_Init 1 */
  431. /* Enable DMA transfer complete/error interrupts */
  432. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
  433. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
  434. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
  435. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
  436. /* USER CODE END I2C1_Init 1 */
  437. /** I2C Initialization
  438. */
  439. I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
  440. I2C_InitStruct.Timing = 0x0010061A;
  441. I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
  442. I2C_InitStruct.DigitalFilter = 0;
  443. I2C_InitStruct.OwnAddress1 = 0;
  444. I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
  445. I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
  446. LL_I2C_EnableAutoEndMode(I2C1);
  447. LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
  448. LL_I2C_DisableOwnAddress2(I2C1);
  449. LL_I2C_DisableGeneralCall(I2C1);
  450. LL_I2C_DisableClockStretching(I2C1);
  451. LL_I2C_Init(I2C1, &I2C_InitStruct);
  452. /* USER CODE BEGIN I2C1_Init 2 */
  453. /* USER CODE END I2C1_Init 2 */
  454. }
  455. /**
  456. * @brief SPI1 Initialization Function
  457. * @param None
  458. * @retval None
  459. */
  460. static void MX_SPI1_Init(void)
  461. {
  462. /* USER CODE BEGIN SPI1_Init 0 */
  463. /* USER CODE END SPI1_Init 0 */
  464. LL_SPI_InitTypeDef SPI_InitStruct = {0};
  465. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  466. /* Peripheral clock enable */
  467. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  468. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  469. /**SPI1 GPIO Configuration
  470. PB3 ------> SPI1_SCK
  471. PB5 ------> SPI1_MOSI
  472. */
  473. GPIO_InitStruct.Pin = LL_GPIO_PIN_3;
  474. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  475. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  476. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  477. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  478. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  479. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  480. GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
  481. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  482. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  483. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  484. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  485. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  486. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  487. /* SPI1 DMA Init */
  488. /* SPI1_TX Init */
  489. LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_SPI1_TX);
  490. LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
  491. LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
  492. LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
  493. LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
  494. LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
  495. LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_BYTE);
  496. LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_BYTE);
  497. /* SPI1 interrupt Init */
  498. NVIC_SetPriority(SPI1_IRQn, 0);
  499. NVIC_EnableIRQ(SPI1_IRQn);
  500. /* USER CODE BEGIN SPI1_Init 1 */
  501. /* Enable DMA transfer complete/error interrupts */
  502. LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
  503. LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
  504. /* USER CODE END SPI1_Init 1 */
  505. /* SPI1 parameter configuration*/
  506. SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  507. SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  508. SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  509. SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
  510. SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
  511. SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  512. SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
  513. SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  514. SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  515. SPI_InitStruct.CRCPoly = 7;
  516. LL_SPI_Init(SPI1, &SPI_InitStruct);
  517. LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
  518. LL_SPI_DisableNSSPulseMgt(SPI1);
  519. /* USER CODE BEGIN SPI1_Init 2 */
  520. /* USER CODE END SPI1_Init 2 */
  521. }
  522. /**
  523. * @brief TIM3 Initialization Function
  524. * @param None
  525. * @retval None
  526. */
  527. static void MX_TIM3_Init(void)
  528. {
  529. /* USER CODE BEGIN TIM3_Init 0 */
  530. /* USER CODE END TIM3_Init 0 */
  531. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  532. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  533. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  534. /* Peripheral clock enable */
  535. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
  536. /* USER CODE BEGIN TIM3_Init 1 */
  537. /* USER CODE END TIM3_Init 1 */
  538. TIM_InitStruct.Prescaler = 94;
  539. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  540. TIM_InitStruct.Autoreload = 255;
  541. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  542. LL_TIM_Init(TIM3, &TIM_InitStruct);
  543. LL_TIM_EnableARRPreload(TIM3);
  544. LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH1);
  545. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  546. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  547. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  548. TIM_OC_InitStruct.CompareValue = 25;
  549. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  550. LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  551. LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
  552. LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH2);
  553. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  554. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  555. LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
  556. LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH2);
  557. LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH3);
  558. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  559. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  560. LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH3, &TIM_OC_InitStruct);
  561. LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH3);
  562. LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
  563. LL_TIM_DisableMasterSlaveMode(TIM3);
  564. /* USER CODE BEGIN TIM3_Init 2 */
  565. /* USER CODE END TIM3_Init 2 */
  566. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  567. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  568. /**TIM3 GPIO Configuration
  569. PA6 ------> TIM3_CH1
  570. PA7 ------> TIM3_CH2
  571. PB0 ------> TIM3_CH3
  572. */
  573. GPIO_InitStruct.Pin = PWM_R_Pin;
  574. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  575. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  576. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  577. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  578. GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  579. LL_GPIO_Init(PWM_R_GPIO_Port, &GPIO_InitStruct);
  580. GPIO_InitStruct.Pin = PWM_G_Pin;
  581. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  582. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  583. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  584. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  585. GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  586. LL_GPIO_Init(PWM_G_GPIO_Port, &GPIO_InitStruct);
  587. GPIO_InitStruct.Pin = PWM_B_Pin;
  588. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  589. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  590. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  591. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  592. GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  593. LL_GPIO_Init(PWM_B_GPIO_Port, &GPIO_InitStruct);
  594. }
  595. /**
  596. * @brief TIM14 Initialization Function
  597. * @param None
  598. * @retval None
  599. */
  600. static void MX_TIM14_Init(void)
  601. {
  602. /* USER CODE BEGIN TIM14_Init 0 */
  603. /* USER CODE END TIM14_Init 0 */
  604. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  605. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  606. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  607. /* Peripheral clock enable */
  608. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM14);
  609. /* TIM14 interrupt Init */
  610. NVIC_SetPriority(TIM14_IRQn, 0);
  611. NVIC_EnableIRQ(TIM14_IRQn);
  612. /* USER CODE BEGIN TIM14_Init 1 */
  613. /* USER CODE END TIM14_Init 1 */
  614. TIM_InitStruct.Prescaler = 240;
  615. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  616. TIM_InitStruct.Autoreload = 1000;
  617. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  618. LL_TIM_Init(TIM14, &TIM_InitStruct);
  619. LL_TIM_EnableARRPreload(TIM14);
  620. LL_TIM_OC_EnablePreload(TIM14, LL_TIM_CHANNEL_CH1);
  621. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  622. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  623. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  624. TIM_OC_InitStruct.CompareValue = 750;
  625. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  626. LL_TIM_OC_Init(TIM14, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  627. LL_TIM_OC_DisableFast(TIM14, LL_TIM_CHANNEL_CH1);
  628. /* USER CODE BEGIN TIM14_Init 2 */
  629. /* USER CODE END TIM14_Init 2 */
  630. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  631. /**TIM14 GPIO Configuration
  632. PB1 ------> TIM14_CH1
  633. */
  634. GPIO_InitStruct.Pin = PWM_T_Pin;
  635. GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  636. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  637. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  638. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  639. GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
  640. LL_GPIO_Init(PWM_T_GPIO_Port, &GPIO_InitStruct);
  641. }
  642. /**
  643. * @brief TIM16 Initialization Function
  644. * @param None
  645. * @retval None
  646. */
  647. static void MX_TIM16_Init(void)
  648. {
  649. /* USER CODE BEGIN TIM16_Init 0 */
  650. /* USER CODE END TIM16_Init 0 */
  651. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  652. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  653. LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
  654. /* Peripheral clock enable */
  655. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM16);
  656. /* TIM16 interrupt Init */
  657. NVIC_SetPriority(TIM16_IRQn, 0);
  658. NVIC_EnableIRQ(TIM16_IRQn);
  659. /* USER CODE BEGIN TIM16_Init 1 */
  660. /* USER CODE END TIM16_Init 1 */
  661. TIM_InitStruct.Prescaler = 24;
  662. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  663. TIM_InitStruct.Autoreload = 1000;
  664. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  665. TIM_InitStruct.RepetitionCounter = 0;
  666. LL_TIM_Init(TIM16, &TIM_InitStruct);
  667. LL_TIM_EnableARRPreload(TIM16);
  668. LL_TIM_OC_EnablePreload(TIM16, LL_TIM_CHANNEL_CH1);
  669. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  670. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  671. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  672. TIM_OC_InitStruct.CompareValue = 0;
  673. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  674. TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  675. TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  676. TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  677. LL_TIM_OC_Init(TIM16, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  678. LL_TIM_OC_DisableFast(TIM16, LL_TIM_CHANNEL_CH1);
  679. TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
  680. TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
  681. TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
  682. TIM_BDTRInitStruct.DeadTime = 0;
  683. TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
  684. TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
  685. TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
  686. TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  687. LL_TIM_BDTR_Init(TIM16, &TIM_BDTRInitStruct);
  688. /* USER CODE BEGIN TIM16_Init 2 */
  689. /* USER CODE END TIM16_Init 2 */
  690. }
  691. /**
  692. * @brief TIM17 Initialization Function
  693. * @param None
  694. * @retval None
  695. */
  696. static void MX_TIM17_Init(void)
  697. {
  698. /* USER CODE BEGIN TIM17_Init 0 */
  699. /* USER CODE END TIM17_Init 0 */
  700. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  701. LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
  702. LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
  703. /* Peripheral clock enable */
  704. LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM17);
  705. /* TIM17 interrupt Init */
  706. NVIC_SetPriority(TIM17_IRQn, 0);
  707. NVIC_EnableIRQ(TIM17_IRQn);
  708. /* USER CODE BEGIN TIM17_Init 1 */
  709. /* USER CODE END TIM17_Init 1 */
  710. TIM_InitStruct.Prescaler = 240;
  711. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  712. TIM_InitStruct.Autoreload = 1000;
  713. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  714. TIM_InitStruct.RepetitionCounter = 100;
  715. LL_TIM_Init(TIM17, &TIM_InitStruct);
  716. LL_TIM_EnableARRPreload(TIM17);
  717. LL_TIM_OC_EnablePreload(TIM17, LL_TIM_CHANNEL_CH1);
  718. TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  719. TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  720. TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  721. TIM_OC_InitStruct.CompareValue = 0;
  722. TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  723. TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
  724. TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
  725. TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
  726. LL_TIM_OC_Init(TIM17, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  727. LL_TIM_OC_DisableFast(TIM17, LL_TIM_CHANNEL_CH1);
  728. TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
  729. TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
  730. TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
  731. TIM_BDTRInitStruct.DeadTime = 0;
  732. TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
  733. TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
  734. TIM_BDTRInitStruct.BreakFilter = LL_TIM_BREAK_FILTER_FDIV1;
  735. TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
  736. LL_TIM_BDTR_Init(TIM17, &TIM_BDTRInitStruct);
  737. /* USER CODE BEGIN TIM17_Init 2 */
  738. /* USER CODE END TIM17_Init 2 */
  739. }
  740. /**
  741. * Enable DMA controller clock
  742. */
  743. static void MX_DMA_Init(void)
  744. {
  745. /* Init with LL driver */
  746. /* DMA controller clock enable */
  747. LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
  748. /* DMA interrupt init */
  749. /* DMA1_Channel1_IRQn interrupt configuration */
  750. NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
  751. NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  752. /* DMA1_Channel2_3_IRQn interrupt configuration */
  753. NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0);
  754. NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
  755. }
  756. /**
  757. * @brief GPIO Initialization Function
  758. * @param None
  759. * @retval None
  760. */
  761. static void MX_GPIO_Init(void)
  762. {
  763. LL_EXTI_InitTypeDef EXTI_InitStruct = {0};
  764. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  765. /* GPIO Ports Clock Enable */
  766. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  767. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
  768. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  769. /**/
  770. LL_GPIO_ResetOutputPin(LC0_GPIO_Port, LC0_Pin);
  771. /**/
  772. LL_GPIO_ResetOutputPin(LC1_GPIO_Port, LC1_Pin);
  773. /**/
  774. LL_GPIO_ResetOutputPin(LC2_GPIO_Port, LC2_Pin);
  775. /**/
  776. LL_GPIO_ResetOutputPin(LC3_GPIO_Port, LC3_Pin);
  777. /**/
  778. LL_GPIO_ResetOutputPin(SHDN_GPIO_Port, SHDN_Pin);
  779. /**/
  780. LL_GPIO_ResetOutputPin(Latch_GPIO_Port, Latch_Pin);
  781. /**/
  782. GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
  783. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  784. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  785. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  786. /**/
  787. GPIO_InitStruct.Pin = LL_GPIO_PIN_14;
  788. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  789. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  790. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  791. /**/
  792. GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  793. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  794. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  795. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  796. /**/
  797. GPIO_InitStruct.Pin = LC0_Pin;
  798. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  799. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  800. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  801. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  802. LL_GPIO_Init(LC0_GPIO_Port, &GPIO_InitStruct);
  803. /**/
  804. GPIO_InitStruct.Pin = LC1_Pin;
  805. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  806. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  807. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  808. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  809. LL_GPIO_Init(LC1_GPIO_Port, &GPIO_InitStruct);
  810. /**/
  811. GPIO_InitStruct.Pin = LC2_Pin;
  812. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  813. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  814. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  815. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  816. LL_GPIO_Init(LC2_GPIO_Port, &GPIO_InitStruct);
  817. /**/
  818. GPIO_InitStruct.Pin = LC3_Pin;
  819. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  820. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  821. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  822. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  823. LL_GPIO_Init(LC3_GPIO_Port, &GPIO_InitStruct);
  824. /**/
  825. GPIO_InitStruct.Pin = SHDN_Pin;
  826. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  827. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  828. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  829. GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  830. LL_GPIO_Init(SHDN_GPIO_Port, &GPIO_InitStruct);
  831. /**/
  832. GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
  833. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  834. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  835. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  836. /**/
  837. GPIO_InitStruct.Pin = LL_GPIO_PIN_2;
  838. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  839. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  840. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  841. /**/
  842. GPIO_InitStruct.Pin = BTN1_Pin;
  843. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  844. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  845. LL_GPIO_Init(BTN1_GPIO_Port, &GPIO_InitStruct);
  846. /**/
  847. GPIO_InitStruct.Pin = BTN2_Pin;
  848. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  849. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  850. LL_GPIO_Init(BTN2_GPIO_Port, &GPIO_InitStruct);
  851. /**/
  852. GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
  853. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  854. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  855. LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  856. /**/
  857. GPIO_InitStruct.Pin = BTN3_Pin;
  858. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  859. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  860. LL_GPIO_Init(BTN3_GPIO_Port, &GPIO_InitStruct);
  861. /**/
  862. GPIO_InitStruct.Pin = BTN4_Pin;
  863. GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  864. GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  865. LL_GPIO_Init(BTN4_GPIO_Port, &GPIO_InitStruct);
  866. /**/
  867. GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
  868. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  869. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  870. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  871. /**/
  872. GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  873. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  874. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  875. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  876. /**/
  877. GPIO_InitStruct.Pin = Latch_Pin;
  878. GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  879. GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  880. GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
  881. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  882. LL_GPIO_Init(Latch_GPIO_Port, &GPIO_InitStruct);
  883. /**/
  884. LL_EXTI_SetEXTISource(LL_EXTI_CONFIG_PORTB, LL_EXTI_CONFIG_LINE8);
  885. /**/
  886. EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_8;
  887. EXTI_InitStruct.LineCommand = ENABLE;
  888. EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
  889. EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING;
  890. LL_EXTI_Init(&EXTI_InitStruct);
  891. /**/
  892. LL_GPIO_SetPinPull(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_PULL_UP);
  893. /**/
  894. LL_GPIO_SetPinMode(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_MODE_INPUT);
  895. /* EXTI interrupt init*/
  896. NVIC_SetPriority(EXTI4_15_IRQn, 0);
  897. NVIC_EnableIRQ(EXTI4_15_IRQn);
  898. }
  899. /* USER CODE BEGIN 4 */
  900. /**
  901. * S U B R O U T I N E S
  902. */
  903. /* Feel byte with tube position by digit.
  904. * If digit == 0xf, then tube is off -- clear all bits.
  905. */
  906. static void showDigit(tube_pos_t pos, uint8_t dig)
  907. {
  908. if (dig > 9) {
  909. if (dig != 0xf) {
  910. dig = 0;
  911. }
  912. }
  913. switch (pos) {
  914. case Tube_E:
  915. tubesBuffer[0] = 0;
  916. tubesBuffer[1] &= nixieCathodeMask[Tube_E][1];
  917. if (Tube_E != 0xf) {
  918. tubesBuffer[0] = (uint8_t)(nixieCathodeMap[Tube_E][dig] >> 8);
  919. tubesBuffer[1] |= (uint8_t)(nixieCathodeMap[Tube_E][dig]);
  920. }
  921. break;
  922. case Tube_D:
  923. tubesBuffer[1] &= nixieCathodeMask[Tube_D][0];
  924. tubesBuffer[2] &= nixieCathodeMask[Tube_D][1];
  925. if (Tube_D != 0xf) {
  926. tubesBuffer[1] |= (uint8_t)(nixieCathodeMap[Tube_D][dig] >> 8);
  927. tubesBuffer[2] |= (uint8_t)(nixieCathodeMap[Tube_D][dig]);
  928. }
  929. break;
  930. case Tube_B:
  931. tubesBuffer[2] &= nixieCathodeMask[Tube_B][0];
  932. tubesBuffer[3] &= nixieCathodeMask[Tube_B][1];
  933. if (Tube_B != 0xf) {
  934. tubesBuffer[2] |= (uint8_t)(nixieCathodeMap[Tube_B][dig] >> 8);
  935. tubesBuffer[3] |= (uint8_t)(nixieCathodeMap[Tube_B][dig]);
  936. }
  937. break;
  938. case Tube_A:
  939. tubesBuffer[3] &= nixieCathodeMask[Tube_A][0];
  940. tubesBuffer[4] = 0;
  941. if (Tube_A != 0xf) {
  942. tubesBuffer[3] |= (uint8_t)(nixieCathodeMap[Tube_A][dig] >> 8);
  943. tubesBuffer[4] = (uint8_t)(nixieCathodeMap[Tube_A][dig]);
  944. }
  945. break;
  946. default:
  947. break;
  948. }
  949. }
  950. /**
  951. * @brief Îáðàáîòêà êíîïîê.
  952. * @param : None
  953. * @retval : None
  954. */
  955. static void btnProcess(void) {
  956. /* get pin state */
  957. uint32_t pins = BTN_PORT->IDR & BTN_PINS;
  958. int i;
  959. for (i=0; i<BTN_NUM; i++) {
  960. if ((pins & Button[i].pin) == 0) {
  961. /* button pressed */
  962. Button[i].time ++;
  963. if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
  964. Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
  965. if (Button[i].holded == Button[i].pressed) {
  966. /* if pressed and holded - same function, then button pressed auto repeat */
  967. ES_PlaceEvent(Button[i].pressed);
  968. }
  969. }
  970. } else if (Button[i].time != 0) {
  971. /* button released */
  972. if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
  973. /* process long press */
  974. ES_PlaceEvent(Button[i].holded);
  975. } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
  976. /* process short press */
  977. ES_PlaceEvent(Button[i].pressed);
  978. }
  979. Button[i].time = 0;
  980. RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
  981. }
  982. } /* end FOR */
  983. }
  984. void in15Off(void) {
  985. IN15_OFF;
  986. }
  987. void in15Minus(void) {
  988. IN15_OFF;
  989. IN15_Minus;
  990. }
  991. void in15Plus(void) {
  992. IN15_OFF;
  993. IN15_Plus;
  994. }
  995. void in15Percent(void) {
  996. IN15_OFF;
  997. IN15_Percent;
  998. }
  999. void in15P(void) {
  1000. IN15_OFF;
  1001. IN15_P;
  1002. }
  1003. void showTime(void) {
  1004. in15Minus();
  1005. RTOS_SetTask(in15Off, 500, 0);
  1006. showDigit(Tube_A, Clock.Hr >> 4);
  1007. showDigit(Tube_B, Clock.Hr & 0xf);
  1008. showDigit(Tube_D, Clock.Min >> 4);
  1009. showDigit(Tube_E, Clock.Min & 0xf);
  1010. tubes_Refresh();
  1011. }
  1012. void showWD(void) {
  1013. dispWDT = DISP_WDT_TIME;
  1014. IN15_OFF;
  1015. showDigit(Tube_A, 0xf);
  1016. showDigit(Tube_B, Clock.WD & 0xf);
  1017. showDigit(Tube_D, 0xf);
  1018. showDigit(Tube_E, 0xf);
  1019. tubes_Refresh();
  1020. }
  1021. void showDay(void) {
  1022. dispWDT = DISP_WDT_TIME;
  1023. IN15_OFF;
  1024. showDigit(Tube_A, Clock.Day >> 4);
  1025. showDigit(Tube_B, Clock.Day & 0xf);
  1026. showDigit(Tube_D, 0xf);
  1027. showDigit(Tube_E, 0xf);
  1028. tubes_Refresh();
  1029. }
  1030. void showMonth(void) {
  1031. dispWDT = DISP_WDT_TIME;
  1032. IN15_OFF;
  1033. showDigit(Tube_A, 0xf);
  1034. showDigit(Tube_B, 0xf);
  1035. showDigit(Tube_D, Clock.Mon >> 4);
  1036. showDigit(Tube_E, Clock.Mon & 0xf);
  1037. tubes_Refresh();
  1038. }
  1039. void showDayMon(void) {
  1040. dispWDT = DISP_WDT_TIME;
  1041. IN15_OFF;
  1042. showDigit(Tube_A, Clock.Day >> 4);
  1043. showDigit(Tube_B, Clock.Day & 0xf);
  1044. showDigit(Tube_D, Clock.Mon >> 4);
  1045. showDigit(Tube_E, Clock.Mon & 0xf);
  1046. tubes_Refresh();
  1047. }
  1048. void showYear(void) {
  1049. dispWDT = DISP_WDT_TIME;
  1050. IN15_OFF;
  1051. showDigit(Tube_A, 2);
  1052. showDigit(Tube_B, 0);
  1053. showDigit(Tube_D, Clock.Year >> 4);
  1054. showDigit(Tube_E, Clock.Year & 0xf);
  1055. tubes_Refresh();
  1056. }
  1057. void showHumidity(void) {
  1058. dispWDT = DISP_WDT_TIME;
  1059. in15Percent();
  1060. showDigit(Tube_A, Humidity >> 4);
  1061. showDigit(Tube_B, Humidity & 0xf);
  1062. showDigit(Tube_D, 0xf);
  1063. showDigit(Tube_E, 0xf);
  1064. tubes_Refresh();
  1065. }
  1066. void showTemperature(void) {
  1067. dispWDT = DISP_WDT_TIME;
  1068. in15Plus();
  1069. showDigit(Tube_A, 0xf);
  1070. showDigit(Tube_B, 0xf);
  1071. showDigit(Tube_D, Temperature >> 4);
  1072. showDigit(Tube_E, Temperature & 0xf);
  1073. tubes_Refresh();
  1074. }
  1075. void showPressure(void) {
  1076. dispWDT = DISP_WDT_TIME;
  1077. in15P();
  1078. showDigit(Tube_A, 0xf);
  1079. showDigit(Tube_B, Pressure.s16.u8H & 0xf);
  1080. showDigit(Tube_D, Pressure.s16.u8L >> 4);
  1081. showDigit(Tube_E, Pressure.s16.u8L & 0xf);
  1082. tubes_Refresh();
  1083. }
  1084. /* Simple function for cyclic show all sensor data */
  1085. void showSensorData(void) {
  1086. ES_SetState(stShowSensorData);
  1087. showTemperature();
  1088. tdelay_ms(3000);
  1089. showHumidity();
  1090. tdelay_ms(3000);
  1091. showPressure();
  1092. tdelay_ms(3000);
  1093. ES_SetState(stShowTime);
  1094. showTime();
  1095. }
  1096. /* USER CODE END 4 */
  1097. /**
  1098. * @brief This function is executed in case of error occurrence.
  1099. * @retval None
  1100. */
  1101. void Error_Handler(void)
  1102. {
  1103. /* USER CODE BEGIN Error_Handler_Debug */
  1104. /* User can add his own implementation to report the HAL error return state */
  1105. __disable_irq();
  1106. while (1)
  1107. {
  1108. }
  1109. /* USER CODE END Error_Handler_Debug */
  1110. }
  1111. #ifdef USE_FULL_ASSERT
  1112. /**
  1113. * @brief Reports the name of the source file and the source line number
  1114. * where the assert_param error has occurred.
  1115. * @param file: pointer to the source file name
  1116. * @param line: assert_param error line source number
  1117. * @retval None
  1118. */
  1119. void assert_failed(uint8_t *file, uint32_t line)
  1120. {
  1121. /* USER CODE BEGIN 6 */
  1122. /* User can add his own implementation to report the file name and line number,
  1123. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  1124. /* USER CODE END 6 */
  1125. }
  1126. #endif /* USE_FULL_ASSERT */
  1127. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/