-
Notifications
You must be signed in to change notification settings - Fork 1
/
t328p.c
377 lines (286 loc) · 8.36 KB
/
t328p.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* t328p --- test code for ATmega328P 2019-11-26 */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define LED PB0 // Blinking LED on PB0
#define SQWAVE PB1 // 500Hz square wave on PB1
#define LED_R PD2 // Red LED on PD2
#define LED_G PD7 // Green LED on PD7
#define LED_B PD4 // Blue LED on PD4
#define BAUDRATE (9600)
#define BAUD_SETTING ((F_CPU / (BAUDRATE * 16UL)) - 1)
#define UART_RX_BUFFER_SIZE (128)
#define UART_RX_BUFFER_MASK (UART_RX_BUFFER_SIZE - 1)
#if (UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK) != 0
#error UART_RX_BUFFER_SIZE must be a power of two and <= 256
#endif
#define UART_TX_BUFFER_SIZE (128)
#define UART_TX_BUFFER_MASK (UART_TX_BUFFER_SIZE - 1)
#if (UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK) != 0
#error UART_TX_BUFFER_SIZE must be a power of two and <= 256
#endif
struct UART_RX_BUFFER
{
volatile uint8_t head;
volatile uint8_t tail;
uint8_t buf[UART_RX_BUFFER_SIZE];
};
struct UART_TX_BUFFER
{
volatile uint8_t head;
volatile uint8_t tail;
uint8_t buf[UART_TX_BUFFER_SIZE];
};
struct UART_BUFFER
{
struct UART_TX_BUFFER tx;
struct UART_RX_BUFFER rx;
};
// UART buffers
struct UART_BUFFER U0Buf;
uint8_t SavedMCUSR = 0;
volatile uint32_t Milliseconds = 0UL;
volatile uint8_t Tick = 0;
/* USART_RX_vect --- ISR for USART Receive Complete, used for Rx */
ISR(USART_RX_vect)
{
const uint8_t tmphead = (U0Buf.rx.head + 1) & UART_RX_BUFFER_MASK;
const uint8_t ch = UDR0; // Read received byte from UART
if (tmphead == U0Buf.rx.tail) // Is receive buffer full?
{
// Buffer is full; discard new byte
}
else
{
U0Buf.rx.head = tmphead;
U0Buf.rx.buf[tmphead] = ch; // Store byte in buffer
}
}
/* USART_UDRE_vect --- ISR for USART Data Register Empty, used for Tx */
ISR(USART_UDRE_vect)
{
if (U0Buf.tx.head != U0Buf.tx.tail) // Is there anything to send?
{
const uint8_t tmptail = (U0Buf.tx.tail + 1) & UART_TX_BUFFER_MASK;
U0Buf.tx.tail = tmptail;
UDR0 = U0Buf.tx.buf[tmptail]; // Transmit one byte
}
else
{
UCSR0B &= ~(1 << UDRIE0); // Nothing left to send; disable Tx interrupt
}
}
/* TIMER1_COMPA_vect --- ISR for Timer/Counter 1 overflow, used for 1ms ticker */
ISR(TIMER1_COMPA_vect)
{
Milliseconds++;
Tick = 1;
PINB = (1 << SQWAVE); // DEBUG: 500Hz on PB1 pin
}
/* millis --- return milliseconds since reset */
uint32_t millis(void)
{
uint32_t ms;
cli();
ms = Milliseconds;
sei();
return (ms);
}
/* UART0RxByte --- read one character from the UART via the circular buffer */
uint8_t UART0RxByte(void)
{
const uint8_t tmptail = (U0Buf.rx.tail + 1) & UART_RX_BUFFER_MASK;
while (U0Buf.rx.head == U0Buf.rx.tail) // Wait, if buffer is empty
;
U0Buf.rx.tail = tmptail;
return (U0Buf.rx.buf[tmptail]);
}
/* UART0TxByte --- send one character to the UART via the circular buffer */
void UART0TxByte(const uint8_t data)
{
const uint8_t tmphead = (U0Buf.tx.head + 1) & UART_TX_BUFFER_MASK;
while (tmphead == U0Buf.tx.tail) // Wait, if buffer is full
;
U0Buf.tx.buf[tmphead] = data;
U0Buf.tx.head = tmphead;
UCSR0B |= (1 << UDRIE0); // Enable UART0 Tx interrupt
}
/* USART0_printChar --- helper function to make 'stdio' functions work */
static int USART0_printChar(const char c, FILE *stream)
{
if (c == '\n')
UART0TxByte('\r');
UART0TxByte(c);
return (0);
}
static FILE USART_stream = FDEV_SETUP_STREAM(USART0_printChar, NULL, _FDEV_SETUP_WRITE);
/* UART0RxAvailable --- return true if a byte is available in the UART0 circular buffer */
int UART0RxAvailable(void)
{
return (U0Buf.rx.head != U0Buf.rx.tail);
}
/* setRGBLed --- control RGB LED connected to PORT D */
void setRGBLed(const int state, const uint8_t fade)
{
switch (state) {
case 0: // Red fading up, blue on
OCR2A = fade;
OCR0A = 0;
OCR0B = 255;
PORTD |= (1 << LED_R);
PORTD &= ~(1 << LED_G);
PORTD &= ~(1 << LED_B);
break;
case 1: // Red on, blue fading down
OCR2A = 255;
OCR0A = 0;
OCR0B = 255 - fade;
PORTD |= (1 << LED_R);
PORTD |= (1 << LED_G);
PORTD &= ~(1 << LED_B);
break;
case 2: // Red on, green fading up
OCR2A = 255;
OCR0A = fade;
OCR0B = 0;
PORTD &= ~(1 << LED_R);
PORTD |= (1 << LED_G);
PORTD &= ~(1 << LED_B);
break;
case 3: // Red fading down, green on
OCR2A = 255 - fade;
OCR0A = 255;
OCR0B = 0;
PORTD &= ~(1 << LED_R);
PORTD |= (1 << LED_G);
PORTD |= (1 << LED_B);
break;
case 4: // Green on, blue fading up
OCR2A = 0;
OCR0A = 255;
OCR0B = fade;
PORTD &= ~(1 << LED_R);
PORTD &= ~(1 << LED_G);
PORTD |= (1 << LED_B);
break;
case 5: // Green fading down, blue on
OCR2A = 0;
OCR0A = 255 - fade;
OCR0B = 255;
PORTD |= (1 << LED_R);
PORTD &= ~(1 << LED_G);
PORTD |= (1 << LED_B);
break;
}
}
/* printResetReason --- print the cause of the chip's reset */
void printResetReason(void)
{
printf("MCUSR = %02x\n", SavedMCUSR);
}
/* initMCU --- set up the microcontroller in general */
static void initMCU(void)
{
SavedMCUSR = MCUSR;
MCUSR = 0;
}
/* initGPIOs --- set up the GPIO pins */
static void initGPIOs(void)
{
// Set up output pins
DDRB |= (1 << LED) | (1 << SQWAVE);
DDRD |= (1 << LED_R) | (1 << LED_B) | (1 << LED_G);
PORTB = 0; // All LEDs off
PORTD = 0;
}
/* initUARTs --- set up UART(s) and buffers, and connect to 'stdout' */
static void initUARTs(void)
{
// Set up UART0 and associated circular buffers
U0Buf.tx.head = 0;
U0Buf.tx.tail = 0;
U0Buf.rx.head = 0;
U0Buf.rx.tail = 0;
UBRR0H = (uint8_t)(BAUD_SETTING >> 8);
UBRR0L = (uint8_t)(BAUD_SETTING);
// Enable receive and transmit
UCSR0B = (1 << RXCIE0) | (1 << RXEN0) | (1 << TXEN0);
// Set frame format
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); // Async 8N1
stdout = &USART_stream; // Allow use of 'printf' and similar functions
}
/* initPWM --- set up PWM channels */
static void initPWM(void)
{
// Config Timer 0 for PWM
TCCR0A = (1 << COM0A1) | (1 << COM0B1) | (1 << WGM00);
TCCR0B = (1 << CS01); // Clock source = CLK/8, start PWM
OCR0A = 0x80;
OCR0B = 0x80;
DDRD |= (1 << PD6) | (1 << PD5); // Set OC0A and OC0B as outputs
// Config Timer 2 for PWM
TCCR2A = (1 << COM2A1) | (1 << COM2B1) | (1 << WGM20);
TCCR2B = (1 << CS21); // Clock source = CLK/8, start PWM
OCR2A = 0x80;
OCR2B = 0x80;
DDRB |= (1 << PB3); // Set OC2A as output
}
/* initMillisecondTimer --- set up a timer to interrupt every millisecond */
static void initMillisecondTimer(void)
{
// Set up Timer/Counter 1 for regular 1ms interrupt
TCCR1A = 0; // WGM11 and WGM10 are set to 0 for CTC mode
TCCR1B = (1 << WGM12) | (1 << CS10); // WGM13 set to 0 and WGM12 set to 1 for CTC mode
// CS10 set to 1 for divide-by-1 prescaler
OCR1A = 15999; // 16000 counts gives 1ms
TCNT1 = 0;
TIMSK1 = (1 << OCIE1A); // Enable interrupts
}
int main(void)
{
int ledState = 0;
uint8_t fade = 0;
uint32_t end;
initMCU();
initGPIOs();
initUARTs();
initPWM();
initMillisecondTimer();
sei(); // Enable interrupts
printf("\nHello from the %s\n", "ATmega328P");
printResetReason();
end = millis() + 500UL;
while (1) {
if (Tick) {
if (fade == 255) {
fade = 0;
if (ledState == 5)
ledState = 0;
else
ledState++;
}
else
fade++;
setRGBLed(ledState, fade);
if (millis() >= end) {
end = millis() + 500UL;
PINB = (1 << LED); // LED on PB0 toggle
printf("millis() = %ld\n", millis());
}
Tick = 0;
}
if (UART0RxAvailable()) {
const uint8_t ch = UART0RxByte();
printf("UART0: %02x\n", ch);
switch (ch) {
case 'r':
case 'R':
printResetReason();
break;
}
}
}
}