forked from renesas-rz/rzg2_flash_writer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scifdrv.c
203 lines (178 loc) · 5.26 KB
/
scifdrv.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) 2015-2022, Renesas Electronics Corporation. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "common.h"
#include "scifdrv.h"
#include "bit.h"
#include "rzg2l_def.h"
#include "scif_regs.h"
#include "cpg_regs.h"
#include "pfc_regs.h"
#include "devdrv.h"
/************************************************************************/
/* */
/* Debug Seirial(SCIF ch0) */
/* */
/************************************************************************/
int32_t PutCharSCIF0(char outChar)
{
while(!(0x60U & *((volatile uint16_t*)SCIF0_FSR)));
*((volatile unsigned char*)SCIF0_FTDR) = outChar;
*((volatile uint16_t*)SCIF0_FSR) &= ~0x60U; /* TEND,TDFE clear */
return(0);
}
int32_t GetCharSCIF0(char *inChar)
{
do
{
if (0x91U & *((volatile uint16_t *)SCIF0_FSR))
{
*((volatile uint16_t *)SCIF0_FSR) &= ~0x91U;
}
if (0x01U & *((volatile uint16_t *)SCIF0_LSR))
{
PutStr("ORER", 1);
*((volatile uint16_t *)SCIF0_LSR) &= ~0x01U;
}
} while (!(0x02U & *((volatile uint16_t *)SCIF0_FSR)));
*inChar = *((volatile unsigned char*)SCIF0_FRDR);
*((volatile uint16_t*)SCIF0_FSR) &= ~0x02U;
return(0);
}
void PowerOnScif0(void)
{
volatile uint32_t dataL;
dataL = *((volatile uint32_t*)CPG_CLKMON_SCIF);
if ((dataL & BIT0) == 0U)
{
/* case SCIF ch0 Standby */
*((volatile uint32_t*)CPG_CLKON_SCIF) = (BIT0 | BIT16);
do
{
dataL = *((volatile uint32_t*)CPG_CLKMON_SCIF);
} while ((dataL & BIT0) == 0U); /* wait until bit0=1 */
}
dataL = *((volatile uint32_t*)CPG_RST_SCIF);
dataL |= 0x00010001U;
*((volatile uint32_t*)CPG_RST_SCIF) = dataL;
do
{
dataL = *((volatile uint32_t*)CPG_RSTMON_SCIF);
} while ((dataL & BIT0) == 1U); /* wait until bit0=1 */
}
void WaitPutScif0SendEnd(void)
{
volatile uint16_t dataW;
while(1)
{
dataW = *((volatile uint16_t*)SCIF0_FSR);
if (dataW & BIT6)
{
break;
}
}
}
void InitScif0PinFunction(void)
{
uint8_t dataB;
uint32_t dataL;
/* SCIF ch0 */
dataL = *((volatile uint32_t*)CPG_CLKON_GPIO);
dataL |= 0x00010001U;
*((volatile uint32_t*)CPG_CLKON_GPIO) = dataL;
do
{
dataL = *((volatile uint32_t*)CPG_CLKMON_GPIO);
} while ((dataL & BIT0) == 0U); /* wait until bit0=1 */
dataL = *((volatile uint32_t*)CPG_RST_GPIO);
dataL |= 0x00070007U;
*((volatile uint32_t*)CPG_RST_GPIO) = dataL;
do
{
dataL = *((volatile uint32_t*)CPG_RSTMON_GPIO);
} while ((dataL & BIT0) == 1U); /* wait until bit0=1 */
dataL = *((volatile uint32_t*)PFC_PWPR);
dataL &= ~0x00000080U; /* B0WI = 0(Enable) */
*((volatile uint32_t*)PFC_PWPR) = dataL;
dataL |= 0x00000040U; /* PFCWE = 1(Enable) */
*((volatile uint32_t*)PFC_PWPR) = dataL;
dataB = *((volatile uint8_t*)PFC_PMC36);
dataB &= ~0x03U;
dataB |= 0x03U; /* P38_0(TXD), P38_1(RXD) */
*((volatile uint8_t*)PFC_PMC36) = dataB;
// dataB = *((volatile uint8_t*)PFC_PMC37);
// dataB &= ~0x07U;
// dataB |= 0x07U; /* P39_0(SCK), P39_1(CTS), P39_2(RTS) */
// *((volatile uint8_t*)PFC_PMC37) = dataB;
dataL = *((volatile uint32_t*)PFC_PFC36);
dataL &= ~0x00000077U; /* P38_0(TXD), P38_1(RXD) */
dataL |= 0x00000011U;
*((volatile uint32_t*)PFC_PFC36) = dataL;
// dataL = *((volatile uint32_t*)PFC_PFC37);
// dataL &= ~0x00000777U; /* P39_0(SCK), P39_1(CTS), P39_2(RTS) */
// dataL |= 0x00000111U;
// *((volatile uint32_t*)PFC_PFC37) = dataL;
// dataL = *((volatile uint32_t*)PFC_PWPR);
// dataL &= ~0x00000040U; /* PFCWE = 0(Disable) */
// *((volatile uint32_t*)PFC_PWPR) = dataL;
// dataL |= 0x00000080U; /* B0WI = 1(Disable) */
// *((volatile uint32_t*)PFC_PWPR) = dataL;
}
void InitScif0_SCIFCLK(uint32_t baudrate)
{
volatile uint16_t dataW;
uint32_t prr;
dataW = *((volatile uint16_t*)SCIF0_LSR); /* dummy read */
*((volatile uint16_t*)SCIF0_LSR) = 0x0000U; /* clear ORER bit */
*((volatile uint16_t*)SCIF0_FSR) = 0x0000U; /* clear all error bit */
*((volatile uint16_t*)SCIF0_SCR) = 0x0000U; /* clear SCR.TE & SCR.RE*/
*((volatile uint16_t*)SCIF0_FCR) = 0x0006U; /* reset tx-fifo, reset rx-fifo. */
*((volatile uint16_t*)SCIF0_FSR) = 0x0000U; /* clear ER, TEND, TDFE, BRK, RDF, DR */
*((volatile uint16_t*)SCIF0_SCR) = 0x0000U; /* internal clock P1/1 */
*((volatile uint16_t*)SCIF0_SMR) = 0x0000U; /* 8bit data, no-parity, 1 stop, Po/1 */
*((volatile uint8_t*)SCIF0_SEMR) = 0x00U;
SoftDelay(100);
if (baudrate == 115200)
{
*((volatile uint8_t*)SCIF0_BRR) = 0x1AU; /* 115200bps */
}
else
{
*((volatile uint8_t*)SCIF0_BRR) = 0x01U; /* 921600bps */
}
*((volatile uint8_t*)SCIF0_SEMR) = 0x30U;
if (baudrate == 115200)
{
*((volatile uint8_t*)SCIF0_MDDR) = 0xFFU; /* 115200bps */
}
else
{
*((volatile uint8_t*)SCIF0_MDDR) = 0x97U; /* 921600bps */
}
SoftDelay(100);
*((volatile uint16_t*)SCIF0_FCR) = 0x0000U; /* reset-off tx-fifo, rx-fifo. */
*((volatile uint16_t*)SCIF0_SCR) = 0x0030U; /* enable TE, RE */
SoftDelay(100);
}
uint32_t SCIF_TerminalInputCheck(char* str)
{
char result = 0;
if (0x91U & *((volatile uint16_t *)SCIF0_FSR))
{
*((volatile uint16_t *)SCIF0_FSR) &= ~0x91U;
}
if (0x01U & *((volatile uint16_t *)SCIF0_LSR))
{
PutStr("ORER",1);
*((volatile uint16_t *)SCIF0_LSR) &= ~0x01U;
}
if (0x02U & *((volatile uint16_t *)SCIF0_FSR))
{
*str = *((volatile unsigned char*)SCIF0_FRDR);
*((volatile uint16_t*)SCIF0_FSR) &= ~0x02U;
result = 1;
}
return result;
}