-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvrUartBaud.c
99 lines (83 loc) · 3.16 KB
/
AvrUartBaud.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
/*********************************************************************************/
/*
* Author : Jeong Hyun Gu
* File name : AvrUartBaud.c
*/
/*********************************************************************************/
#include "AvrUartBaud.h"
/*********************************************************************************/
#if(AVR_UART_BAUD_REVISION_DATE != 20200108)
#error wrong include file. (AvrUartBaud.h)
#endif
/*********************************************************************************/
/** Global variable **/
/*********************************************************************************/
tU8 AvrUartBaudControlInit(tag_UartBaudControl *BaudCtrl, tU32 CpuClock, tU8 *pUBRRL, tU8 *pUBRRH)
{
/*
1) Parameter
- BaudCtrl : 'tag_UartBaudControl'의 인스턴스 주소.
- CpuClock : Target CPU의 Clock Speed.
- pUBRRL, pUBRRH : Uart Baud Rate Register의 주소.
2) Description
- Baud를 관리할 'tag_UartBaudControl' 인스턴스를 초기화한다.
- 본 초기화는 한번만 실행하면 된다.
- 다음 함수들은 반드시 본 초기화를 실행한 후 호출해야 한다.
AvrUartBaudChange();
*/
if((pUBRRL) == null || (pUBRRH == null))
{
return false;
}
BaudCtrl->CpuClock = CpuClock;
BaudCtrl->Baud = BAUD_INIT;
BaudCtrl->pUBRRL = pUBRRL;
BaudCtrl->pUBRRH = pUBRRH;
BaudCtrl->Bit.Init = true;
return BaudCtrl->Bit.Init;
}
/*********************************************************************************/
void AvrUartBaudChange(tag_UartBaudControl *BaudCtrl, Enum_BaudRate Baud)
{
/*
1) Parameter
- BaudCtrl : 'tag_UartBaudControl'의 인스턴스 주소.
- Baud : 변경하고자 하는 baud rate.
2) Description
- 'BaudCtrl' 인스턴스의 baud rate를 변경한다.
*/
if(BaudCtrl->Bit.Init == false)
{
return;
/* error */
}
if(BaudCtrl->Baud != Baud)
{
BaudCtrl->Baud = Baud;
switch(Baud)
{
default :
case BAUD_9600 :
*BaudCtrl->pUBRRH = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 9600, 0) >> 8);
*BaudCtrl->pUBRRL = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 9600, 0) & 0x00FF);
break;
case BAUD_19200 :
*BaudCtrl->pUBRRH = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 19200, 0) >> 8);
*BaudCtrl->pUBRRL = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 19200, 0) & 0x00FF);
break;
case BAUD_38400 :
*BaudCtrl->pUBRRH = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 38400, 0) >> 8);
*BaudCtrl->pUBRRL = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 38400, 0) & 0x00FF);
break;
case BAUD_57600 :
*BaudCtrl->pUBRRH = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 57600, 0) >> 8);
*BaudCtrl->pUBRRL = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 57600, 0) & 0x00FF);
break;
case BAUD_115200 :
*BaudCtrl->pUBRRH = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 115200, 0) >> 8);
*BaudCtrl->pUBRRL = (unsigned char) (GET_UBRR(BaudCtrl->CpuClock, 115200, 0) & 0x00FF);
break;
}
}
}
/*********************************************************************************/