1413 views|0 replies

6822

Posts

11

Resources
The OP
 

[STM32U599J-DK] Smart watch one RTC time and STTS22H data acquisition and display [Copy link]

【Purpose】
Real-time collection and display of date, time and temperature.
【Experimental Hardware】
STM32U599J-DK development board and onboard stts22h thermometer
【Development environment】
TouchGFX4.22
Stm32CubeIDE1.14.1
【Model diagram】
【Implementation steps】
  1. To obtain temperature, configure i2c3 as the interface of stts22h.
  2. Configure RTC real-time time:

    Turn on the RTC clock and calendar.
  3. Enable LSE clock:
  4. Add custom code in RTC_init, mainly to implement the restart without writing the default time.
    /**
    * @brief RTC Initialization Function
    * @param None
    * @retval None
    */
    static void MX_RTC_Init ( void )
    {
    /* USER CODE BEGIN RTC_Init 0 */
    RTC_DateTypeDef datebuff; //Date structure parameter ???
    /* USER CODE END RTC_Init 0 */
    RTC_Privi legeStateTypeDef privilegeState = {0};
    RTC_TimeTypeDef sTime = {0};
    RTC_DateTypeDef sDate = {0};
    /* USER CODE BEGIN RTC_Init 1 */
    __HAL_RCC_PWR_CLK_ENABLE();
    /* USER CODE END RTC_Init 1 */
    /** Initialize RTC Only
    */
    hrtc.Instance = RTC;
    .Init.HourFormat = RTC_HOURFORMAT_24;
    hrtc.Init.AsynchPrediv = 127;
    hrtc.Init.SynchPrediv = 255; hrtc.Init.OutPut =
    RTC_OUTPUT_DISABLE; hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; hrtc.Init.OutPutPolarity = RTC_OUTPUT_POL ARITY_HIGH; hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE; hrtc.Init.BinMode = RTC_BINARY_NONE; if ( HAL_RTC_Init (&hrtc) != HAL_OK ) { Error_Handler(); } privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO; privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE; privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0; privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0; if ( HAL_RTCEx_PrivilegeModeSet (& hrtc, &privilegeState) != HAL_OK ) { Error_Handler(); } /* USER CODE BEGIN Check_RTC_BKUP */ if ( HAL_RTCEx_BKUPRead (&hrtc,RTC_BKP_DR1)!= 0x5051) { /* USER CODE END Check_RTC_BKUP */ /** Initialize RTC and set the Time and Date */ sTime. Hours = 0x7; sTime.Minutes = 0x15; sTime.Seconds = 0x10; sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; sTime.StoreOperation = RTC_STOREOPERATION_RESET; if ( HAL_RTC_SetTime (&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK ) { Error_Handler(); sDate.WeekDay = RTC_WEEKDAY_FRIDAY; sDate.Month = RTC_MONTH_DECEMBER; sDate.Date = 0x7; sDate.Year = 0x23; if ( HAL_RTC_SetDate




































    (&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK )
    {
    Error_Handler();
    }
    /* USER CODE BEGIN RTC_Init 2 */
    datebuff = sDate; //Copy the date data to the defined data register.
    HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR1, 0x5051);//Write the data to the specified backup area register.
    HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR2, (uint16_t)datebuff.Year);
    HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR3, (uint16_t)datebuff.Month);
    HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR4, (uint16_t)datebuff.Date);
    HAL_RTCEx_BKUPWrite (&hrtc, RTC_BKP_DR5, (uint16_t)datebuff.WeekDay);
    }
    else
    {
    datebuff.Year = HAL_RTCEx_BKUPRead (&hrtc, RTC_BKP_DR2);
    datebuff.Month = HAL_RTCEx_BKUPRead (&hrtc, R TC_BKP_DR3);
    datebuff.Date = HAL_RTCEx_BKUPRead (&hrtc, RTC_BKP_DR4);
    datebuff.WeekDay = HAL_RTCEx_BKUPRead (&hrtc, RTC_BKP_DR5);
    sDate = datebuff;
    if ( HAL_RTC_SetDate (&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK )
    {
    Error_Handler();
    }
    }
    /* USER CODE END RTC_Init 2 */
    }
  5. Add stts22h driver library stts22h_reg.c/h:
  6. Add a semaphore in stm32CubeMax to communicate with the model:
  7. In the freertos task, we add code to periodically obtain temperature and time, mainly to read the temperature value, date, time, and send a semaphore so that the model can receive the semaphore and display it.
    /* USER CODE END Header_StartDefaultTask */
    void StartDefaultTask ( void *argument)
    {
    /* USER CODE BEGIN defaultTask */
    static int16_t data_raw_temperature;
    static uint8_t whoamI;
    stmdev_ctx_t dev_ctx;
    dev_ctx.write_reg = platform_write;
    dev_ctx.read_reg = platform_read;
    dev_ctx.hand le = &SENSOR_BUS;
    /* Check device ID */
    stts22h_dev_id_get (&dev_ctx, &whoamI);
    if (whoamI != STTS22H_ID)
    while (1)
    {
    osDelay (10);
    }
    /* Infinite loop */
    stts22h_temp_data_rate_set (&dev_ctx, STTS22H_1Hz );
    for (;;)
    {
    uint8_t flag;
    stts22h_temp_flag_data_ready_get (&dev_ctx, &flag);
    if (flag) {
    /* Read temperature data */
    memset (&data_raw_temperature, 0, sizeof (int16_t));
    stts22h_temperature_raw_get (&dev_ctx, &data_raw_temperature);
    temperature_degC = stts22h_from_lsb_to_cels ius (
    data_raw_temperature);
    HAL_RTC_GetTime (&hrtc, &GetTime, RTC_FORMAT_BIN);
    HAL_RTC_GetDate (&hrtc, &GetData, RTC_FORMAT_BIN);
    osSemaphoreRelease (myBinarySemtempHandle);
    osDelay (1000);
    }
    }
    /* USER CODE END defaultTask */
    }
  8. Add a virtual function in modelListener.hpp and implement it in model.cpp:
    virtual void NotifyTempValudChanged ( float value, RTC_TimeTypeDef GetTime, RTC_DateTypeDef GetData) {}
    void Model::tick ()
    {
    if ( osSemaphoreGetCount (myBinarySemtempHandle)>0)
    {
    modelListener->NotifyTempValudChanged(temperature_degC,
    GetTime, GetData);
    }
    }
  9. Construct the virtual function NotifyTempValudChanged in screen1Presenter.hpp and implement it in screen1Presenter.cpp:
    virtual void NotifyTempValudChanged ( float value, RTC_TimeTypeDef GetTime, RTC_DateTypeDef GetData);
    void Screen1Presenter::NotifyTempValudChanged ( float value,
    RTC_TimeTypeDef GetTime, RTC_DateTypeDef GetData)
    {
    view. setTempValud (value, GetTime, GetData);
    }
  10. Construct the virtual function setTempValud in screen1View.hpp and implement it in screen1View.cpp:
    virtual void setTempValud ( float value, RTC_TimeTypeDef GetTime, RTC_DateTypeDef GetData);
    void Screen1View::setTempValud ( float value,
    RTC_TimeTypeDef GetTime, RTC_DateTypeDef GetData)
    {
    Unicode: : snprintfFloat (textAreaTempBuffer, TEXTAREATEMP_SIZE , "%2.2f", value);
    Unicode:: snprintf (textAreaDataBuffer, TEXTAREADATA_SIZE , "%04d-%02d-%02d",
    2000 + GetData.Year, GetData.Month, GetData.Date) ;
    Unicode:: snprintf (textAreaTimeBuffer, TEXTAREATIME_SIZE ,"%02d:%02d:%02d",
    GetTime.Hours, GetTime.Minutes, GetTime.Seconds);
    textAreaData. invalidate ();
    textAreaTime. invalidate ();
    textAreaTemp. invalidate ();
    }
  11. Create the display interface in touchGFX Designer:
【Procedure Effect】
【Summarize】
The Stm32U599J-DK development board has a built-in thermometer, and the stm32u599J comes standard with an RTC peripheral, which can achieve accurate output and configuration of date and time.
In this project, we mainly implemented the MPV design pattern to achieve separate development of the background and the interface.
This post is from stm32/stm8
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list