|
@@ -37,7 +37,9 @@ typedef enum {
|
|
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
/* USER CODE BEGIN PD */
|
|
|
-#define SPI_BUFFER_SIZE 5
|
|
|
+#define SPI_BUFFER_SIZE 5
|
|
|
+/* Display timeout, sec */
|
|
|
+#define DISP_WDT_TIME 10
|
|
|
|
|
|
/* USER CODE END PD */
|
|
|
|
|
@@ -74,8 +76,16 @@ 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 rsltSensor;
|
|
|
-static int16_t Humidity, Temperature, Pressure;
|
|
|
+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 -----------------------------------------------*/
|
|
@@ -90,10 +100,13 @@ static void MX_TIM16_Init(void);
|
|
|
static void MX_TIM17_Init(void);
|
|
|
/* USER CODE BEGIN PFP */
|
|
|
static void showDigit(tube_pos_t pos, uint8_t dig);
|
|
|
-static void SPI_StartTX(void);
|
|
|
+static void tubes_Refresh(void);
|
|
|
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
|
|
|
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
|
|
|
int8_t i2c_check_err(void);
|
|
|
+static void sensorStartMeasure(void);
|
|
|
+static void sensorGetData(void);
|
|
|
+static void btnProcess(void);
|
|
|
/* USER CODE END PFP */
|
|
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
@@ -150,6 +163,9 @@ int main(void)
|
|
|
/* USER CODE BEGIN 2 */
|
|
|
RTOS_Init();
|
|
|
|
|
|
+ /* Initialize Event State Machine */
|
|
|
+ ES_Init(stShowTime);
|
|
|
+
|
|
|
/* Start RGB PWM */
|
|
|
LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH1);
|
|
|
LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH2);
|
|
@@ -175,12 +191,13 @@ int main(void)
|
|
|
SPI1->CR2 |= SPI_CR2_TXDMAEN;
|
|
|
SPI1->CR1 |= SPI_CR1_SPE;
|
|
|
|
|
|
- SPI_StartTX();
|
|
|
+ tubes_Refresh();
|
|
|
|
|
|
IN15_OFF;
|
|
|
|
|
|
RTC_Init();
|
|
|
|
|
|
+ int8_t rsltSensor;
|
|
|
SensorDev.dev_id = (BME280_I2C_ADDR_PRIM << 1);
|
|
|
SensorDev.intf = BME280_I2C_INTF;
|
|
|
SensorDev.read = user_i2c_read;
|
|
@@ -190,6 +207,9 @@ int main(void)
|
|
|
if (rsltSensor == BME280_OK) {
|
|
|
Flag.BME280 = 1;
|
|
|
}
|
|
|
+
|
|
|
+ /* Set tasks for Sheduler */
|
|
|
+ RTOS_SetTask(btnProcess, 1, BTN_SCAN_PERIOD);
|
|
|
/* USER CODE END 2 */
|
|
|
|
|
|
/* USER CODE BEGIN WHILE */
|
|
@@ -202,64 +222,44 @@ int main(void)
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
- uint8_t temp_l, temp_h, hum_h, hum_l, pres_h, pres_l;
|
|
|
- uint32_t tmp;
|
|
|
+ es_event_t event = eventNull;
|
|
|
+ COLOR_RGB(0xFF, 0x12, 0x0); // Nixie color. FF7E00 or FFBF00
|
|
|
+ showTime();
|
|
|
|
|
|
/* Infinite loop */
|
|
|
while (1)
|
|
|
{
|
|
|
- IN15_OFF;
|
|
|
- COLOR_RGB(0, 0, 0);
|
|
|
- RTC_ReadAll(&Clock);
|
|
|
- if (Flag.BME280 != 0) {
|
|
|
- rsltSensor = bme280_set_sensor_mode(BME280_FORCED_MODE, &SensorDev);
|
|
|
- }
|
|
|
- tdelay_ms(500);
|
|
|
-
|
|
|
+ /* new second interrupt from RTC */
|
|
|
if (Flag.RTC_IRQ != 0) {
|
|
|
Flag.RTC_IRQ = 0;
|
|
|
- IN15_Minus;
|
|
|
- COLOR_RGB(0xFF, 0x12, 0x0); // Nixie color. FF7E00 or FFBF00
|
|
|
- }
|
|
|
- if (Flag.BME280 != 0) {
|
|
|
- rsltSensor = bme280_get_sensor_data(BME280_ALL, &SensorData, &SensorDev);
|
|
|
- }
|
|
|
- tdelay_ms(500);
|
|
|
+
|
|
|
+ RTC_ReadAll(&Clock);
|
|
|
+
|
|
|
+ ES_PlaceEvent(evNewSecond);
|
|
|
+
|
|
|
+ if (dispWDT != 0) {
|
|
|
+ dispWDT --;
|
|
|
+ if (dispWDT == 0) {
|
|
|
+ ES_PlaceEvent(evDisplayWDT);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } /* end of New second */
|
|
|
+
|
|
|
/* USER CODE END WHILE */
|
|
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
|
- if (rsltSensor == BME280_OK) {
|
|
|
- tmp = SensorData.temperature + 50;
|
|
|
- temp_h = (uint8_t)(tmp / 100);
|
|
|
- temp_l = (uint8_t)(tmp % 100);
|
|
|
-
|
|
|
- tmp = SensorData.humidity + 512;
|
|
|
- hum_h = (uint8_t)(tmp / 1024);
|
|
|
- hum_l = (uint8_t)((tmp % 1024) / 10);
|
|
|
-
|
|
|
- /* in 32-bit ariphmetics pressure in Pa */
|
|
|
- tmp = SensorData.pressure * 1000;
|
|
|
- tmp += 66661;
|
|
|
- tmp /= 133322; // pressure in mmHg
|
|
|
- pres_h = (uint8_t)(tmp / 100);
|
|
|
- pres_l = (uint8_t)(tmp % 100);
|
|
|
-
|
|
|
- showDigit(Tube_A, pres_h / 10);
|
|
|
- showDigit(Tube_B, pres_h % 10);
|
|
|
- showDigit(Tube_D, pres_l / 10);
|
|
|
- showDigit(Tube_E, pres_l % 10);
|
|
|
- } else {
|
|
|
- showDigit(Tube_A, Clock.Min >> 4);
|
|
|
- showDigit(Tube_B, Clock.Min & 0xf);
|
|
|
- showDigit(Tube_D, Clock.Sec >> 4);
|
|
|
- showDigit(Tube_E, Clock.Sec & 0xf);
|
|
|
+ event = ES_GetEvent();
|
|
|
+ if (event) {
|
|
|
+ ES_Dispatch(event);
|
|
|
}
|
|
|
|
|
|
- SPI_StartTX();
|
|
|
-
|
|
|
RTOS_DispatchTask();
|
|
|
+
|
|
|
__WFI();
|
|
|
}
|
|
|
/* USER CODE END 3 */
|
|
@@ -270,7 +270,7 @@ int main(void)
|
|
|
* @brief Launch SPI transaction.
|
|
|
* @retval None
|
|
|
*/
|
|
|
-static void SPI_StartTX(void) {
|
|
|
+static void tubes_Refresh(void) {
|
|
|
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
|
|
|
}
|
|
|
|
|
@@ -290,6 +290,7 @@ int8_t i2c_check_err(void) {
|
|
|
}
|
|
|
|
|
|
if (r != I2C_RET_OK) {
|
|
|
+ /* restart I2C and clear flags */
|
|
|
I2C1->CR1 &= ~I2C_CR1_PE;
|
|
|
while ((I2C1->CR1 & I2C_CR1_PE) != 0) {};
|
|
|
I2C1->CR1 |= I2C_CR1_PE;
|
|
@@ -396,6 +397,33 @@ int8_t user_i2c_write(const uint8_t id, const uint8_t reg_addr, uint8_t *data, c
|
|
|
return r;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Sensor
|
|
|
+ */
|
|
|
+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 System Clock Configuration
|
|
|
* @retval None
|
|
@@ -651,9 +679,9 @@ static void MX_TIM3_Init(void)
|
|
|
/* USER CODE BEGIN TIM3_Init 1 */
|
|
|
|
|
|
/* USER CODE END TIM3_Init 1 */
|
|
|
- TIM_InitStruct.Prescaler = 24;
|
|
|
+ TIM_InitStruct.Prescaler = 94;
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
- TIM_InitStruct.Autoreload = 1000;
|
|
|
+ TIM_InitStruct.Autoreload = 255;
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
LL_TIM_Init(TIM3, &TIM_InitStruct);
|
|
|
LL_TIM_EnableARRPreload(TIM3);
|
|
@@ -661,7 +689,7 @@ static void MX_TIM3_Init(void)
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
|
- TIM_OC_InitStruct.CompareValue = 100;
|
|
|
+ TIM_OC_InitStruct.CompareValue = 25;
|
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
|
LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
|
LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
|
|
@@ -1142,6 +1170,191 @@ static void showDigit(tube_pos_t pos, uint8_t dig)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Îáðàáîòêà êíîïîê.
|
|
|
+ * @param : None
|
|
|
+ * @retval : None
|
|
|
+ */
|
|
|
+static void btnProcess(void) {
|
|
|
+ /* get pin state */
|
|
|
+ uint32_t pins = BTN_PORT->IDR & BTN_PINS;
|
|
|
+
|
|
|
+ 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 */
|
|
|
+}
|
|
|
+
|
|
|
+void in15Off(void) {
|
|
|
+ IN15_OFF;
|
|
|
+}
|
|
|
+
|
|
|
+void in15Minus(void) {
|
|
|
+ IN15_OFF;
|
|
|
+ IN15_Minus;
|
|
|
+}
|
|
|
+
|
|
|
+void in15Plus(void) {
|
|
|
+ IN15_OFF;
|
|
|
+ IN15_Plus;
|
|
|
+}
|
|
|
+
|
|
|
+void in15Percent(void) {
|
|
|
+ IN15_OFF;
|
|
|
+ IN15_Percent;
|
|
|
+}
|
|
|
+
|
|
|
+void in15P(void) {
|
|
|
+ IN15_OFF;
|
|
|
+ IN15_P;
|
|
|
+}
|
|
|
+
|
|
|
+void showTime(void) {
|
|
|
+ in15Minus();
|
|
|
+ RTOS_SetTask(in15Off, 500, 0);
|
|
|
+
|
|
|
+ showDigit(Tube_A, Clock.Hr >> 4);
|
|
|
+ showDigit(Tube_B, Clock.Hr & 0xf);
|
|
|
+ showDigit(Tube_D, Clock.Min >> 4);
|
|
|
+ showDigit(Tube_E, Clock.Min & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showWD(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ IN15_OFF;
|
|
|
+
|
|
|
+ showDigit(Tube_A, 0xf);
|
|
|
+ showDigit(Tube_B, Clock.WD & 0xf);
|
|
|
+ showDigit(Tube_D, 0xf);
|
|
|
+ showDigit(Tube_E, 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showDay(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ IN15_OFF;
|
|
|
+
|
|
|
+ showDigit(Tube_A, Clock.Day >> 4);
|
|
|
+ showDigit(Tube_B, Clock.Day & 0xf);
|
|
|
+ showDigit(Tube_D, 0xf);
|
|
|
+ showDigit(Tube_E, 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showMonth(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ IN15_OFF;
|
|
|
+
|
|
|
+ showDigit(Tube_A, 0xf);
|
|
|
+ showDigit(Tube_B, 0xf);
|
|
|
+ showDigit(Tube_D, Clock.Mon >> 4);
|
|
|
+ showDigit(Tube_E, Clock.Mon & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showDayMon(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ IN15_OFF;
|
|
|
+
|
|
|
+ showDigit(Tube_A, Clock.Day >> 4);
|
|
|
+ showDigit(Tube_B, Clock.Day & 0xf);
|
|
|
+ showDigit(Tube_D, Clock.Mon >> 4);
|
|
|
+ showDigit(Tube_E, Clock.Mon & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showYear(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ IN15_OFF;
|
|
|
+
|
|
|
+ showDigit(Tube_A, 2);
|
|
|
+ showDigit(Tube_B, 0);
|
|
|
+ showDigit(Tube_D, Clock.Year >> 4);
|
|
|
+ showDigit(Tube_E, Clock.Year & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showHumidity(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ in15Percent();
|
|
|
+
|
|
|
+ showDigit(Tube_A, Humidity >> 4);
|
|
|
+ showDigit(Tube_B, Humidity & 0xf);
|
|
|
+ showDigit(Tube_D, 0xf);
|
|
|
+ showDigit(Tube_E, 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showTemperature(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ in15Plus();
|
|
|
+
|
|
|
+ showDigit(Tube_A, 0xf);
|
|
|
+ showDigit(Tube_B, 0xf);
|
|
|
+ showDigit(Tube_D, Temperature >> 4);
|
|
|
+ showDigit(Tube_E, Temperature & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+void showPressure(void) {
|
|
|
+ dispWDT = DISP_WDT_TIME;
|
|
|
+ in15P();
|
|
|
+
|
|
|
+ showDigit(Tube_A, 0xf);
|
|
|
+ showDigit(Tube_B, Pressure.s16.u8H & 0xf);
|
|
|
+ showDigit(Tube_D, Pressure.s16.u8L >> 4);
|
|
|
+ showDigit(Tube_E, Pressure.s16.u8L & 0xf);
|
|
|
+
|
|
|
+ tubes_Refresh();
|
|
|
+}
|
|
|
+
|
|
|
+/* 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 */
|
|
|
|
|
|
/**
|