• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <time.h>
5 #include "stm32l4xx_hal.h"
6 #include "usb_device.h"
7 #include "StmUtil.h"
8 
9 // RTC initialized by MX_RTC_Init
10 extern RTC_HandleTypeDef hrtc;
11 
12 typedef unsigned char DEVICE_UNIQUE_ID_T[12];
13 #define DEVICE_UNIQUE_ID (*(DEVICE_UNIQUE_ID_T*)(UID_BASE))
14 #define DEVICE_FLASH_SIZE (*(uint16_t *)(FLASHSIZE_BASE))
15 #define DEVICE_TYPE (*(uint16_t *) (DBGMCU->IDCODE & 0x00000fff))
16 #define DEVICE_REV (*(uint16_t *) (DBGMCU->IDCODE >> 16))
17 char __attribute__((section (".ram2"))) logStampStr[40] = {0};
18 void* __attribute__((section (".ram2"))) g_itm[ITMCHANNELS] = {0};
19 
20 GPIO_PinState BlueButtonLast = GPIO_PIN_SET;
BlueButtonTransitionDetected(void)21 int BlueButtonTransitionDetected(void)
22 {
23     GPIO_PinState PPButton = HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
24     if((PPButton == GPIO_PIN_RESET) && (BlueButtonLast == GPIO_PIN_SET))
25     {
26         // Now pressed
27         BlueButtonLast = PPButton;
28         return 1;
29     }
30     else if((PPButton == GPIO_PIN_SET) && (BlueButtonLast == GPIO_PIN_RESET))
31     {
32         // Now released
33         BlueButtonLast = PPButton;
34         return -1;
35     }
36     // No change
37     return 0;
38 }
39 
40 #ifndef NDEBUG
41 #define ITM_PORT_BITS (0xffffffff)
InitializeITM()42 void InitializeITM()
43 {
44 //    CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk; /* enable trace in core debug */
45 //    ITM->TCR = ITM_TCR_TraceBusID_Msk | ITM_TCR_SWOENA_Msk | ITM_TCR_SYNCENA_Msk | ITM_TCR_ITMENA_Msk; /* ITM Trace Control Register */
46 //    ITM->TPR = ITM_TPR_PRIVMASK_Msk; /* ITM Trace Privilege Register */
47 //    ITM->TER = ITM_PORT_BITS; /* ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port. */
48 //    *((volatile unsigned *)(ITM_BASE + 0x01000)) = 0x400003FE; /* DWT_CTRL */
49 //    *((volatile unsigned *)(ITM_BASE + 0x40304)) = 0x00000100; /* Formatter and Flush Control Register */
50 
51     for(uint32_t n = 0; n < ITMCHANNELS; n++)
52     {
53         char fileName[10];
54         sprintf(fileName, "ITM[%02u]", (unsigned int)n);
55         g_itm[n] = (void*)fopen(fileName, "wb");
56     }
57 }
58 
ITM_Out(uint32_t port,uint8_t ch)59 void ITM_Out(uint32_t port, uint8_t ch)
60 {
61     while(ITM->PORT[port].u32 == 0);
62     ITM->PORT[port].u8 = ch;
63 }
64 #endif
65 
SetDutyCycleIndicator(bool on)66 void SetDutyCycleIndicator(bool on)
67 {
68     HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, on ? GPIO_PIN_SET : GPIO_PIN_RESET);
69 }
70 
GetLogStamp(void)71 char* GetLogStamp(void)
72 {
73     RTC_TimeTypeDef time = {0};
74     RTC_DateTypeDef date = {0};
75     HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
76     HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
77 
78     sprintf(logStampStr, "%04d.%02d.%02d-%02d:%02d:%02d.%03dGMT",
79                 date.Year + 2000,
80                 date.Month,
81                 date.Date,
82                 time.Hours,
83                 time.Minutes,
84                 time.Seconds,
85                 (int)((1000 / time.SecondFraction) * (time.SecondFraction - time.SubSeconds)));
86     return logStampStr;
87 }
88 
KillUSBLink(void)89 void KillUSBLink(void)
90 {
91     dbgPrint("USB de-initialization...\r\n");
92     MX_USB_DEVICE_DeInit();
93 }
94 
SetRealTimeClock(time_t tm)95 void SetRealTimeClock(time_t tm)
96 {
97     struct tm* local = localtime((time_t*)&tm);
98     RTC_TimeTypeDef time = {0};
99     RTC_DateTypeDef date = {0};
100     date.Year = local->tm_year - 100;
101     date.Month = local->tm_mon + 1;
102     date.Date = local->tm_mday;
103     date.WeekDay = local->tm_wday  + 1;
104     time.Hours = local->tm_hour;
105     time.Minutes = local->tm_min;
106     time.Seconds = local->tm_sec;
107     HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN);
108     HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN);
109 }
110 
ReadMcuInfo(unsigned char * serial,uint16_t * flashSize,uint16_t * mcuType,uint16_t * mcuRev)111 void ReadMcuInfo(unsigned char* serial, uint16_t *flashSize, uint16_t *mcuType, uint16_t *mcuRev)
112 {
113     if(serial)
114     {
115         memcpy(serial, DEVICE_UNIQUE_ID, sizeof(DEVICE_UNIQUE_ID));
116     }
117     if(flashSize)
118     {
119         *flashSize = DEVICE_FLASH_SIZE;
120     }
121     if(mcuType)
122     {
123         *mcuType = DEVICE_TYPE;
124     }
125     if(mcuRev)
126     {
127         *mcuRev = DEVICE_REV;
128     }
129 }
130 
PerformSystemReset(void)131 void PerformSystemReset(void)
132 {
133     dbgPrint("Executing NVIC_SystemReset()...\r\n");
134     HAL_Delay(1);
135     NVIC_SystemReset();
136 }
137