embox

Форк
0
/
msp430usci.c 
102 строки · 2.1 Кб
1
/**
2
 * @file
3
 * @brief MSP430's universal serial communication interface, uart mode
4
 *
5
 * @author  Anton Kozlov
6
 * @date    02.08.2013
7
 */
8

9
#include <hal/reg.h>
10
#include <drivers/diag.h>
11

12
#define USCI_BASE 	0x60
13
#define CTL0 		(USCI_BASE + 0)
14
#define CTL1 		(USCI_BASE + 1)
15
#define BR0  		(USCI_BASE + 2)
16
#define BR1  		(USCI_BASE + 3)
17
#define MCTL 		(USCI_BASE + 4)
18
#define STAT 		(USCI_BASE + 5)
19
#define RXB  		(USCI_BASE + 6)
20
#define TXB  		(USCI_BASE + 7)
21
#define ABCTL  		0x5d
22
#define IE2    		0x1
23
#define IFG2   		0x3
24

25
#define CTL1_RST	(1 << 0)
26
#define CTL1_SMCLK	(2 << 6)
27

28
#define MCTL_BRF_OFF 	4
29
#define MCTL_BRS_OFF 	1
30
#define MCTL_UCOS16O 	0
31

32
#define RXIFG		(1 << 0)
33
#define TXIFG 		(1 << 1)
34

35
#define P1_IN	    	0x20
36
#define P1_OUT	    	0x21
37
#define P1_PORTSEL1 	0x26
38
#define P1_PORTSEL2 	0x41
39

40
#define UARTRXD 	(1 << 1)
41
#define UARTTXD 	(1 << 2)
42

43
/* below is for 9660 from official doc */
44
#define PAR_BR 		1666
45
#define PAR_BRS		6
46
#define PAR_BRF 	0
47
#define PAR_UCO		0
48

49
static int msp430usci_diag_init(const struct diag *diag) {
50

51
	/*reset uart, select clock*/
52
	REG_STORE(CTL1, 0x01);
53
	REG_ORIN(CTL1, CTL1_SMCLK);
54

55
	/* select 8-1-no_parity, manual baudrate.
56
 	 * This is defaults, just to be sure */
57
	REG_STORE(CTL0, 0);
58
	REG_STORE(ABCTL, 0);
59

60
	/* select baudrate */
61
	REG_STORE(BR0, PAR_BR & 0xff);
62
	REG_STORE(BR1, PAR_BR >> 8);
63
	REG_STORE(MCTL, (PAR_BRF << MCTL_BRF_OFF) |
64
		        (PAR_BRS << MCTL_BRS_OFF) |
65
			(PAR_UCO << MCTL_UCOS16O));
66

67
	/*configure i/o pins */
68
	REG_ORIN(P1_PORTSEL1, UARTRXD);
69
	REG_ORIN(P1_PORTSEL2, UARTRXD);
70
	REG_ORIN(P1_IN, UARTRXD);
71

72
	REG_ORIN(P1_PORTSEL1, UARTTXD);
73
	REG_ORIN(P1_PORTSEL2, UARTTXD);
74
	REG_ORIN(P1_OUT, UARTTXD);
75

76
	/* release the reset */
77
	REG_ANDIN(CTL1, ~1);
78

79
	return 0;
80
}
81

82
static void msp430usci_diag_putc(const struct diag *diag, char ch) {
83

84
	REG_STORE(TXB, ch);
85

86
	while (!(REG_LOAD(IFG2) & TXIFG));
87
}
88

89
static char msp430usci_diag_getc(const struct diag *diag) {
90
	return REG_LOAD(RXB);
91
}
92

93
static int msp430usci_diag_kbhit(const struct diag *diag) {
94
	return (REG_LOAD(IFG2) & RXIFG);
95
}
96

97
DIAG_OPS_DEF(
98
		.init = msp430usci_diag_init,
99
		.putc = msp430usci_diag_putc,
100
		.getc = msp430usci_diag_getc,
101
		.kbhit = msp430usci_diag_kbhit,
102
);
103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.