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

Subversion Repositories softavrcore

[/] [softavrcore/] [trunk/] [build/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 apal
#define         F_CPU           12000000UL
2
 
3
#include <avr/cpufunc.h>
4
#include <avr/interrupt.h>
5
#include <inttypes.h>
6
#include <util/delay.h>
7
#include <stdio.h>
8
 
9
/*****************************************************************************/
10
 
11
#define         __IOR(x)        (*(volatile uint8_t *)(0x20+(x)))
12
#define         __IOW(x)        (*(volatile uint16_t *)(0x20+(x)))
13
 
14
/*****************************************************************************/
15
 
16
 
17
#define         IO_BASE_UART0           0x00
18
#define         IO_BASE_PORTOUT0        0x04
19
#define         IO_BASE_TIMER0          0x08
20
 
21
/* uart.h */
22
 
23
#define         UDR0            __IOR(IO_BASE_UART0+0x00)
24
#define         UCSRA0          __IOR(IO_BASE_UART0+0x01)
25
#define         UCSRB0          __IOR(IO_BASE_UART0+0x02)
26
#define         UBRR0           __IOR(IO_BASE_UART0+0x03)
27
 
28
/* UCSRA */
29
#define         RXB8            0
30
#define         PE              2
31
#define         DOR             3
32
#define         FE              4
33
#define         UDRE            5
34
#define         TXC             6
35
#define         RXC             7
36
 
37
/* UCSRB */
38
#define         TXB8            0
39
#define         UCSZ            1
40
#define         UPM0            2
41
#define         UPM1            3
42
#define         USBS            4
43
#define         UDRIE           5
44
#define         TXCIE           6
45
#define         RXCIE           7       
46
 
47
/* timer.h */
48
 
49
#define         TCNT0           __IOW(IO_BASE_TIMER0+0x00)
50
#define         TCR0            __IOR(IO_BASE_TIMER0+0x02)
51
#define         TSR0            __IOR(IO_BASE_TIMER0+0x03)
52
 
53
#define         TOF             7       /* timer overflow */
54
#define         TOFIE           7       /* timer overflow interrupt enable */
55
#define         TPRESC0         0        /* timer prescaler bit 0 */
56
#define         TPRESC1         1       /* timer prescaler bit 1 */
57
 
58
/* port.h */
59
 
60
#define         PORTOUT0        __IOR(IO_BASE_PORTOUT0+0x00)
61
 
62
/*****************************************************************************/
63
 
64
static int uart_putchar(char c, FILE *stream)
65
{
66
 loop_until_bit_is_set(UCSRA0, UDRE);
67
 UDR0 = c;
68
 return(0);
69
}
70
 
71
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
72
 
73
/*****************************************************************************/
74
 
75
/* RXC */
76
ISR(_VECTOR(3))
77
{
78
 uint8_t        c;
79
 c=UDR0;
80
 if ( 'a' <= c && c <= 'z' )     c-=('a'-'A');
81
 UDR0=c;
82
}
83
 
84
ISR(_VECTOR(1))
85
{
86
 TCR0=TCR0;
87
 PORTOUT0 ^= 0x02;
88
}
89
 
90
 
91
static inline void msleep(uint16_t msec)
92
{
93
 while ( msec )
94
  {     _delay_loop_2((uint32_t)F_CPU/4000UL);
95
        msec--;
96
  }
97
}
98
 
99
static void test_memory_buffer(void)
100
{
101
 static char *  string="0123456789";
102
 uint8_t        length=10,offset,cnt;
103
 
104
 msleep(10);
105
 
106
 offset=0;
107
 cnt=0;
108
 while ( 1 )
109
  {     uint8_t c;
110
 
111
        while ( ! (UCSRA0&(1<<RXC)) );
112
        c=UDR0;
113
 
114
        if ( c != 10 )
115
         {      c=string[offset];
116
                offset++;
117
                if ( length==offset )   offset=0;
118
         }
119
 
120
        UDR0=c;
121
        cnt++;
122
        PORTOUT0=cnt&3;
123
  };
124
 
125
 
126
}
127
 
128
static void test_uppercase(void)
129
{
130
 while ( 1 )
131
  {     uint8_t c;
132
        while ( ! (UCSRA0&(1<<RXC)) );
133
        c=UDR0;
134
        if ( 'a' <= c && c <= 'z' )     c-=('a'-'A');
135
        while ( ! (UCSRA0&(1<<UDRE)) );
136
        UDR0=c;
137
  };
138
 
139
}
140
 
141
static void test_printf(void)
142
{
143
 int    i;
144
 
145
 stdout=&mystdout;
146
 
147
 i=0;
148
 PORTOUT0 = 0x90;
149
 PORTOUT0 ^= 0x03;
150
 
151
 while ( 1 )
152
  {     PORTOUT0 ^= 0x03;
153
        printf("[x] %d => %d\n",i,i*i);
154
        msleep(1000);
155
        i++;
156
  }
157
}
158
 
159
void test_interrupt(void)
160
{
161
 UCSRB0 |= (1<<RXCIE);
162
 
163
 TCR0 = 0x02;
164
 TCR0 |= (1<<TOFIE);
165
 
166
 sei();
167
 while ( 1 )
168
  {     PORTOUT0 ^= 0x01;
169
        msleep(500);
170
  }
171
}
172
 
173
 
174
int main(void)
175
{
176
 uint8_t        c;
177
 
178
 UBRR0 = 13-1;
179
 
180
 test_printf();
181
 test_interrupt();
182
 test_uppercase();
183
 test_memory_buffer();
184
 
185
 c=0;
186
 while ( 1 )
187
  {     msleep(500);
188
        PORTOUT0=c;
189
        UDR0='a'+c;
190
        c=(c+1)%4;
191
  }
192
 test_uppercase();
193
 return(0);
194
}
195
 

powered by: WebSVN 2.1.0

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