-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
89 lines (73 loc) · 2.29 KB
/
serial.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
#include "defines.h"
#include "serial.h"
#define SERIAL_SCI_NUM 3
#define H8_3069F_SCI0 ((volatile struct h8_3069f_sci *)0xffffb0)
#define H8_3069F_SCI1 ((volatile struct h8_3069f_sci *)0xffffb8)
#define H8_3069F_SCI2 ((volatile struct h8_3069f_sci *)0xffffc0)
struct h8_3069f_sci {
volatile uint8 smr;
volatile uint8 brr;
volatile uint8 scr;
volatile uint8 tdr;
volatile uint8 ssr;
volatile uint8 rdr;
volatile uint8 scmr;
};
#define H8_3069F_SCI_SMR_CKS_PER1 (0<<0)
#define H8_3069F_SCI_SMR_CKS_PER4 (1<<0)
#define H8_3069F_SCI_SMR_CKS_PER16 (2<<0)
#define H8_3069F_SCI_SMR_CKS_PER64 (3<<0)
#define H8_3069F_SCI_SMR_MP (1<<2)
#define H8_3069F_SCI_SMR_STOP (1<<3)
#define H8_3069F_SCI_SMR_OE (1<<4)
#define H8_3069F_SCI_SMR_PE (1<<5)
#define H8_3069F_SCI_SMR_CHR (1<<6)
#define H8_3069F_SCI_SMR_CA (1<<7)
#define H8_3069F_SCI_SCR_CKE0 (1<<0)
#define H8_3069F_SCI_SCR_CKE1 (1<<1)
#define H8_3069F_SCI_SCR_TEIE (1<<2)
#define H8_3069F_SCI_SCR_MPIE (1<<3)
#define H8_3069F_SCI_SCR_RE (1<<4)
#define H8_3069F_SCI_SCR_TE (1<<5)
#define H8_3069F_SCI_SCR_RIE (1<<6)
#define H8_3069F_SCI_SCR_TIE (1<<7)
#define H8_3069F_SCI_SSR_MPBT (1<<0)
#define H8_3069F_SCI_SSR_MPB (1<<1)
#define H8_3069F_SCI_SSR_TEND (1<<2)
#define H8_3069F_SCI_SSR_PER (1<<3)
#define H8_3069F_SCI_SSR_FERERS (1<<4)
#define H8_3069F_SCI_SSR_ORER (1<<5)
#define H8_3069F_SCI_SSR_RDRF (1<<6)
#define H8_3069F_SCI_SSR_TDRE (1<<7)
static struct {
volatile struct h8_3069f_sci *sci;
} regs[SERIAL_SCI_NUM] = {
{ H8_3069F_SCI0 },
{ H8_3069F_SCI1 },
{ H8_3069F_SCI2 },
};
int serial_init(int index)
{
volatile struct h8_3069f_sci *sci = regs[index].sci;
sci->scr = 0;
sci->smr = 0;
sci->brr = 64;
sci->scr = H8_3069F_SCI_SCR_RE | H8_3069F_SCI_SCR_TE;
sci->ssr = 0;
return 0;
}
int serial_is_send_enable(int index)
{
volatile struct h8_3069f_sci *sci = regs[index].sci;
return (sci->ssr & H8_3069F_SCI_SSR_TDRE);
}
int serial_send_byte(int index, unsigned char c)
{
volatile struct h8_3069f_sci *sci = regs[index].sci;
while (!serial_is_send_enable(index)) {
;
}
sci->tdr = c;
sci->ssr &= ~H8_3069F_SCI_SSR_TDRE;
return 0;
}