source: rc_os_nios2/atk2-sc1_1.3.2/obj/nios2_dev_rc/comuart.c@ 128

Last change on this file since 128 was 128, checked in by ertl-honda, 9 years ago

追加.

File size: 4.7 KB
Line 
1#include "Os.h"
2#include "t_stdlib.h"
3#include "prc_sil.h"
4#include "comuart.h"
5
6/*
7 * ƒŒƒWƒXƒ^ƒIƒtƒZƒbƒg
8 */
9#define UART_RXDATA_OFFSET UINT_C(0x00)
10#define UART_TXDATA_OFFSET UINT_C(0x04)
11#define UART_STATUS_OFFSET UINT_C(0x08)
12#define UART_CONTROL_OFFSET UINT_C(0x0c)
13#define UART_DIVISOR_OFFSET UINT_C(0x10)
14
15/*
16 * ƒŒƒWƒXƒ^ƒrƒbƒg’è‹`
17 */
18#define UART_STATUS_TRDY UINT_C(0x0040)
19#define UART_STATUS_RRDY UINT_C(0x0080)
20
21#define UART_CONTROL_ITRDY UINT_C(0x0040)
22#define UART_CONTROL_IRRDY UINT_C(0x0080)
23
24/*
25 * UART
26 */
27#define RxBUFF_SIZE 256
28#define TxBUFF_SIZE 256
29
30static uint8 s_rxbuff[RxBUFF_SIZE];
31static uint8 s_txbuff[TxBUFF_SIZE];
32
33static int s_rxbuff_tail;
34static int s_rxbuff_head;
35static int s_rxbuff_cnt;
36static int s_txbuff_tail;
37static int s_txbuff_head;
38static int s_txbuff_cnt;
39
40static boolean s_do_tx;
41
42
43static void
44ena_rx_int(void){
45 sil_wrw_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET),
46 sil_rew_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET))
47 | UART_CONTROL_IRRDY);
48}
49
50static void
51dis_rx_int(void){
52 sil_wrw_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET),
53 sil_rew_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET))
54 & ~UART_CONTROL_IRRDY);
55}
56
57static void
58ena_tx_int(void){
59 sil_wrw_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET),
60 sil_rew_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET))
61 | UART_CONTROL_ITRDY);
62}
63
64static void
65dis_tx_int(void){
66 sil_wrw_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET),
67 sil_rew_iop((void *) (COMUART_BASE + UART_CONTROL_OFFSET))
68 & ~UART_CONTROL_ITRDY);
69}
70
71static void
72comuart_putc(unsigned char c){
73 sil_wrw_iop((void *) (COMUART_BASE + UART_TXDATA_OFFSET), (uint32) c);
74}
75
76static unsigned char
77comuart_getc(void){
78 return((uint8) sil_rew_iop((void *) (COMUART_BASE + UART_RXDATA_OFFSET)));
79}
80
81static boolean
82comuart_getready(void)
83{
84 return((sil_rew_iop((void *) (COMUART_BASE + UART_STATUS_OFFSET)) & UART_STATUS_RRDY) != 0U);
85}
86
87static boolean
88comuart_putready(void)
89{
90 return((sil_rew_iop((void *) (COMUART_BASE + UART_STATUS_OFFSET)) & UART_STATUS_TRDY) != 0U);
91}
92
93void
94comuart_pputc(unsigned char c){
95 while(!(comuart_putready())){}
96 comuart_putc(c);
97}
98
99/*
100 * ‰Šú‰»
101 */
102void
103comuart_init(void)
104{
105 /*
106 * ŽóMŠ„ž‚Ý‹–‰Â
107 */
108 ena_rx_int();
109}
110
111
112/*
113 * ŽóM‚µ‚½ƒf[ƒ^’·
114 */
115int
116comuart_get_rxcnt(void)
117{
118 return s_rxbuff_cnt;
119}
120
121/*
122 * ŽóM
123 */
124boolean
125comuart_receive(unsigned char *data, int len)
126{
127 boolean result = TRUE;
128 int loop;
129
130 SuspendOSInterrupts();
131 if (s_rxbuff_cnt < len) {
132 result = FALSE;
133 }else {
134 for(loop = 0; loop < len; loop++) {
135 *data++ = s_rxbuff[s_rxbuff_head++];
136 s_rxbuff_head = s_rxbuff_head % RxBUFF_SIZE;
137 s_rxbuff_cnt--;
138 }
139 }
140 ResumeOSInterrupts();
141
142 return result;
143}
144
145/*
146 * ‘—M
147 */
148boolean
149comuart_send(const unsigned char *data, int len)
150{
151 int loop;
152 char c;
153
154 /* ‹ó‚«‚ª–³‚¯‚ê‚΃Gƒ‰[ */
155 if (s_txbuff_cnt + len > TxBUFF_SIZE) {
156 return FALSE;
157 }
158
159 /* ƒŠƒ“ƒOƒoƒbƒtƒ@‚ɃRƒs[ */
160 for(loop = 0; loop < len; loop++){
161 s_txbuff[s_txbuff_tail++] = *data++;
162 s_txbuff_tail = s_txbuff_tail % TxBUFF_SIZE;
163 s_txbuff_cnt++;
164 }
165
166 if (!(s_do_tx)) {
167 SuspendOSInterrupts();
168 /* ƒŠƒ“ƒOƒoƒbƒtƒ@‚©‚çŽæ‚èo‚µ‘—M */
169 c = s_txbuff[s_txbuff_head++];
170 s_txbuff_head = s_txbuff_head % TxBUFF_SIZE;
171 s_txbuff_cnt--;
172 s_do_tx = TRUE;
173 comuart_putc(c);
174 ResumeOSInterrupts();
175
176 /* ŽóMŠ„ž‚Ý‹–‰Â */
177 if(s_txbuff_cnt > 0){
178 ena_tx_int();
179 }
180 }
181
182 return TRUE;
183}
184
185/*
186 * UART2Š„ž‚݃nƒ“ƒhƒ‰
187 */
188#define TASK_NONE 0xFFFFFFFF
189static TaskType COMUartWaitTask = TASK_NONE;
190
191/*
192 * Š„ž‚݃nƒ“ƒhƒ‰
193 */
194ISR(COMUART_Interrupt)
195{
196 char c;
197
198 if (comuart_putready() != FALSE) {
199 /* ‘—Mˆ— */
200 if (s_txbuff_cnt == 0) {
201 /* ƒoƒbƒtƒ@‚ª‹ó */
202 s_do_tx = FALSE;
203 dis_tx_int();
204 }
205 else {
206 /* ƒoƒbƒtƒ@‚©‚çƒf[ƒ^‚ðŽæ‚èo‚µo—Í */
207 c = s_txbuff[s_txbuff_head++];
208 s_txbuff_head = s_txbuff_head % TxBUFF_SIZE;
209 s_txbuff_cnt--;
210 comuart_putc(c);
211 s_do_tx = TRUE;
212 }
213 }
214
215 if (comuart_getready() != FALSE) {
216 /* ŽóMˆ— */
217 c = comuart_getc();
218
219 if (s_rxbuff_cnt < RxBUFF_SIZE) {
220 s_rxbuff[s_rxbuff_tail++] = c;
221 s_rxbuff_tail = s_rxbuff_tail % RxBUFF_SIZE;
222 s_rxbuff_cnt++;
223 }
224 else {
225 com_error();
226 }
227
228 if (COMUartWaitTask != TASK_NONE) {
229 SetEvent(COMUartWaitTask, COMUartEvt);
230 }
231 }
232}
233
234/*
235 * UARTE2‚̃Cƒxƒ“ƒg‘Ò‚¿
236 */
237void
238WaitCOMUartEvent(void) {
239
240 TaskType task_id;
241 GetTaskID(&task_id);
242
243 COMUartWaitTask = task_id;
244 if(comuart_get_rxcnt() == 0) {
245 WaitEvent(COMUartEvt);
246 ClearEvent(COMUartEvt);
247 }
248 COMUartWaitTask = TASK_NONE;
249}
Note: See TracBrowser for help on using the repository browser.