avr_usart.h

 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;
105 } USART_ConfigData;
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;
127 void USART_FlushTransmitBuffer();
128 void USART_FlushReceiveBuffer();
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();
191 void USART_FlushReceiveBuffer()
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 
209 void USART_FlushTransmitBuffer()
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 
227 inline void USART_EnableTransmitInterrupt()
228 {
229  UCSR0B |= USART_DATA_REGISTER_EMPTY_INTERRUPT;
230 }
231 
237 inline void USART_DisableTransmitInterrupt()
238 {
239  UCSR0B &= ~USART_DATA_REGISTER_EMPTY_INTERRUPT;
240 }
241 
247 inline void USART_EnableReceiveInterrupt()
248 {
249  UCSR0B |= USART_RX_COMPLETE_INTERRUPT;
250 }
251 
257 inline void USART_DisableReceiveInterrupt()
258 {
259  UCSR0B &= ~USART_RX_COMPLETE_INTERRUPT;
260 }
261 
276 void USART_Init(USART_ConfigData Data)
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 
308 void USART_DeInit()
309 {
310  UCSR0B = 0x00;
311  UCSR0C = 0x06;
312 }
313 
319 uint8_t USART_ReceiveBlocking()
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;
343  USART_EnableReceiveInterrupt();
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;
363  USART_EnableReceiveInterrupt();
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;
386  USART_EnableReceiveInterrupt();
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;
406  USART_EnableReceiveInterrupt();
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;
430  USART_EnableReceiveInterrupt();
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;
456  USART_EnableReceiveInterrupt();
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;
481  USART_EnableReceiveInterrupt();
482  return OK;
483 }
484 
490 void USART_CancelReceive()
491 {
492  USART_DisableReceiveInterrupt();
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;
524  USART_EnableTransmitInterrupt();
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;
544  USART_EnableTransmitInterrupt();
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;
567  USART_EnableTransmitInterrupt();
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;
654  USART_EnableTransmitInterrupt();
655  return OK;
656 }
657 
663 void USART_CancelTransmit()
664 {
665  USART_DisableTransmitInterrupt();
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;
684  USART_DisableReceiveInterrupt();
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;
703  USART_DisableTransmitInterrupt();
704  }
705 }
706 
707 #endif /* AVR_USART_H_ */

إرسال تعليق

Post a Comment (0)

أحدث أقدم