Project

General

Profile

Revision 234

RTC functional to retrieve date and time

View differences:

proj/include/errors.h
4 4
/** @brief Error Codes */
5 5
enum errors {
6 6
    SUCCESS = 0,        /** @brief Sucessful */
7
    NULL_PTR,           /** @brief Null Pointer Error */
7
    NULL_PTR = 250,     /** @brief Null Pointer Error */
8 8
    LCF_ERROR,          /** @brief Error originated on LCF */
9 9
    SBCR_ERROR,         /** @brief Error on Subscribing Interrupt */
10 10
    UNSBCR_ERROR,       /** @brief Error on Unsubscring Interrupt*/
proj/include/rtc.h
1
#ifndef RTC_H_INCLUDED
2
#define RTC_H_INCLUDED
3

  
4
#include <stdint.h>
5

  
6
/**
7
 * @brief Subscribes RTC Interrupts
8
 * @param interrupt_bit Bit of Interrupt Vector that will be set when RTC Interrupt is pending
9
 * @param interrupt_id RTC Interrupt ID to specify the RTC Interrupt in other calls
10
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
11
 * @see {_ERRORS_H_::errors}
12
 */
13
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
14

  
15
int (rtc_read_register)(uint32_t reg, uint8_t *data);
16

  
17
int (rtc_write_register)(uint32_t reg, uint8_t data);
18

  
19
/**
20
 * @brief Checks if there's an update in progress.
21
 * @return  The value of the flag UIP, or ERROR_CODE if error occurs.
22
 */
23
int (rtc_check_update)(void);
24

  
25
/**
26
 * @brief Enables/Disables updates of time/date registers.
27
 * @param   on  zero to disable, any other value to enable
28
 * @return  ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
29
 */
30
int (rtc_set_updates)(int on);
31

  
32
/**
33
 * @brief Reads time from RTC.
34
 * @param   time  Pointer to array of 3 bytes to store the information about time (hours, minutes, seconds)
35
 * @return  ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
36
 */
37
int (rtc_read_time)(uint8_t *time);
38

  
39
/**
40
 * @brief Reads date from RTC.
41
 * @param   date  Pointer to array of 4 bytes to store the information about date (year, month, day, weekday)
42
 * @return  ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
43
 */
44
int (rtc_read_date)(uint8_t *date);
45

  
46
#endif /* end of include guard: RTC_H_INCLUDED */
0 47

  
proj/include/rtc_macros.h
1
#ifndef RTC_MACROS_H_INCLUDED
2
#define RTC_MACROS_H_INCLUDED
3

  
4
#include <lcom/lcf.h>
5

  
6
// RTC IRQ Line
7

  
8
#define KBC_IRQ   8  /* @brief KBC IRQ Line */
9

  
10
// RTC Ports
11

  
12
#define RTC_ADDR_REG        0x70
13
#define RTC_DATA_REG        0x71
14

  
15
// RTC Registers (Byte)
16

  
17
#define RTC_SEC             0
18
#define RTC_SEC_ALARM       1
19
#define RTC_MIN             2
20
#define RTC_MIN_ALARM       3
21
#define RTC_HOUR            4
22
#define RTC_HOUR_ALARM      5
23
#define RTC_WEEK_DAY        6
24
#define RTC_MONTH_DAY       7
25
#define RTC_MONTH           8
26
#define RTC_YEAR            9
27
#define RTC_REG_A           10
28
#define RTC_REG_B           11
29
#define RTC_REG_C           12
30
#define RTC_REG_D           13
31

  
32
// Register A
33

  
34
#define RS0     BIT(0)  /** @brief Rate selector */
35
#define RS1     BIT(1)  /** @brief Rate selector */
36
#define RS2     BIT(2)  /** @brief Rate selector */
37
#define RS3     BIT(3)  /** @brief Rate selector */
38
#define UIP     BIT(7)  /** @brief Update in progress */
39

  
40
// Register B
41

  
42
#define DSE             BIT(0)  /** @brief Enable Daylight Saving Time */
43
#define HOUR_FORM       BIT(1)  /** @brief Set to 1 for 24h format (0-23), 0 to 12h format (1-12) */
44
#define DM              BIT(2)  /** @brief Set to 1 if registers are in binary, 0 if in BCD */
45
#define SQWE            BIT(3)  /** @brief Enable square wave generation */
46
#define UIE             BIT(4)  /** @brief Enable Update interrupts */
47
#define AIE             BIT(5)  /** @brief Enable alarm interrupts */
48
#define PIE             BIT(6)  /** @brief Enable periodic interrupts */
49
#define SET             BIT(7)  /** @brief Set to 1 to inhibit updates of time/date registers */
50

  
51
// Register C
52

  
53
#define UF      BIT(4)  /** @brief Update Interrupt Pending */
54
#define AF      BIT(5)  /** @brief Alarm Interrupt Pending */
55
#define PF      BIT(6)  /** @brief Periodic Interrupt Pending */
56
#define IRQF    BIT(7)  /** @brief IRQ Line Active */
57

  
58
// Register D
59

  
60
#define VRT     BIT(7)  /** @brief Valid RAM/time - If set to 0 RTC reading aren't valid */
61

  
62
#endif /* end of include guard: RTC_MACROS_H_INCLUDED */
0 63

  
proj/include/utils.h
1 1
#ifndef UTILS_H_INCLUDED
2 2
#define UTILS_H_INCLUDED
3 3

  
4
#define BCD_FIRST(n)     (n >> 4)       /** @brief Get first digit (leftmost digit) of 8-bit BCD */
5
#define BCD_SECOND(n)    (n & 0x0F)     /** @brief Get second digit (rightmost digit) of 8-bit BCD */
6

  
4 7
/**
5 8
 * @brief Gets the least significant byte of a 16-bit variable
6 9
 * @param val 16-bit variable
proj/src/proj.c
11 11
#include "keyboard.h"
12 12
#include "mouse.h"
13 13
#include "graph.h"
14
#include "rtc.h"
14 15
#include "interrupts_func.h"
15 16

  
16 17
#include "graph.h"
......
84 85
    }
85 86

  
86 87
    #ifdef DIOGO
88
        /*
87 89
        graph_clear_screen();
88 90

  
89 91
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
......
132 134
        if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
133 135

  
134 136

  
135
        tickdelay(micros_to_ticks(1000000));
137
        tickdelay(micros_to_ticks(1000000));*/
136 138

  
139
        // RTC
140
        uint8_t date[4], time[3];
141

  
142
        if (rtc_read_time(time)) return 1;
143

  
144
        if (rtc_read_date(date)) return 1;
145

  
146
        printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
147

  
148
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
137 149
    #endif
138 150

  
139 151
    #ifdef TELMO
proj/src/rtc.c
1
#include "rtc.h"
2
#include "rtc_macros.h"
3

  
4
#include <lcom/lcf.h>
5

  
6
#include "utils.h"
7
#include "errors.h"
8

  
9
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
10
    if (interrupt_id == NULL) return NULL_PTR;
11
    *interrupt_id = interrupt_bit;
12
    if (sys_irqsetpolicy(KBC_IRQ, IRQ_REENABLE | IRQ_EXCLUSIVE, interrupt_id)) return SBCR_ERROR;
13
    return SUCCESS;
14
}
15

  
16
int (rtc_read_register)(uint32_t reg, uint8_t *data) {
17
    if (sys_outb(RTC_ADDR_REG, reg)) return WRITE_ERROR;
18

  
19
    if (util_sys_inb(RTC_DATA_REG, data)) return READ_ERROR;
20

  
21
    return SUCCESS;
22
}
23

  
24
int (rtc_write_register)(uint32_t reg, uint8_t data) {
25
    if (sys_outb(RTC_ADDR_REG, reg)) return WRITE_ERROR;
26

  
27
    if (sys_outb(RTC_DATA_REG, data)) return WRITE_ERROR;
28

  
29
    return SUCCESS;
30
}
31

  
32
int (rtc_check_update)(void) {
33
    uint8_t data;
34
    int r;
35

  
36
    if ((r = rtc_read_register(RTC_REG_A, &data))) return r;
37

  
38
    return (data & UIP) != 0;
39
}
40

  
41
int (rtc_set_updates)(int on) {
42
    uint8_t data;
43
    int r;
44
    if (on) {
45
        if ((r = rtc_read_register(RTC_REG_B, &data))) return r;
46

  
47
        data &= ~SET;
48

  
49
        if ((r = rtc_write_register(RTC_REG_B, data))) return r;
50

  
51
    } else {
52
        while (rtc_check_update());
53

  
54
        if ((r = rtc_read_register(RTC_REG_B, &data))) return r;
55

  
56
        data |= SET;
57

  
58
        if ((r = rtc_write_register(RTC_REG_B, data))) return r;
59
    }
60
    return SUCCESS;
61
}
62

  
63
int (rtc_read_time)(uint8_t *time) {
64

  
65
    int r;
66
    //if ((r = rtc_set_updates(false))) return r;
67
    while (rtc_check_update());
68

  
69
    uint8_t hour, min, sec;
70

  
71
    if ((r = rtc_read_register(RTC_SEC, &sec)))    return r;
72
    if ((r = rtc_read_register(RTC_MIN, &min)))    return r;
73
    if ((r = rtc_read_register(RTC_HOUR, &hour)))  return r;
74

  
75
    //if ((r = rtc_set_updates(true))) return r;
76

  
77
    time[0] = BCD_FIRST(sec)*10   + BCD_SECOND(sec);
78
    time[1] = BCD_FIRST(min)*10   + BCD_SECOND(min);
79
    time[2] = BCD_FIRST(hour)*10  + BCD_SECOND(hour);
80

  
81
    return SUCCESS;
82
}
83

  
84
int (rtc_read_date)(uint8_t *date) {
85
    int r;
86
    //if ((r = rtc_set_updates(false))) return r;
87
    while (rtc_check_update());
88

  
89
    uint8_t year, month, day, weekday;
90

  
91
    if ((r = rtc_read_register(RTC_WEEK_DAY,    &weekday)))     return r;
92
    if ((r = rtc_read_register(RTC_MONTH_DAY,   &day)))         return r;
93
    if ((r = rtc_read_register(RTC_MONTH,       &month)))       return r;
94
    if ((r = rtc_read_register(RTC_YEAR,        &year)))        return r;
95

  
96
    //if ((r = rtc_set_updates(true))) return r;
97

  
98
    date[0] = weekday;
99
    date[1] = BCD_FIRST(day)*10     + BCD_SECOND(day);
100
    date[2] = BCD_FIRST(month)*10   + BCD_SECOND(month);
101
    date[3] = BCD_FIRST(year)*10    + BCD_SECOND(year);
102

  
103
    return SUCCESS;
104
}
0 105

  
proj/DR.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
8 8

  
proj/Makefile
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
8 8

  
proj/TB.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
8 8

  

Also available in: Unified diff