OpenCores
URL https://opencores.org/ocsvn/light52/light52/trunk

Subversion Repositories light52

[/] [light52/] [trunk/] [test/] [common/] [soc.c] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ja_rd
/**
2
    @file soc.c
3
    @brief Hardware-dependent function library.
4
 
5
*/
6
 
7
#include <stdio.h>
8
#include <stdint.h>
9
#include <stdbool.h>
10
#include "../include/light52.h"
11
#include "soc.h"
12
 
13
/*-- Local macros ------------------------------------------------------------*/
14
 
15
/** How many Timer 0 counts are there in a millisecond period */
16
#define TIMER0_COUNTS_PER_MS (uint32_t)((CLOCK_RATE/1000)/TIMER0_PRESCALER)
17
 
18
/*-- Static data -------------------------------------------------------------*/
19
 
20
/**
21
    Counter of seconds elapsed since last call to mcu_init.
22
    Updated by timer 0 irq routine.
23
*/
24
volatile uint32_t seconds;
25
 
26
/*-- Public functions --------------------------------------------------------*/
27
 
28
/*-- SoC ----------------------------------------------------------*/
29
 
30
void soc_init(void){
31
    seconds = 0;
32
    /* Set up timer 0 to trigger an interrupt every second... */
33
    timer0_init(CLOCK_RATE/TIMER0_PRESCALER);
34
    /* ...enable interrupts globally, plus the timer interrupt only... */
35
    timer0_enable_irq(1);
36
    cpu_enable_interrupts(1);
37
    /* ...and (re)start the timer */
38
    timer0_enable(1);
39
}
40
 
41
uint32_t soc_get_msecs(void){
42
    uint32_t msecs;
43
    /* First, get the elapsed milliseconds of the current second */
44
    msecs = timer0_counter();           /* Get the counter... */
45
    msecs = msecs/TIMER0_COUNTS_PER_MS; /* ...and convert it to milliseconds */
46
    /* Then, add the number of full seconds elapsed */
47
    msecs = msecs + seconds*1000;
48
    return msecs;
49
}
50
 
51
 
52
/*-- CPU ----------------------------------------------------------*/
53
 
54
void cpu_enable_interrupts(uint8_t enable){
55
    EA = enable & 0x01;
56
}
57
 
58
/*-- Timer --------------------------------------------------------*/
59
 
60
void timer0_init(uint16_t reload){
61
    if(reload!=0xffff){
62
        T0CH = (uint8_t)(reload >> 8);
63
        T0CL = (uint8_t)(reload & 0xff);
64
        T0ARL = 1;
65
    }
66
    else{
67
        T0ARL = 0;
68
    };
69
    T0IRQ = 1; /* Clear IRQ flag by writing a 1 on it */
70
}
71
 
72
void timer0_enable(uint8_t enable){
73
    T0CEN = enable & 0x01;
74
}
75
 
76
uint16_t timer0_counter(void){
77
    volatile uint8_t h, l0, l1;
78
    uint16_t value;
79
    bool retried = false;
80
 
81
    /* Read the counter register... */
82
    h = T0H;
83
    l0 = T0L;
84
    l1 = T0L;
85
    /* ...and make sure we didn't catch it incrementing */
86
    if(l0!=l1){
87
        /* If we did, read it again.
88
           Enough if the counter does not run too fast. */
89
        h = T0H;
90
        l0 = T0L;
91
    }
92
 
93
    value = ((uint16_t)h)<<8 | (uint16_t)l0;
94
    return value;
95
}
96
 
97
void timer0_enable_irq(uint8_t enable){
98
    ET0 = enable & 0x01;
99
}
100
 
101
/*-- Interrupt routines ------------------------------------------------------*/
102
 
103
void timer0_isr(void) __interrupt(1) {
104
    static uint8_t q = 0;
105
 
106
    T0IRQ = 1; /* Clear IRQ flag by writing a 1 on it */
107
    P1 = q;
108
    q++;
109
 
110
    seconds++;
111
}
112
 
113
/*-- STDCLIB replacement functions -------------------------------------------*/
114
 
115
/**
116
    Stdclib putchar replacement function.
117
    Relies on polling for simplicity and does CR to CRLF expansion.
118
 
119
    @arg c Character to be displayed. Character '\n' will be expanded to '\n\r'.
120
*/
121
void putchar (char c) {
122
    while (!TXRDY);
123
    SBUF = c;
124
    if(c=='\n'){
125
        while (!TXRDY);
126
        SBUF = '\r';
127
    }
128
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.