39 #include <avr/interrupt.h> 43 #pragma message ( "avr_usart.h included" ) 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 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);
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()
193 for (
int i = 0; i < USART_ReceiveBufferLength; i++)
195 USART_ReceiveBuffer[i] = 0;
198 USART_ReceiveBuffer = 0;
199 USART_ReceiveBusyFlag = 0;
200 USART_ReceiveBufferIndex = 0;
201 USART_ReceiveBufferLength = 0;
209 void USART_FlushTransmitBuffer()
211 for (
int i = 0; i < USART_TransmitBufferLength; i++)
213 USART_TransmitBuffer[i] = 0;
216 USART_TransmitBuffer = 0;
217 USART_TransmitBusyFlag = 0;
218 USART_TransmitBufferIndex = 0;
219 USART_TransmitBufferLength = 0;
227 inline void USART_EnableTransmitInterrupt()
229 UCSR0B |= USART_DATA_REGISTER_EMPTY_INTERRUPT;
237 inline void USART_DisableTransmitInterrupt()
239 UCSR0B &= ~USART_DATA_REGISTER_EMPTY_INTERRUPT;
247 inline void USART_EnableReceiveInterrupt()
249 UCSR0B |= USART_RX_COMPLETE_INTERRUPT;
257 inline void USART_DisableReceiveInterrupt()
259 UCSR0B &= ~USART_RX_COMPLETE_INTERRUPT;
276 void USART_Init(USART_ConfigData Data)
278 uint16_t USART_BaudPrescaler = 0;
280 if (Data.UsartMode == (USART_MODE_ASYNCHRONOUS))
282 USART_BaudPrescaler = (((F_CPU / (Data.BaudRate * 16UL))) - 1);
284 else if (Data.UsartMode == (USART_MODE_SYNCHRONOUS))
286 USART_BaudPrescaler = (((F_CPU / (Data.BaudRate * 2UL))) - 1);
290 UBRR0H = USART_BaudPrescaler >> 8;
291 UBRR0L = USART_BaudPrescaler;
294 UCSR0C = Data.UsartMode | Data.ParityBit | Data.StopBit | Data.DataBit;
297 UCSR0B = (1<<RXEN0) | (1<<TXEN0);
319 uint8_t USART_ReceiveBlocking()
322 while ((UCSR0A & (1<<RXC0)) == 0);
333 uint8_t USART_ReceiveByte(uint8_t *Buffer)
335 while (USART_ReceiveBusyFlag)
339 USART_ReceiveBuffer = Buffer;
340 USART_ReceiveBufferLength = 1;
341 USART_ReceiveBufferIndex = 0;
342 USART_ReceiveBusyFlag = 1;
343 USART_EnableReceiveInterrupt();
353 uint8_t USART_ReceiveTwoBytes(uint16_t *Buffer)
355 while (USART_ReceiveBusyFlag)
359 USART_ReceiveBuffer = (uint8_t*)Buffer;
360 USART_ReceiveBufferLength = 2;
361 USART_ReceiveBufferIndex = 0;
362 USART_ReceiveBusyFlag = 1;
363 USART_EnableReceiveInterrupt();
374 uint8_t USART_ReceiveBytes(uint8_t *Buffer, uint16_t Size)
376 if (Size == 0)
return ERROR;
378 while (USART_ReceiveBusyFlag)
382 USART_ReceiveBuffer = Buffer;
383 USART_ReceiveBufferLength = Size;
384 USART_ReceiveBufferIndex = 0;
385 USART_ReceiveBusyFlag = 1;
386 USART_EnableReceiveInterrupt();
396 uint8_t USART_ReceiveChar(
char *Character)
398 while (USART_ReceiveBusyFlag)
402 USART_ReceiveBuffer = (uint8_t*)Character;
403 USART_ReceiveBufferLength = 1;
404 USART_ReceiveBufferIndex = 0;
405 USART_ReceiveBusyFlag = 1;
406 USART_EnableReceiveInterrupt();
417 uint8_t USART_ReceiveInt(
int *Number, uint8_t Size)
419 if (Size == 0)
return ERROR;
421 char localbuffer[7] = {0};
422 while (USART_ReceiveBusyFlag)
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);
443 uint8_t USART_ReceiveLong(
long *Number, uint8_t Size)
445 if (Size == 0)
return ERROR;
447 char localbuffer[12] = {0};
448 while (USART_ReceiveBusyFlag)
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);
469 uint8_t USART_ReceiveString(
char *Buffer, uint16_t Size)
471 if (Size == 0)
return ERROR;
473 while (USART_ReceiveBusyFlag)
477 USART_ReceiveBuffer = (uint8_t*)Buffer;
478 USART_ReceiveBufferLength = Size;
479 USART_ReceiveBufferIndex = 0;
480 USART_ReceiveBusyFlag = 1;
481 USART_EnableReceiveInterrupt();
490 void USART_CancelReceive()
492 USART_DisableReceiveInterrupt();
493 USART_ReceiveBusyFlag = 0;
501 uint8_t USART_TransmitBlocking(uint8_t *Buffer)
503 while ((UCSR0A & (1<<UDRE0)) == 0);
514 uint8_t USART_TransmitByte(uint8_t *Buffer)
516 while (USART_TransmitBusyFlag)
520 USART_TransmitBuffer = Buffer;
521 USART_TransmitBufferLength = 1;
522 USART_TransmitBufferIndex = 0;
523 USART_TransmitBusyFlag = 1;
524 USART_EnableTransmitInterrupt();
534 uint8_t USART_TransmitTwoBytes(uint16_t *Buffer)
536 while (USART_TransmitBusyFlag)
540 USART_TransmitBuffer = (uint8_t*)Buffer;
541 USART_TransmitBufferLength = 2;
542 USART_TransmitBufferIndex = 0;
543 USART_TransmitBusyFlag = 1;
544 USART_EnableTransmitInterrupt();
555 uint8_t USART_TransmitBytes(uint8_t *Buffer, uint16_t Size)
557 if (Size == 0)
return ERROR;
559 while (USART_TransmitBusyFlag)
563 USART_TransmitBuffer = Buffer;
564 USART_TransmitBufferLength = Size;
565 USART_TransmitBufferIndex = 0;
566 USART_TransmitBusyFlag = 1;
567 USART_EnableTransmitInterrupt();
577 uint8_t USART_TransmitChar(
char Character)
579 uint8_t* data = (uint8_t*)&Character;
580 return USART_TransmitByte(data);
589 uint8_t USART_TransmitInt(
int Number)
591 while (USART_TransmitBusyFlag)
595 static uint8_t localbuffer[7] = {0};
596 uint16_t Length = UTILS_IntToString(Number,(
char*)localbuffer);
597 return USART_TransmitBytes(localbuffer,Length);
606 uint8_t USART_TransmitLong(
long Number)
608 while (USART_TransmitBusyFlag)
612 static uint8_t localbuffer[12] = {0};
613 uint16_t Length = UTILS_LongToString(Number,(
char*)localbuffer);
614 return USART_TransmitBytes(localbuffer,Length);
624 uint8_t USART_TransmitFloat(
float Number,uint8_t Precision)
626 while (USART_TransmitBusyFlag)
630 static uint8_t localbuffer[17] = {0};
631 uint16_t Length = UTILS_FloatToString(Number,(
char*)localbuffer,Precision);
632 return USART_TransmitBytes(localbuffer,Length);
641 uint8_t USART_TransmitString(
char *Buffer)
643 while (USART_TransmitBusyFlag)
647 const char *p = Buffer;
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();
663 void USART_CancelTransmit()
665 USART_DisableTransmitInterrupt();
666 USART_TransmitBusyFlag = 0;
676 if(USART_ReceiveBufferIndex < USART_ReceiveBufferLength)
678 USART_ReceiveBuffer[USART_ReceiveBufferIndex] = UDR0;
679 USART_ReceiveBufferIndex++;
681 if(USART_ReceiveBufferIndex >= USART_ReceiveBufferLength)
683 USART_ReceiveBusyFlag = 0;
684 USART_DisableReceiveInterrupt();
695 if(USART_TransmitBufferIndex < USART_TransmitBufferLength)
697 UDR0 = USART_TransmitBuffer[USART_TransmitBufferIndex];
698 USART_TransmitBufferIndex++;
700 if(USART_TransmitBufferIndex >= USART_TransmitBufferLength)
702 USART_TransmitBusyFlag = 0;
703 USART_DisableTransmitInterrupt();
Post a Comment