123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- typedef enum {
- Tube_A = 3,
- Tube_B = 2,
- Tube_D = 1,
- Tube_E = 0
- } tube_pos_t;
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define SPI_BUFFER_SIZE 5
- /* Display timeout, sec */
- #define DISP_WDT_TIME 10
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- volatile flag_t Flag = {0};
- static LL_RCC_ClocksTypeDef rcc_clocks;
- /**
- * Nixi Tube cathodes map in Byte Array:
- * {E0 E9 E8 E7 E6 E5 E4 E3}
- * {E2 E1 D0 D9 D8 D7 D6 D5}
- * {D4 D3 D2 D1 B0 B9 B8 B7}
- * {B6 B5 B4 B3 B2 B1 A0 A9}
- * {A8 A7 A6 A5 A4 A3 A2 A1}
- *
- * Shift register bit map in Tube cathodes (from 0 to 1):
- * {5.7 5.6 5.5 5.4 5.3 5.2 5.1 5.0 4.7 4.6} VL5/E
- * {4.5 4.4 4.3 4.2 4.1 4.0 3.7 3.6 3.5 3.4} VL4/D
- * {3.3 3.2 3.1 3.0 2.7 2.6 2.5 2.4 2.3 2.2} VL2/B
- * {2.1 2.0 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1.0} VL1/A
- */
- static const uint16_t nixieCathodeMap[4][10] = {
- {0x8000, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000},
- {0x2000, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000},
- {0x0800, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400},
- {0x0200, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100}
- };
- //static const uint8_t nixieCathodeMask[4][2] = {{0x00, 0x3f}, {0xc0, 0x0f}, {0xf0, 0x03}, {0xc0, 0x00}};
- static uint8_t tubesBuffer[SPI_BUFFER_SIZE] = {0};
- static rtc_t Clock;
- static struct bme280_dev SensorDev;
- static struct bme280_data SensorData;
- static int8_t Humidity, Temperature;
- static nt16_t Pressure;
- static btn_t Button[BTN_NUM] = {
- {0, evBTN1Pressed, evBTN1Holded, BTN1_PIN},
- {0, evBTN2Pressed, evBTN2Pressed, BTN2_PIN},
- {0, evBTN3Pressed, evBTN3Pressed, BTN3_PIN},
- {0, evBTN4Pressed, evBTN4Holded, BTN4_PIN}
- };
- static volatile uint8_t dispWDT = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_I2C1_Init(void);
- static void MX_SPI1_Init(void);
- /* USER CODE BEGIN PFP */
- static void showDigits(uint8_t * dig);
- static void sensor_Init(void);
- static void sensorStartMeasure(void);
- static void sensorGetData(void);
- static void btnProcess(void);
- static void Color_RGB(uint8_t r, uint8_t g, uint8_t b);
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
- LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
- /* System interrupt init*/
- /* Peripheral interrupt init*/
- /* RCC_IRQn interrupt configuration */
- NVIC_SetPriority(RCC_IRQn, 0);
- NVIC_EnableIRQ(RCC_IRQn);
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- LL_LPM_EnableSleep();
- LL_LPM_DisableSleepOnExit();
- LL_RCC_GetSystemClocksFreq(&rcc_clocks);
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_I2C1_Init();
- MX_SPI1_Init();
- /* USER CODE BEGIN 2 */
- /* Initialize onBoard Hardware */
- Board_Init();
- /* Initialize Scheduler */
- RTOS_Init();
- /* Initialize Event State Machine */
- ES_Init(stShowTime);
- /* Enable tube power */
- TUBE_PWR_ON;
- RTC_Init();
- sensor_Init();
- /** Star SPI transfer to shift registers */
- /* Set DMA source and destination addresses. */
- /* Source: Address of the SPI buffer. */
- DMA1_Channel1->CMAR = (uint32_t)&tubesBuffer;
- /* Destination: SPI1 data register. */
- DMA1_Channel1->CPAR = (uint32_t)&(SPI1->DR);
- /* Set DMA data transfer length (SPI buffer length). */
- DMA1_Channel1->CNDTR = SPI_BUFFER_SIZE;
- /* Enable SPI+DMA transfer */
- SPI1->CR2 |= SPI_CR2_TXDMAEN;
- SPI1->CR1 |= SPI_CR1_SPE;
- Flag.SPI_TX_End = 1;
- /** Set tasks for Sheduler */
- RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
- /* USER CODE END 2 */
- /* USER CODE BEGIN WHILE */
- RTC_ReadAll(&Clock);
- es_event_t event = eventNull;
- Color_RGB(0xFF, 0x12, 0x0); // Nixie color. FF1200 or FF7E00 or FFBF00
- showTime();
- /* Infinite loop */
- while (1)
- {
- /* new second interrupt from RTC */
- if (Flag.RTC_IRQ != 0) {
- Flag.RTC_IRQ = 0;
- Blink_Start(); // !!! TODO
- RTC_ReadAll(&Clock);
- if (dispWDT != 0) {
- dispWDT --;
- if (dispWDT == 0) {
- ES_PlaceEvent(evDisplayWDT);
- }
- }
- } /* end of New second */
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- event = ES_GetEvent();
- if (event) {
- ES_Dispatch(event);
- }
- RTOS_DispatchTask();
- __WFI();
- }
- /* USER CODE END 3 */
- } /* End of mine() */
- /**
- * Sensor
- */
- static void sensor_Init(void) {
- int8_t rsltSensor;
- Flag.BME280 = 0;
- SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
- SensorDev.intf = BME280_I2C_INTF;
- SensorDev.read = user_i2c_read;
- SensorDev.write = user_i2c_write;
- SensorDev.delay_ms = tdelay_ms;
- rsltSensor = bme280_init(&SensorDev);
- if (rsltSensor == BME280_OK) {
- Flag.BME280 = 1;
- /* BME280 Recommended mode of operation: Indoor navigation */
- SensorDev.settings.osr_h = BME280_OVERSAMPLING_1X;
- SensorDev.settings.osr_p = BME280_OVERSAMPLING_16X;
- SensorDev.settings.osr_t = BME280_OVERSAMPLING_2X;
- SensorDev.settings.filter = BME280_FILTER_COEFF_16;
- rsltSensor = bme280_set_sensor_settings((BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL), &SensorDev);
- RTOS_SetTask(sensorStartMeasure, 103, 1000);
- RTOS_SetTask(sensorGetData, 603, 1000);
- }
- }
- static void sensorStartMeasure(void) {
- bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
- }
- static void sensorGetData(void) {
- bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
- int32_t tmp;
- tmp = SensorData.humidity + 512;
- Humidity = (int8_t)(tmp / 1024);
- tmp = SensorData.temperature + 50;
- Temperature = (int8_t)(tmp / 100);
- /* in 32-bit arithmetics pressure in Pa */
- tmp = SensorData.pressure * 1000;
- tmp += 66661;
- tmp /= 133322;
- /* pressure in mmHg */
- Pressure.s16.u8H = (uint8_t)(tmp / 100);
- Pressure.s16.u8L = (uint8_t)(tmp % 100);
- }
- /**
- * @brief I2C1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_I2C1_Init(void)
- {
- /* USER CODE BEGIN I2C1_Init 0 */
- /* USER CODE END I2C1_Init 0 */
- LL_I2C_InitTypeDef I2C_InitStruct = {0};
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
- /**I2C1 GPIO Configuration
- PB8 ------> I2C1_SCL
- PB9 ------> I2C1_SDA
- */
- GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
- LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
- LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* Peripheral clock enable */
- LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
- /* I2C1 DMA Init */
- /* I2C1_RX Init */
- LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_2, LL_DMAMUX_REQ_I2C1_RX);
- LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
- LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_MEDIUM);
- LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);
- LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);
- LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);
- LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
- /* I2C1_TX Init */
- LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_3, LL_DMAMUX_REQ_I2C1_TX);
- LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_3, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
- LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PRIORITY_MEDIUM);
- LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PERIPH_NOINCREMENT);
- LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MEMORY_INCREMENT);
- LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_PDATAALIGN_BYTE);
- LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_3, LL_DMA_MDATAALIGN_BYTE);
- /* I2C1 interrupt Init */
- /* USER CODE BEGIN I2C1_Init 1 */
- /* Enable DMA transfer complete/error interrupts */
- LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
- LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_2);
- LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_3);
- LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_3);
- /* USER CODE END I2C1_Init 1 */
- /** I2C Initialization
- */
- I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
- I2C_InitStruct.Timing = 0x0010061A;
- I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
- I2C_InitStruct.DigitalFilter = 0;
- I2C_InitStruct.OwnAddress1 = 0;
- I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
- I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
- LL_I2C_EnableAutoEndMode(I2C1);
- LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
- LL_I2C_DisableOwnAddress2(I2C1);
- LL_I2C_DisableGeneralCall(I2C1);
- LL_I2C_DisableClockStretching(I2C1);
- LL_I2C_Init(I2C1, &I2C_InitStruct);
- /* USER CODE BEGIN I2C1_Init 2 */
- /* USER CODE END I2C1_Init 2 */
- }
- /**
- * @brief SPI1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_SPI1_Init(void)
- {
- /* USER CODE BEGIN SPI1_Init 0 */
- /* USER CODE END SPI1_Init 0 */
- LL_SPI_InitTypeDef SPI_InitStruct = {0};
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* Peripheral clock enable */
- LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
- /**SPI1 GPIO Configuration
- PB3 ------> SPI1_SCK
- PB5 ------> SPI1_MOSI
- */
- GPIO_InitStruct.Pin = LL_GPIO_PIN_3;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
- GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
- LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
- GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
- LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* SPI1 DMA Init */
- /* SPI1_TX Init */
- LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_SPI1_TX);
- LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
- LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
- LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
- LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
- LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
- LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_BYTE);
- LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_BYTE);
- /* SPI1 interrupt Init */
- NVIC_SetPriority(SPI1_IRQn, 0);
- NVIC_EnableIRQ(SPI1_IRQn);
- /* USER CODE BEGIN SPI1_Init 1 */
- /* Enable DMA transfer complete/error interrupts */
- LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
- LL_DMA_EnableIT_TE(DMA1, LL_DMA_CHANNEL_1);
- /* USER CODE END SPI1_Init 1 */
- /* SPI1 parameter configuration*/
- SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
- SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
- SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
- SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
- SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
- SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
- SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
- SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
- SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
- SPI_InitStruct.CRCPoly = 7;
- LL_SPI_Init(SPI1, &SPI_InitStruct);
- LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
- LL_SPI_DisableNSSPulseMgt(SPI1);
- /* USER CODE BEGIN SPI1_Init 2 */
- /* USER CODE END SPI1_Init 2 */
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void)
- {
- /* Init with LL driver */
- /* DMA controller clock enable */
- LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
- NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- /* DMA1_Channel2_3_IRQn interrupt configuration */
- NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0);
- NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- LL_EXTI_InitTypeDef EXTI_InitStruct = {0};
- LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
- LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
- /**/
- LL_GPIO_ResetOutputPin(UART_EN_GPIO_Port, UART_EN_Pin);
- /**/
- LL_GPIO_ResetOutputPin(LC0_GPIO_Port, LC0_Pin);
- /**/
- LL_GPIO_ResetOutputPin(LC1_GPIO_Port, LC1_Pin);
- /**/
- LL_GPIO_ResetOutputPin(LC2_GPIO_Port, LC2_Pin);
- /**/
- LL_GPIO_ResetOutputPin(LC3_GPIO_Port, LC3_Pin);
- /**/
- LL_GPIO_ResetOutputPin(SHDN_GPIO_Port, SHDN_Pin);
- /**/
- LL_GPIO_ResetOutputPin(Latch_GPIO_Port, Latch_Pin);
- /**/
- LL_EXTI_SetEXTISource(LL_EXTI_CONFIG_PORTC, LL_EXTI_CONFIG_LINE14);
- /**/
- EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_14;
- EXTI_InitStruct.LineCommand = ENABLE;
- EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
- EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING;
- LL_EXTI_Init(&EXTI_InitStruct);
- /**/
- LL_GPIO_SetPinPull(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_PULL_UP);
- /**/
- LL_GPIO_SetPinMode(IRQ_GPIO_Port, IRQ_Pin, LL_GPIO_MODE_INPUT);
- /**/
- GPIO_InitStruct.Pin = UART_EN_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
- LL_GPIO_Init(UART_EN_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = LC0_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(LC0_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = LC1_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(LC1_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = LC2_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(LC2_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = LC3_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(LC3_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = SHDN_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
- LL_GPIO_Init(SHDN_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = BTN4_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- LL_GPIO_Init(BTN4_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = BTN1_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- LL_GPIO_Init(BTN1_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = Latch_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
- GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
- LL_GPIO_Init(Latch_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = BTN2_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- LL_GPIO_Init(BTN2_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = UART_ST_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- LL_GPIO_Init(UART_ST_GPIO_Port, &GPIO_InitStruct);
- /**/
- GPIO_InitStruct.Pin = BTN3_Pin;
- GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
- LL_GPIO_Init(BTN3_GPIO_Port, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- NVIC_SetPriority(EXTI4_15_IRQn, 0);
- NVIC_EnableIRQ(EXTI4_15_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /*************************
- * S U B R O U T I N E S *
- *************************/
- /**
- * @brief Out digits to SPI buffer. ON/off tube power.
- * @param : array with four BCD digits
- * @retval : None
- */
- static void showDigits(uint8_t * dig)
- {
- /* Clear buffer */
- tubesBuffer[0] = 0;
- tubesBuffer[1] = 0;
- tubesBuffer[2] = 0;
- tubesBuffer[3] = 0;
- tubesBuffer[4] = 0;
- /* check values range */
- int i;
- for (i=0; i<4; i++) {
- if (dig[i] > 9) {
- if (dig[i] != 0xf) {
- dig[i] = 0;
- }
- }
- }
- /* Wait for SPI */
- while (Flag.SPI_TX_End == 0) {};
- Flag.SPI_TX_End = 0;
- /* Feel buffer */
- tubesBuffer[0] = (uint8_t)(nixieCathodeMap[Tube_E][dig[Tube_E]] >> 8);
- tubesBuffer[1] = (uint8_t)((nixieCathodeMap[Tube_E][dig[Tube_E]]) | (nixieCathodeMap[Tube_D][dig[Tube_D]] >> 8));
- tubesBuffer[2] = (uint8_t)((nixieCathodeMap[Tube_D][dig[Tube_D]]) | (nixieCathodeMap[Tube_B][dig[Tube_B]] >> 8));
- tubesBuffer[3] = (uint8_t)((nixieCathodeMap[Tube_B][dig[Tube_B]]) | (nixieCathodeMap[Tube_A][dig[Tube_A]] >> 8));
- tubesBuffer[4] = (uint8_t)(nixieCathodeMap[Tube_A][dig[Tube_A]]);
- /* Start DMA transfer to SPI */
- DMA1_Channel1->CCR |= DMA_CCR_EN;
- /* On/Off tube power */
- if (dig[Tube_A] == 0xf) {
- TUBE_A_OFF;
- } else {
- TUBE_A_ON;
- }
- if (dig[Tube_B] == 0xf) {
- TUBE_B_OFF;
- } else {
- TUBE_B_ON;
- }
- if (dig[Tube_D] == 0xf) {
- TUBE_D_OFF;
- } else {
- TUBE_D_ON;
- }
- if (dig[Tube_E] == 0xf) {
- TUBE_E_OFF;
- } else {
- TUBE_E_ON;
- }
- }
- /**
- * @brief Вывод HEX значений цвета в таймер.
- * @param : RGB value in range 0x00-0xFF
- * @retval : None
- */
- static void Color_RGB(uint8_t r, uint8_t g, uint8_t b) {
- /* Более быстрый вариант, на пробу. */
- COLOR_R(r * 4);
- COLOR_G(g * 4);
- COLOR_B(b * 4);
- /* Предварительный обсчёт в переменные сделан для того,
- что-бы вывести значения в таймер максимально одновременно. */
- /*
- uint32_t val_r, val_g, val_b;
- // * 999 + 127 / 255 ???
- val_r = ((uint32_t)(r * 1000) + 128) / 256;
- val_g = ((uint32_t)(g * 1000) + 128) / 256;
- val_b = ((uint32_t)(b * 1000) + 128) / 256;
- COLOR_R((uint16_t)val_r);
- COLOR_G((uint16_t)val_g);
- COLOR_B((uint16_t)val_b);
- */
- }
- /**
- * @brief Обработка кнопок.
- * @param : None
- * @retval : None
- */
- static void btnProcess(void) {
- /* get pin state */
- uint32_t pins = BTNS_STATE;
- int i;
- for (i=0; i<BTN_NUM; i++) {
- if ((pins & Button[i].pin) == 0) {
- /* button pressed */
- Button[i].time ++;
- if (Button[i].time >= (BTN_TIME_HOLDED/BTN_SCAN_PERIOD)) {
- Button[i].time -= (BTN_TIME_REPEATED/BTN_SCAN_PERIOD);
- if (Button[i].holded == Button[i].pressed) {
- /* if pressed and holded - same function, then button pressed auto repeat */
- ES_PlaceEvent(Button[i].pressed);
- }
- }
- } else if (Button[i].time != 0) {
- /* button released */
- if (Button[i].time >= ((BTN_TIME_HOLDED - BTN_TIME_REPEATED)/BTN_SCAN_PERIOD)) {
- /* process long press */
- ES_PlaceEvent(Button[i].holded);
- } else if (Button[i].time >= (BTN_TIME_PRESSED/BTN_SCAN_PERIOD)) {
- /* process short press */
- ES_PlaceEvent(Button[i].pressed);
- }
- Button[i].time = 0;
- RTOS_SetTask(btnProcess, BTN_SCAN_PAUSE, BTN_SCAN_PERIOD);
- }
- } /* end FOR */
- }
- /**
- * On/off symbols on IN-15 tube.
- */
- void in15Off(void) {
- IN15_OFF;
- TUBE_C_OFF;
- }
- void in15Minus(void) {
- IN15_OFF;
- IN15_Minus;
- TUBE_C_ON;
- }
- void in15Plus(void) {
- IN15_OFF;
- IN15_Plus;
- TUBE_C_ON;
- }
- void in15Percent(void) {
- IN15_OFF;
- IN15_Percent;
- TUBE_C_ON;
- }
- void in15P(void) {
- IN15_OFF;
- IN15_P;
- TUBE_C_ON;
- }
- void showTime(void) {
- in15Minus();
- RTOS_SetTask(in15Off, 500, 0);
- uint8_t buf[4];
- buf[Tube_A] = Clock.Hr >> 4;
- buf[Tube_B] = Clock.Hr & 0xf;
- buf[Tube_D] = Clock.Min >> 4;
- buf[Tube_E] = Clock.Min & 0xf;
- showDigits(buf);
- }
- /**
- * Show info on tubes.
- */
- void showWD(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- uint8_t buf[4];
- buf[Tube_A] = 0xf;
- buf[Tube_B] = Clock.WD & 0xf;
- buf[Tube_D] = 0xf;
- buf[Tube_E] = 0xf;
- showDigits(buf);
- }
- void showDay(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- uint8_t buf[4];
- buf[Tube_A] = Clock.Day >> 4;
- buf[Tube_B] = Clock.Day & 0xf;
- buf[Tube_D] = 0xf;
- buf[Tube_E] = 0xf;
- showDigits(buf);
- }
- void showMonth(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- uint8_t buf[4];
- buf[Tube_A] = 0xf;
- buf[Tube_B] = 0xf;
- buf[Tube_D] = Clock.Mon >> 4;
- buf[Tube_E] = Clock.Mon & 0xf;
- showDigits(buf);
- }
- void showDayMon(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- uint8_t buf[4];
- buf[Tube_A] = Clock.Day >> 4;
- buf[Tube_B] = Clock.Day & 0xf;
- buf[Tube_D] = Clock.Mon >> 4;
- buf[Tube_E] = Clock.Mon & 0xf;
- showDigits(buf);
- }
- void showYear(void) {
- dispWDT = DISP_WDT_TIME;
- IN15_OFF;
- uint8_t buf[4];
- buf[Tube_A] = 2;
- buf[Tube_B] = 0;
- buf[Tube_D] = Clock.Year >> 4;
- buf[Tube_E] = Clock.Year & 0xf;
- showDigits(buf);
- }
- void showHumidity(void) {
- dispWDT = DISP_WDT_TIME;
- in15Percent();
- uint8_t buf[4];
- buf[Tube_A] = Humidity / 10;
- buf[Tube_B] = Humidity % 10;
- buf[Tube_D] = 0xf;
- buf[Tube_E] = 0xf;
- showDigits(buf);
- }
- void showTemperature(void) {
- dispWDT = DISP_WDT_TIME;
- in15Plus();
- uint8_t buf[4];
- buf[Tube_A] = 0xf;
- buf[Tube_B] = 0xf;
- buf[Tube_D] = Temperature / 10;
- buf[Tube_E] = Temperature % 10;
- showDigits(buf);
- }
- void showPressure(void) {
- dispWDT = DISP_WDT_TIME;
- in15P();
- uint8_t buf[4];
- buf[Tube_A] = 0xf;
- buf[Tube_B] = Pressure.s16.u8H & 0xf;
- buf[Tube_D] = Pressure.s16.u8L >> 4;
- buf[Tube_E] = Pressure.s16.u8L & 0xf;
- showDigits(buf);
- }
- /* Simple function for cyclic show all sensor data */
- void showSensorData(void) {
- ES_SetState(stShowSensorData);
- showTemperature();
- tdelay_ms(3000);
- showHumidity();
- tdelay_ms(3000);
- showPressure();
- tdelay_ms(3000);
- ES_SetState(stShowTime);
- showTime();
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|