A simple alarm clock for the 1 day workshop presented by Skool and ARM Hungary in 2015.

Dependencies:   Skool_wkshp_lib2015 mbed

Revision:
1:f76b625bd36e
Parent:
0:28b9efbdeffc
Child:
2:e084bab7bc1c
diff -r 28b9efbdeffc -r f76b625bd36e mbed_clock.cpp
--- a/mbed_clock.cpp	Mon Sep 14 20:10:47 2015 +0000
+++ b/mbed_clock.cpp	Tue Sep 15 21:53:26 2015 +0000
@@ -25,10 +25,13 @@
 //const int nrows = 4;
 const int ncols = 3;
 const char BCD2HEX[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+const char DOW[7][3] = { {'H',' ',' '}, {'K',' ',' '}, {'S','z','e'}, {'C','s',' '}, {'P',' ',' '}, {'S','z','o'}, {'V',' ',' '} };
 
 uint32_t Index = 0xFF;
 uint32_t new_key = 0;
 uint32_t Systck_cnt = 0;
+uint32_t Systck_evt = 0;
+GPIO_InitTypeDef keypadInit;
 
 void Error(int err) {
     switch (err) {
@@ -69,8 +72,9 @@
         __HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_9);
         HAL_GPIO_EXTI_Callback(3);
     }
-    NVIC_ClearPendingIRQ(EXTI9_5_IRQn);
     HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_RESET);  // All cols are driven Low for resume IRQ operation
