Embedded C Firmware Library : Arduino / Atmega328p  1
Register Level Embedded C Hardware Abstraction Library for AVR ATmega48A/PA/88A/PA/168A/PA/328/P or Arduino UNO/NANO/MINI
avr_usart.h
Go to the documentation of this file.
1 
35 #ifndef AVR_USART_H_
36 #define AVR_USART_H_
37 
38 #include <avr/io.h>
39 #include <avr/interrupt.h>
40 #include "avr_macros.h"
41 #include "avr_utils.h"
42 
43 #pragma message ( "avr_usart.h included" )
44 
49 #define USART_SYNCHRONOUS_CLOCK_PRIORITY 0
50 #define USART_MODE_ASYNCHRONOUS (0<<UMSEL00)
51 #define USART_MODE_SYNCHRONOUS (1<<UMSEL00) | USART_SYNCHRONOUS_CLOCK_PRIORITY
52 #define USART_MODE_MASK (1<<UMSEL00)
59 #define USART_PARITY_BIT_NO (0<<UPM00)
60 #define USART_PARITY_BIT_EVEN (1<<UPM01)
61 #define USART_PARITY_BIT_ODD (1<<UPM01) | (1<<UPM00)
62 #define USART_PARITY_BIT_MASK (1<<UPM01) | (1<<UPM00)
69 #define USART_STOP_BIT_ONE (0<<USBS0)
70 #define USART_STOP_BIT_TWO (1<<USBS0)
71 #define USART_STOP_BIT_MASK (1<<USBS0)
78 #define USART_DATA_BIT_FIVE (0<<UCSZ00)
79 #define USART_DATA_BIT_SIX (1<<UCSZ00)
80 #define USART_DATA_BIT_SEVEN (1<<UCSZ01)
81 #define USART_DATA_BIT_EIGHT (1<<UCSZ01) | (1<<UCSZ00)
82 #define USART_DATA_BIT_MASK (1<<UCSZ01) | (1<<UCSZ00)
89 #define USART_RX_COMPLETE_INTERRUPT (1<<RXCIE0)
90 #define USART_TX_COMPLETE_INTERRUPT (1<<TXCIE0)
91 #define USART_DATA_REGISTER_EMPTY_INTERRUPT (1<<UDRIE0)
98 typedef struct USART_ConfigData
99 {
100  uint8_t UsartMode;
101  uint16_t BaudRate;
102  uint8_t ParityBit;
103  uint8_t StopBit;
104  uint8_t DataBit;
112 static volatile uint8_t *USART_TransmitBuffer;
113 static volatile uint8_t USART_TransmitBusyFlag = 0;
114 static volatile uint16_t USART_TransmitBufferIndex = 0;
115 static volatile uint16_t USART_TransmitBufferLength = 0;
117 static volatile uint8_t *USART_ReceiveBuffer;
118 static volatile uint8_t USART_ReceiveBusyFlag = 0;
119 static volatile uint16_t USART_ReceiveBufferIndex = 0;
120 static volatile uint16_t USART_ReceiveBufferLength = 0;
135 inline void USART_EnableTransmitInterrupt();
136 inline void USART_DisableTransmitInterrupt();
143 inline void USART_EnableReceiveInterrupt();
144 inline void USART_DisableReceiveInterrupt();
151 void USART_Init(USART_ConfigData Data);
152 void USART_DeInit();
159 uint8_t USART_ReceiveBlocking();
160 uint8_t USART_ReceiveByte(uint8_t *Buffer);
161 uint8_t USART_ReceiveTwoBytes(uint16_t *Buffer);
162 uint8_t USART_ReceiveBytes(uint8_t *Buffer, uint16_t Size);
163 uint8_t USART_ReceiveChar(char *Character);
164 uint8_t USART_ReceiveInt(int *Number, uint8_t Size);
165 uint8_t USART_ReceiveLong(long *Number, uint8_t Size);
166 uint8_t USART_ReceiveString(char *Buffer, uint16_t Size);
167 void USART_CancelReceive();
174 uint8_t USART_TransmitBlocking(uint8_t *Buffer);
175 uint8_t USART_TransmitByte(uint8_t *Buffer);
176 uint8_t USART_TransmitTwoBytes(uint16_t *Buffer);
177 uint8_t USART_TransmitBytes(uint8_t *Buffer, uint16_t Size);
178 uint8_t USART_TransmitChar(char Character);
179 uint8_t USART_TransmitInt(int Number);
180 uint8_t USART_TransmitLong(long Number);
181 uint8_t USART_TransmitFloat(float Number, uint8_t Precision);
182 uint8_t USART_TransmitString(char *Buffer);
183 void USART_CancelTransmit();
192 {
193  for (int i = 0; i < USART_ReceiveBufferLength; i++)
194  {
195  USART_ReceiveBuffer[i] = 0;
196  }
197 
198  USART_ReceiveBuffer = 0;
199  USART_ReceiveBusyFlag = 0;
200  USART_ReceiveBufferIndex = 0;
201  USART_ReceiveBufferLength = 0;
202 }
203 
210 {
211  for (int i = 0; i < USART_TransmitBufferLength; i++)
212  {
213  USART_TransmitBuffer[i] = 0;
214  }
215 
216  USART_TransmitBuffer = 0;
217  USART_TransmitBusyFlag = 0;
218  USART_TransmitBufferIndex = 0;
219  USART_TransmitBufferLength = 0;
220 }
221 
228 {
230 }
231 
238 {
240 }
241 
248 {
249  UCSR0B |= USART_RX_COMPLETE_INTERRUPT;
250 }
251 
258 {
259  UCSR0B &= ~USART_RX_COMPLETE_INTERRUPT;
260 }
261 
277 {
278  uint16_t USART_BaudPrescaler = 0;
279 
280  if (Data.UsartMode == (USART_MODE_ASYNCHRONOUS))
281  {
282  USART_BaudPrescaler = (((F_CPU / (Data.BaudRate * 16UL))) - 1);
283  }
284  else if (Data.UsartMode == (USART_MODE_SYNCHRONOUS))
285  {
286  USART_BaudPrescaler = (((F_CPU / (Data.BaudRate * 2UL))) - 1);
287  }
288 
289  /* Set Baud Rate */
290  UBRR0H = USART_BaudPrescaler >> 8;
291  UBRR0L = USART_BaudPrescaler;
292 
293  /* Set Frame Format */
294  UCSR0C = Data.UsartMode | Data.ParityBit | Data.StopBit | Data.DataBit;
295 
296  /* Enable Receiver and Transmitter */
297  UCSR0B = (1<<RXEN0) | (1<<TXEN0);
298 
299  /* Enable Global Interrupt */
300  sei();
301 }
302 
309 {
310  UCSR0B = 0x00;
311  UCSR0C = 0x06;
312 }
313 
320 {
321  uint8_t DataByte;
322  while ((UCSR0A & (1<<RXC0)) == 0); // Do nothing until data have been received
323  DataByte = UDR0;
324  return DataByte;
325 }
326 
333 uint8_t USART_ReceiveByte(uint8_t *Buffer)
334 {
335  while (USART_ReceiveBusyFlag)
336  {
337  return BUSY;
338  }
339  USART_ReceiveBuffer = Buffer;
340  USART_ReceiveBufferLength = 1;
341  USART_ReceiveBufferIndex = 0;
342  USART_ReceiveBusyFlag = 1;
344  return OK;
345 }
346 
353 uint8_t USART_ReceiveTwoBytes(uint16_t *Buffer)
354 {
355  while (USART_ReceiveBusyFlag)
356  {
357  return BUSY;
358  }
359  USART_ReceiveBuffer = (uint8_t*)Buffer;
360  USART_ReceiveBufferLength = 2;
361  USART_ReceiveBufferIndex = 0;
362  USART_ReceiveBusyFlag = 1;
364  return OK;
365 }
366 
374 uint8_t USART_ReceiveBytes(uint8_t *Buffer, uint16_t Size)
375 {
376  if (Size == 0) return ERROR;
377 
378  while (USART_ReceiveBusyFlag)
379  {
380  return BUSY;
381  }
382  USART_ReceiveBuffer = Buffer;
383  USART_ReceiveBufferLength = Size;
384  USART_ReceiveBufferIndex = 0;
385  USART_ReceiveBusyFlag = 1;
387  return OK;
388 }
389 
396 uint8_t USART_ReceiveChar(char *Character)
397 {
398  while (USART_ReceiveBusyFlag)
399  {
400  return BUSY;
401  }
402  USART_ReceiveBuffer = (uint8_t*)Character;
403  USART_ReceiveBufferLength = 1;
404  USART_ReceiveBufferIndex = 0;
405  USART_ReceiveBusyFlag = 1;
407  return OK;
408 }
409 
417 uint8_t USART_ReceiveInt(int *Number, uint8_t Size)
418 {
419  if (Size == 0) return ERROR;
420 
421  char localbuffer[7] = {0};
422  while (USART_ReceiveBusyFlag)
423  {
424  return BUSY;
425  }
426  USART_ReceiveBuffer = (uint8_t*)localbuffer;
427  USART_ReceiveBufferLength = Size;
428  USART_ReceiveBufferIndex = 0;
429  USART_ReceiveBusyFlag = 1;
431  while (USART_ReceiveBusyFlag);
432  *Number = UTILS_StringToInt(localbuffer);
433  return OK;
434 }
435 
443 uint8_t USART_ReceiveLong(long *Number, uint8_t Size)
444 {
445  if (Size == 0) return ERROR;
446 
447  char localbuffer[12] = {0};
448  while (USART_ReceiveBusyFlag)
449  {
450  return BUSY;
451  }
452  USART_ReceiveBuffer = (uint8_t*)localbuffer;
453  USART_ReceiveBufferLength = Size;
454  USART_ReceiveBufferIndex = 0;
455  USART_ReceiveBusyFlag = 1;
457  while (USART_ReceiveBusyFlag);
458  *Number = UTILS_StringToLong(localbuffer);
459  return OK;
460 }
461 
469 uint8_t USART_ReceiveString(char *Buffer, uint16_t Size)
470 {
471  if (Size == 0) return ERROR;
472 
473  while (USART_ReceiveBusyFlag)
474  {
475  return BUSY;
476  }
477  USART_ReceiveBuffer = (uint8_t*)Buffer;
478  USART_ReceiveBufferLength = Size;
479  USART_ReceiveBufferIndex = 0;
480  USART_ReceiveBusyFlag = 1;
482  return OK;
483 }
484 
491 {
493  USART_ReceiveBusyFlag = 0;
494 }
495 
501 uint8_t USART_TransmitBlocking(uint8_t *Buffer)
502 {
503  while ((UCSR0A & (1<<UDRE0)) == 0); // Do nothing until UDR is ready
504  UDR0 = *Buffer;
505  return OK;
506 }
507 
514 uint8_t USART_TransmitByte(uint8_t *Buffer)
515 {
516  while (USART_TransmitBusyFlag)
517  {
518  return BUSY;
519  }
520  USART_TransmitBuffer = Buffer;
521  USART_TransmitBufferLength = 1;
522  USART_TransmitBufferIndex = 0;
523  USART_TransmitBusyFlag = 1;
525  return OK;
526 }
527 
534 uint8_t USART_TransmitTwoBytes(uint16_t *Buffer)
535 {
536  while (USART_TransmitBusyFlag)
537  {
538  return BUSY;
539  }
540  USART_TransmitBuffer = (uint8_t*)Buffer;
541  USART_TransmitBufferLength = 2;
542  USART_TransmitBufferIndex = 0;
543  USART_TransmitBusyFlag = 1;
545  return OK;
546 }
547 
555 uint8_t USART_TransmitBytes(uint8_t *Buffer, uint16_t Size)
556 {
557  if (Size == 0) return ERROR;
558 
559  while (USART_TransmitBusyFlag)
560  {
561  return BUSY;
562  }
563  USART_TransmitBuffer = Buffer;
564  USART_TransmitBufferLength = Size;
565  USART_TransmitBufferIndex = 0;
566  USART_TransmitBusyFlag = 1;
568  return OK;
569 }
570 
577 uint8_t USART_TransmitChar(char Character)
578 {
579  uint8_t* data = (uint8_t*)&Character;
580  return USART_TransmitByte(data);
581 }
582 
589 uint8_t USART_TransmitInt(int Number)
590 {
591  while (USART_TransmitBusyFlag)
592  {
593  return BUSY;
594  }
595  static uint8_t localbuffer[7] = {0};
596  uint16_t Length = UTILS_IntToString(Number,(char*)localbuffer);
597  return USART_TransmitBytes(localbuffer,Length);
598 }
599 
606 uint8_t USART_TransmitLong(long Number)
607 {
608  while (USART_TransmitBusyFlag)
609  {
610  return BUSY;
611  }
612  static uint8_t localbuffer[12] = {0};
613  uint16_t Length = UTILS_LongToString(Number,(char*)localbuffer);
614  return USART_TransmitBytes(localbuffer,Length);
615 }
616 
624 uint8_t USART_TransmitFloat(float Number,uint8_t Precision)
625 {
626  while (USART_TransmitBusyFlag)
627  {
628  return BUSY;
629  }
630  static uint8_t localbuffer[17] = {0};
631  uint16_t Length = UTILS_FloatToString(Number,(char*)localbuffer,Precision);
632  return USART_TransmitBytes(localbuffer,Length);
633 }
634 
641 uint8_t USART_TransmitString(char *Buffer)
642 {
643  while (USART_TransmitBusyFlag)
644  {
645  return BUSY;
646  }
647  const char *p = Buffer;
648  while (*p) ++p;
649  uint16_t Size = p - Buffer;
650  USART_TransmitBuffer = (uint8_t*)Buffer;
651  USART_TransmitBufferLength = Size;
652  USART_TransmitBufferIndex = 0;
653  USART_TransmitBusyFlag = 1;
655  return OK;
656 }
657 
664 {
666  USART_TransmitBusyFlag = 0;
667 }
668 
674 ISR(USART_RX_vect)
675 {
676  if(USART_ReceiveBufferIndex < USART_ReceiveBufferLength)
677  {
678  USART_ReceiveBuffer[USART_ReceiveBufferIndex] = UDR0;
679  USART_ReceiveBufferIndex++;
680  }
681  if(USART_ReceiveBufferIndex >= USART_ReceiveBufferLength)
682  {
683  USART_ReceiveBusyFlag = 0;
685  }
686 }
687 
693 ISR(USART_UDRE_vect)
694 {
695  if(USART_TransmitBufferIndex < USART_TransmitBufferLength)
696  {
697  UDR0 = USART_TransmitBuffer[USART_TransmitBufferIndex];
698  USART_TransmitBufferIndex++;
699  }
700  if(USART_TransmitBufferIndex >= USART_TransmitBufferLength)
701  {
702  USART_TransmitBusyFlag = 0;
704  }
705 }
706 
707 #endif /* AVR_USART_H_ */
USART_TransmitBlocking
uint8_t USART_TransmitBlocking(uint8_t *Buffer)
Public Function to Transmit 1 Byte of Data in Polling.
Definition: avr_usart.h:501
USART_TransmitFloat
uint8_t USART_TransmitFloat(float Number, uint8_t Precision)
Public Function to Transmit float Data as ASCII.
Definition: avr_usart.h:624
USART_FlushReceiveBuffer
void USART_FlushReceiveBuffer()
Public Function to Flush the Receive Buffer.
Definition: avr_usart.h:191
USART_ReceiveLong
uint8_t USART_ReceiveLong(long *Number, uint8_t Size)
Public Function to Receive long Data as ASCII.
Definition: avr_usart.h:443
USART_ReceiveInt
uint8_t USART_ReceiveInt(int *Number, uint8_t Size)
Public Function to Receive int Data as ASCII.
Definition: avr_usart.h:417
USART_EnableReceiveInterrupt
void USART_EnableReceiveInterrupt()
Public Function to Enable Receive Interrupt.
Definition: avr_usart.h:247
USART_TransmitBytes
uint8_t USART_TransmitBytes(uint8_t *Buffer, uint16_t Size)
Public Function to Transmit Multiple Bytes of Data.
Definition: avr_usart.h:555
USART_TransmitInt
uint8_t USART_TransmitInt(int Number)
Public Function to Transmit int Data as ASCII.
Definition: avr_usart.h:589
USART_CancelTransmit
void USART_CancelTransmit()
Public Function to Cancel any Interrupt based Transmission.
Definition: avr_usart.h:663
UTILS_IntToString
uint8_t UTILS_IntToString(int Number, char *Buffer)
Public Function to Convert an int Data into a string Data.
Definition: avr_utils.h:93
UTILS_LongToString
uint8_t UTILS_LongToString(long Number, char *Buffer)
Public Function to Convert an long Data into a string Data.
Definition: avr_utils.h:123
USART_ReceiveBlocking
uint8_t USART_ReceiveBlocking()
Public Function to Receive 1 Byte of Data in Polling.
Definition: avr_usart.h:319
avr_utils.h
Library for Utility Functions.
USART_TransmitString
uint8_t USART_TransmitString(char *Buffer)
Public Function to Transmit string Data.
Definition: avr_usart.h:641
USART_TransmitTwoBytes
uint8_t USART_TransmitTwoBytes(uint16_t *Buffer)
Public Function to Transmit 2 Bytes of Data with LSB Byte First.
Definition: avr_usart.h:534
USART_CancelReceive
void USART_CancelReceive()
Public Function to Cancel any Interrupt based Reception.
Definition: avr_usart.h:490
USART_ReceiveBytes
uint8_t USART_ReceiveBytes(uint8_t *Buffer, uint16_t Size)
Public Function to Receive Multiple Bytes of Data.
Definition: avr_usart.h:374
UTILS_StringToInt
int UTILS_StringToInt(char *Buffer)
Public Function to Convert an string Data into a int Data.
Definition: avr_utils.h:181
USART_TransmitByte
uint8_t USART_TransmitByte(uint8_t *Buffer)
Public Function to Transmit 1 Byte of Data.
Definition: avr_usart.h:514
UTILS_StringToLong
int UTILS_StringToLong(char *Buffer)
Public Function to Convert an string Data into a long Data.
Definition: avr_utils.h:208
USART_MODE_SYNCHRONOUS
#define USART_MODE_SYNCHRONOUS
Definition: avr_usart.h:51
USART_MODE_ASYNCHRONOUS
#define USART_MODE_ASYNCHRONOUS
Definition: avr_usart.h:50
avr_macros.h
Library for all common Macro Definition.
USART_DisableReceiveInterrupt
void USART_DisableReceiveInterrupt()
Public Function to Disable Receive Interrupt.
Definition: avr_usart.h:257
USART_EnableTransmitInterrupt
void USART_EnableTransmitInterrupt()
Public Function to Enable Transmit Interrupt.
Definition: avr_usart.h:227
UTILS_FloatToString
uint8_t UTILS_FloatToString(float Number, char *Buffer, uint8_t Precision)
Public Function to Convert an float Data into a string Data.
Definition: avr_utils.h:154
F_CPU
#define F_CPU
Definition: avr_macros.h:39
USART_Init
void USART_Init(USART_ConfigData Data)
Public Function to Configure and Initialize USART.
Definition: avr_usart.h:276
USART_ReceiveChar
uint8_t USART_ReceiveChar(char *Character)
Public Function to Receive char Data.
Definition: avr_usart.h:396
USART_DATA_REGISTER_EMPTY_INTERRUPT
#define USART_DATA_REGISTER_EMPTY_INTERRUPT
Definition: avr_usart.h:91
USART_FlushTransmitBuffer
void USART_FlushTransmitBuffer()
Public Function to Flush the Transmit Buffer.
Definition: avr_usart.h:209
USART_ReceiveByte
uint8_t USART_ReceiveByte(uint8_t *Buffer)
Public Function to Receive 1 Byte of Data.
Definition: avr_usart.h:333
USART_TransmitChar
uint8_t USART_TransmitChar(char Character)
Public Function to Transmit char Data.
Definition: avr_usart.h:577
USART_DeInit
void USART_DeInit()
Public Function to De-Initialize USART.
Definition: avr_usart.h:308
USART_ConfigData
Definition: avr_usart.h:99
USART_ReceiveString
uint8_t USART_ReceiveString(char *Buffer, uint16_t Size)
Public Function to Receive string Data.
Definition: avr_usart.h:469
USART_TransmitLong
uint8_t USART_TransmitLong(long Number)
Public Function to Transmit long Data as ASCII.
Definition: avr_usart.h:606
USART_RX_COMPLETE_INTERRUPT
#define USART_RX_COMPLETE_INTERRUPT
Definition: avr_usart.h:89
USART_ReceiveTwoBytes
uint8_t USART_ReceiveTwoBytes(uint16_t *Buffer)
Public Function to Receive 2 Bytes of Data with LSB Byte First.
Definition: avr_usart.h:353
ISR
ISR(USART_RX_vect)
Interrupt Service Routine for Reception.
Definition: avr_usart.h:674
USART_DisableTransmitInterrupt
void USART_DisableTransmitInterrupt()
Public Function to Disable Transmit Interrupt.
Definition: avr_usart.h:237