-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls_atmega328.c
520 lines (430 loc) · 14.8 KB
/
ls_atmega328.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* -----------------------------------------------------------------------------
* File: LS_ATmega328.c
* Module: ATmega328 basic interface
* Author: Leandro Schwarz
* Version: 7.3
* Last edition: 15/09/2014
* -------------------------------------------------------------------------- */
// -----------------------------------------------------------------------------
// Header files ----------------------------------------------------------------
#include "LS_ATmega328.h"
#if __LS_ATMEGA328_H != 73
#error Error 101 - Version mismatch on header and source code files (LS_ATmega328).
#endif
// -----------------------------------------------------------------------------
// Global variables ------------------------------------------------------------
adcConfiguration_t adcConfiguration;
spiConfiguration_t spiConfiguration;
usartConfiguration_t usartConfiguration;
FILE usartStream = FDEV_SETUP_STREAM(usartTransmitStd, usartReceiveStd, _FDEV_SETUP_RW);
uint8 usartReceiverBuffer[USART_RECEIVER_BUFFER_SIZE];
uint8 usartReceiverBufferNextRead; // Pointer to the next read location
uint8 usartReceiverBufferNextWrite; // Pointer to the next write location
uint8 usartReceiverBufferLength; // Buffer length
/* -----------------------------------------------------------------------------
* Writes a byte in the specified address in the EEPROM
* -------------------------------------------------------------------------- */
void eepromWrite(uint8 data, uint16 address)
{
waitUntilBitIsClear(EECR, EEPE);
EEAR = (address & 0x01FF);
EEDR = data;
setBit(EECR, EEMPE);
setBit(EECR, EEPE);
return;
}
/* -----------------------------------------------------------------------------
* Reads a byte from the specified address in the EEPROM
* -------------------------------------------------------------------------- */
uint8 eepromRead(uint16 address)
{
waitUntilBitIsClear(EECR, EEPE);
EEAR = (address & 0x01FF);
setBit(EECR, EERE);
return EEDR;
}
/* -----------------------------------------------------------------------------
* Writes the value of register TCNT1 of timer1 using atomic mode
* -------------------------------------------------------------------------- */
void timer1SetCounterValue(uint16 value)
{
uint8 sreg;
sreg = SREG;
cli();
TCNT1 = value;
SREG = sreg;
return;
}
/* -----------------------------------------------------------------------------
* Reads the value of register TCNT1 of timer1 using atomic mode
* -------------------------------------------------------------------------- */
uint16 timer1GetCounterValue(void)
{
uint8 sreg;
uint16 aux;
sreg = SREG;
cli();
aux = TCNT1;
SREG = sreg;
return aux;
}
/* -----------------------------------------------------------------------------
* Writes the value of register OCR1A of timer1 using atomic mode
* -------------------------------------------------------------------------- */
void timer1SetCompareAValue(uint16 value)
{
uint8 sreg;
sreg = SREG;
cli();
OCR1A = value;
SREG = sreg;
return;
}
/* -----------------------------------------------------------------------------
* Reads the value of register OCR1A of timer1 using atomic mode
* -------------------------------------------------------------------------- */
uint16 timer1GetCompareAValue(void)
{
uint8 sreg;
uint16 aux;
sreg = SREG;
cli();
aux = OCR1A;
SREG = sreg;
return aux;
}
/* -----------------------------------------------------------------------------
* Writes the value of register OCR1B of timer1 using atomic mode
* -------------------------------------------------------------------------- */
void timer1SetCompareBValue(uint16 value)
{
uint8 sreg;
sreg = SREG;
cli();
OCR1B = value;
SREG = sreg;
return;
}
/* -----------------------------------------------------------------------------
* Reads the value of register OCR1B of timer1 using atomic mode
* -------------------------------------------------------------------------- */
uint16 timer1GetCompareBValue(void)
{
uint8 sreg;
uint16 aux;
sreg = SREG;
cli();
aux = OCR1B;
SREG = sreg;
return aux;
}
/* -----------------------------------------------------------------------------
* Writes the value of register ICR1 of timer1 using atomic mode
* -------------------------------------------------------------------------- */
void timer1SetInputCaptureValue(uint16 value)
{
uint8 sreg;
sreg = SREG;
cli();
ICR1 = value;
SREG = sreg;
return;
}
/* -----------------------------------------------------------------------------
* Reads the value of register ICR1 of timer1 using atomic mode
* -------------------------------------------------------------------------- */
uint16 timer1GetInputCaptureValue(void)
{
uint8 sreg;
uint16 aux;
sreg = SREG;
cli();
aux = ICR1;
SREG = sreg;
return aux;
}
/* -----------------------------------------------------------------------------
* Configures the SPI controller
* -------------------------------------------------------------------------- */
void spiInit(void)
{
if(spiConfiguration.masterSlave){
setBit(SPI_DDR, SPI_MOSI);
setBit(SPI_DDR, SPI_SCK);
clrBit(SPI_DDR, SPI_MISO);
}else{
clrBit(SPI_DDR, SPI_MOSI);
clrBit(SPI_DDR, SPI_SCK);
setBit(SPI_DDR, SPI_MISO);
}
if(spiConfiguration.doubleSpeed)
setBit(SPSR, SPI2X);
else
clrBit(SPSR, SPI2X);
SPCR = (spiConfiguration.interruptEnabled << SPIE) |
(spiConfiguration.enabled << SPE) |
(spiConfiguration.msbLsb << DORD) |
(spiConfiguration.masterSlave << MSTR) |
(spiConfiguration.sckIdleValue << CPOL) |
(spiConfiguration.leadingTrailingEdge << CPHA) |
(spiConfiguration.clockPrescaler & 0x03);
return;
}
/* -----------------------------------------------------------------------------
* Transmit data at the SPI bus in master mode
* -------------------------------------------------------------------------- */
uint8 spiMasterTransmit(uint8 data)
{
SPDR = data;
waitUntilBitIsClear(SPSR, SPIF);
return SPDR;
}
/* -----------------------------------------------------------------------------
* Receives data from the SPI bus
* -------------------------------------------------------------------------- */
uint8 spiSlaveTransmit(void)
{
waitUntilBitIsClear(SPSR, SPIF);
return SPDR;
}
/* -----------------------------------------------------------------------------
* Get the current ADC configuration
* -------------------------------------------------------------------------- */
adcConfiguration_t adcGetConfiguration(void)
{
return adcConfiguration;
}
/* -----------------------------------------------------------------------------
* Load an ADC configuration
* -------------------------------------------------------------------------- */
void adcLoadConfiguration(adcConfiguration_t config)
{
adcConfiguration = config;
ADMUX = ((config.reference & 0x03) << 6) |
(config.leftAdjust << 5) |
(config.channel & 0x0F);
ADCSRA = (config.enabled << 7) |
(config.automaticMode << 5) |
(config.interruptEnabled << 3) |
(config.clockPrescaller & 0x07);
ADCSRB = (config.analogComparator << 6) |
(config.triggerSource & 0x07);
DIDR0 = config.digitalDisabled & 0x3F;
return;
}
/* -----------------------------------------------------------------------------
* Configures the USART controller
* -------------------------------------------------------------------------- */
void usartInit(uint32 baudRate)
{
uint64 aux64;
// Clear errors
UCSR0A &= 0xE3;
usartConfiguration.frameError = 0;
usartConfiguration.bufferOverflowError = 0;
usartConfiguration.parityError = 0;
// USART configuration
switch(usartConfiguration.mode){
case 0:
clrBit(UCSR0A, U2X0); // Normal speed
clrBit(UCSR0C, UCPOL0); // Write zero in asynchronous mode
clrBit(UCSR0C, UMSEL01); // Asynchronous mode
clrBit(UCSR0C, UMSEL00); // Asynchronous mode
aux64 = F_CPU / 16 / baudRate;
usartConfiguration.ubrr = (uint16)(aux64 - 1);
break;
case 1:
setBit(UCSR0A, U2X0); // Double speed
clrBit(UCSR0C, UCPOL0); // Write zero in asynchronous mode
clrBit(UCSR0C, UMSEL01); // Asynchronous mode
clrBit(UCSR0C, UMSEL00); // Asynchronous mode
aux64 = F_CPU / 8 / baudRate;
usartConfiguration.ubrr = (uint16)(aux64 - 1);
break;
case 2:
clrBit(UCSR0A, U2X0); // Write zero in synchronous mode
if(usartConfiguration.polarity)
setBit(UCSR0C, UCPOL0); // XCK rising edge
else
clrBit(UCSR0C, UCPOL0); // XCK falling edge
clrBit(UCSR0C, UMSEL01); // Synchronous mode
setBit(UCSR0C, UMSEL00); // Synchronous mode
break;
case 3:
clrBit(UCSR0A, U2X0); // Write zero in synchronous mode
if(usartConfiguration.polarity)
setBit(UCSR0C, UCPOL0); // XCK rising edge
else
clrBit(UCSR0C, UCPOL0); // XCK falling edge
setBit(UCSR0C, UMSEL01); // Master SPI mode
setBit(UCSR0C, UMSEL00); // Master SPI mode
aux64 = F_CPU / 2 / baudRate;
usartConfiguration.ubrr = (uint16)(aux64 - 1);
break;
}
// Sets the baud rate
UBRR0H = (uint8)(usartConfiguration.ubrr >> 8);
UBRR0L = (uint8)usartConfiguration.ubrr;
// Receiver buffer
if(usartConfiguration.receiverBufferEnable)
usartActivateReceptionCompleteInterrupt();
return;
}
/* -----------------------------------------------------------------------------
* Checks if an error occurred during transmission or reception
* -------------------------------------------------------------------------- */
uint8 usartCheckError(void)
{
usartConfiguration.frameError = isBitSet(UCSR0A, FE0);
usartConfiguration.bufferOverflowError = isBitSet(UCSR0A, DOR0);
usartConfiguration.parityError = isBitSet(UCSR0A, UPE0);
return (usartConfiguration.parityError | usartConfiguration.bufferOverflowError | usartConfiguration.frameError);
}
/* -----------------------------------------------------------------------------
* Transmits data in 5, 6, 7 or 8 bits modes using the USART controller
* -------------------------------------------------------------------------- */
void usartTransmit(int8 data)
{
while(!usartIsBufferEmpty())
; // Waits until last transmission ends
UDR0 = data;
return;
}
/* -----------------------------------------------------------------------------
* Transmits data in 9 bits mode using the USART controller
* -------------------------------------------------------------------------- */
void usartTransmit9bits(uint16 data)
{
uint8 aux;
while(!usartIsBufferEmpty())
; // Waits until last transmission ends
aux = ((data & 0x100) >> 8);
if(aux)
setBit(UCSR0B, TXB80);
else
clrBit(UCSR0B, TXB80);
UDR0 = (uint8)data;
return;
}
/* -----------------------------------------------------------------------------
* Transmits data in 5, 6, 7 or 8 bits modes using the USART controller and
* standard output heandler
* -------------------------------------------------------------------------- */
int16 usartTransmitStd(int8 data, FILE * stream)
{
while(!usartIsBufferEmpty())
; // Waits until last transmission ends
UDR0 = data;
return FALSE;
}
/* -----------------------------------------------------------------------------
* Receives data in 5, 6, 7 or 8 bits modes using the USART controller
* -------------------------------------------------------------------------- */
uint8 usartReceive(void)
{
uint8 status;
while(!usartIsReceptionComplete())
; // Waits until last reception ends
status = UCSR0A;
usartConfiguration.frameError = isBitSet(status, FE0);
usartConfiguration.bufferOverflowError = isBitSet(status, DOR0);
usartConfiguration.parityError = isBitSet(status, UPE0);
return UDR0;
}
/* -----------------------------------------------------------------------------
* Receives data in 9 bits modes using the USART controller
* -------------------------------------------------------------------------- */
uint16 usartReceive9bits(void)
{
uint8 status;
uint8 byteh;
uint8 bytel;
uint16 byte;
while(!usartIsReceptionComplete())
; // Waits until last reception ends
status = UCSR0A;
byteh = UCSR0B;
bytel = UDR0;
usartConfiguration.frameError = isBitSet(status, FE0);
usartConfiguration.bufferOverflowError = isBitSet(status, DOR0);
usartConfiguration.parityError = isBitSet(status, UPE0);
byte = (uint16)(byteh & 0x02) << 7;
byte |= bytel;
return byte;
}
/* -----------------------------------------------------------------------------
* Receives data in 5, 6, 7 or 8 bits modes using the USART controller and
* standard input heandler
* -------------------------------------------------------------------------- */
int16 usartReceiveStd(FILE * stream)
{
while(!usartIsReceptionComplete())
; // Waits until last reception ends
return (int16)UDR0;
}
/* -----------------------------------------------------------------------------
* Clears the receptor data buffer
* -------------------------------------------------------------------------- */
void usartClearReceptionBuffer(void)
{
uint8 aux;
while(usartIsReceptionComplete())
aux = UDR0;
return;
}
/* -----------------------------------------------------------------------------
* Adds data to the reception buffer. The function has NO CONTROL of lost data.
* -------------------------------------------------------------------------- */
void usartAddDataToReceiverBuffer(uint8 data)
{
if(((usartReceiverBufferNextWrite + 1) % USART_RECEIVER_BUFFER_SIZE) != usartReceiverBufferNextRead){
usartReceiverBuffer[usartReceiverBufferNextWrite] = data;
usartReceiverBufferNextWrite = (usartReceiverBufferNextWrite + 1) % USART_RECEIVER_BUFFER_SIZE;
usartReceiverBufferLength++;
}
return;
}
/* -----------------------------------------------------------------------------
* Gets data from the reception buffer. If the buffer is empty, the last
* retrieved data will be returned and the pointer will not be changed. The
* usartIsReceiverBufferEmpty() function must be called to check if there is new
* data in the buffer.
* -------------------------------------------------------------------------- */
uint8 usartGetDataFromReceiverBuffer(void)
{
uint8 data = usartReceiverBuffer[usartReceiverBufferNextRead];
if(usartReceiverBufferLength > 0){
usartReceiverBufferNextRead = (usartReceiverBufferNextRead + 1) % USART_RECEIVER_BUFFER_SIZE;
usartReceiverBufferLength--;
}
return data;
}
/* -----------------------------------------------------------------------------
* Verifies if there is new data in the receiver buffer. Must be called before
* reading the buffer.
* -------------------------------------------------------------------------- */
uint8 usartIsReceiverBufferEmpty(void)
{
if(usartReceiverBufferLength == 0)
return TRUE;
return FALSE;
}
/* -----------------------------------------------------------------------------
* Returns the current baud rate.
* -------------------------------------------------------------------------- */
uint32 usartGetCurrentBaudRate(void)
{
uint32 aux32 = 0;
switch(usartConfiguration.mode){
case 0:
aux32 = F_CPU / 16 / (UBRR0 + 1);
break;
case 1:
aux32 = F_CPU / 8 / (UBRR0 + 1);
break;
case 2:
aux32 = F_CPU / 2 / (UBRR0 + 1);
break;
}
return aux32;
}