+    //NVIC_ClearPendingIRQ(EXTI9_5_IRQn);
+    HAL_NVIC_ClearPendingIRQ(EXTI9_5_IRQn);
 }
 
 void HAL_GPIO_EXTI_Callback(uint16_t pin) {
@@ -107,6 +111,7 @@
 
 void HAL_SYSTICK_Callback(void) {
     Systck_cnt++;
+    Systck_evt++;
 }
 
 int write_ser_lcd(unsigned char data, bool mode) {
@@ -202,8 +207,8 @@
 int main() {
     uint32_t RGB_sel = 0;
     uint32_t currentHclk;
+    uint32_t dummy;
 
-    GPIO_InitTypeDef keypadInit;
 
     HAL_StatusTypeDef HAL_status;
     HAL_LockTypeDef HAL_lock;
@@ -221,16 +226,63 @@
     G_LED = 1;
     B_LED = 1;
 
+    rtc_init.AsynchPrediv = 0x7F;
+    rtc_init.SynchPrediv = 0xFF;
+    rtc_init.HourFormat = RTC_HOURFORMAT_24;
+    rtc_init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
+    rtc_init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
+    rtc_init.OutPut = RTC_OUTPUT_DISABLE;
+
+    rtc_date.Date = 13;
+    rtc_date.Month = 9;
+    rtc_date.WeekDay = 7;
+    rtc_date.Year = 15;
+    tmp_date = 13;
+
+    rtc_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+    rtc_time.Hours = 23;
+    rtc_time.Minutes = 58;
+    rtc_time.Seconds = 46;
+    rtc_time.StoreOperation =RTC_STOREOPERATION_SET;
+    tmp_sec = 0;
+
+    rtch.Instance = RTC;
+    rtch.Init = rtc_init;
+    rtch.Lock = HAL_lock;
+    rtch.State = rtc_state;
+
+
+    __PWR_CLK_ENABLE();
+    HAL_PWR_EnableBkUpAccess();
+//    __HAL_RTC_WRITEPROTECTION_DISABLE(&rtch);
+    __HAL_RCC_LSE_CONFIG(RCC_LSE_ON);
+    __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
+    dummy = RCC->BDCR;
+    dummy |= RCC_BDCR_RTCEN;
+    RCC->BDCR = dummy;
+//    __HAL_RCC_RTC_ENABLE();
+    HAL_status = HAL_RTC_Init(&rtch);
+    pc.printf("HAL_Status: %d\r\n",HAL_status);
+    HAL_status = HAL_RTC_SetDate(&rtch, &rtc_date, FORMAT_BIN);
+    pc.printf("HAL_Status: %d\r\n",HAL_status);
+    HAL_status = HAL_RTC_SetTime(&rtch, &rtc_time, FORMAT_BIN);
+    pc.printf("HAL_Status: %d\r\n",HAL_status);
+
     __GPIOC_CLK_ENABLE();
     keypadInit.Pin = cols[0] | cols[1] | cols[2];
     keypadInit.Mode = GPIO_MODE_OUTPUT_OD;
     keypadInit.Pull = GPIO_NOPULL;
     keypadInit.Speed = GPIO_SPEED_MEDIUM;
     HAL_GPIO_Init(GPIOC, &keypadInit);
-    
-    HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_RESET);
+
+    HAL_GPIO_WritePin(GPIOC, cols[0] | cols[1] | cols[2], GPIO_PIN_SET);
+    HAL_GPIO_WritePin(GPIOC, cols[2], GPIO_PIN_RESET);
 
     keypadInit.Pin = rows[0] | rows[1] | rows[2] | rows[3];
+    keypadInit.Mode = GPIO_MODE_INPUT;
+    keypadInit.Pull = GPIO_PULLUP;
+    HAL_GPIO_Init(GPIOC, &keypadInit);
+    keypadInit.Pin = rows[3];
     keypadInit.Mode = GPIO_MODE_IT_FALLING;
     keypadInit.Pull = GPIO_PULLUP;
     HAL_GPIO_Init(GPIOC, &keypadInit);
@@ -240,42 +292,6 @@
     NVIC_SetVector(SysTick_IRQn, (uint32_t)SysTick_Handler);
     HAL_SYSTICK_Config(14400000);
 
-    rtc_init.AsynchPrediv = 0x7F;
-    rtc_init.SynchPrediv = 0xFF;
-    rtc_init.HourFormat = RTC_HOURFORMAT_24;
-    rtc_init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
-    rtc_init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
-    rtc_init.OutPut = RTC_OUTPUT_DISABLE;
-    
-    rtc_date.Date = 13;
-    rtc_date.Month = 9;
-    rtc_date.WeekDay = 7;
-    rtc_date.Year = 15;
-    tmp_date = 13;
-    
-    rtc_time.DayLightSaving = RTC_DAYLIGHTSAVING_ADD1H;
-    rtc_time.Hours = 22;
-    rtc_time.Minutes = 37;
-    rtc_time.Seconds = 00;
-    rtc_time.StoreOperation =RTC_STOREOPERATION_SET;
-    tmp_sec = 0;
-    
-    rtch.Instance = RTC;
-    rtch.Init = rtc_init;
-    rtch.Lock = HAL_lock;
-    rtch.State = rtc_state;
-    
-    
-    __PWR_CLK_ENABLE();
-    HAL_PWR_EnableBkUpAccess();
-    __HAL_RTC_WRITEPROTECTION_DISABLE(&rtch);
-    __HAL_RCC_LSE_CONFIG(RCC_LSE_ON);
-    __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
-    HAL_RTC_Init(&rtch);
-    HAL_RTC_SetDate(&rtch, &rtc_date, FORMAT_BIN);
-    HAL_RTC_SetTime(&rtch, &rtc_time, FORMAT_BIN);
-    __HAL_RCC_RTC_ENABLE();
-
     __enable_irq();
 
     pc.printf("\f\033[00m\033[33mmbed Clock with STM32F303RE NUCLEO board\033[00m\r\n");
@@ -326,6 +342,7 @@
 
 
         HAL_RTC_GetTime(&rtch, &rtc_time, FORMAT_BCD);
+        HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BCD);
         if (rtc_time.Seconds != tmp_sec) {
             tt[0] = BCD2HEX[((rtc_time.Hours & 0xF0) >> 4)];
             tt[1] = BCD2HEX[(rtc_time.Hours & 0x0F)];
@@ -335,13 +352,39 @@
             tt[5] = ':';
             tt[6] = BCD2HEX[((rtc_time.Seconds & 0xF0) >> 4)];
             tt[7] = BCD2HEX[(rtc_time.Seconds & 0x0F)];
+            tt[8] = 0x00;       // For printf
+            tmp_sec = rtc_time.Seconds;
             write_ser_lcd(0xC0, false);
+            pc.printf("\033[3;0H"); // Move the cursur to the beginning of the 3rd line.
+            pc.printf(tt);
             write_ser_text((unsigned char *)tt, 8);
-            
+            dummy = 0;
+
+            if (rtc_date.Date != tmp_date) {
+                tt[0] = BCD2HEX[2];
+                tt[1] = BCD2HEX[0];
+                tt[2] = BCD2HEX[((rtc_date.Year & 0xF0) >> 4)];
+                tt[3] = BCD2HEX[(rtc_date.Year & 0x0F)];
+                tt[4] = '.';
+                tt[5] = BCD2HEX[((rtc_date.Month & 0xF0) >> 4)];
+                tt[6] = BCD2HEX[(rtc_date.Month & 0x0F)];
+                tt[7] = '.';
+                tt[8] = BCD2HEX[((rtc_date.Date & 0xF0) >> 4)];
+                tt[9] = BCD2HEX[(rtc_date.Date & 0x0F)];
+                tt[10] = '.';
+                tmp_date = rtc_date.Date;
+                tt[11] = ' ';
+                tt[12] = DOW[rtc_date.WeekDay - 1][0];
+                tt[13] = DOW[rtc_date.WeekDay - 1][1];
+                tt[14] = DOW[rtc_date.WeekDay - 1][2];
+                tt[15] = 0x00;       // For printf
+                write_ser_lcd(0x80, false);
+                pc.printf("\033[4;0H"); // Move the cursur to the beginning of the 4th line.
+                pc.printf(tt);
+                write_ser_text((unsigned char *)tt,15);
+            }
         }
         
-        //HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BIN);
-        
         if (Systck_cnt > 5) {
             switch (RGB_sel % 3) {
                 case 0: 
@@ -369,6 +412,13 @@
             Systck_cnt = 0;
         }
         //HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
-        __WFI();
+        //__WFI();
+        while (Systck_evt == 0) {
+            dummy++;
+        }
+        Systck_evt = 0;
     }
 }
+
+
+