[136] | 1 | /*
|
---|
| 2 | * TOPPERS/ASP Kernel
|
---|
| 3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
| 4 | * Advanced Standard Profile Kernel
|
---|
| 5 | *
|
---|
| 6 | * Copyright (C) 2007,2011,2013 by Embedded and Real-Time Systems Laboratory
|
---|
| 7 | * Graduate School of Information Science, Nagoya Univ., JAPAN
|
---|
| 8 | *
|
---|
| 9 | * ä¸è¨èä½æ¨©è
|
---|
| 10 | ã¯ï¼ä»¥ä¸ã®(1)ï½(4)ã®æ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§
|
---|
| 11 | * ã¢ï¼æ¬ã½ããã¦ã§ã¢ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹
|
---|
| 12 | * å¤ã»åé
|
---|
| 13 | å¸ï¼ä»¥ä¸ï¼å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
| 14 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
| 15 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
| 16 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
| 17 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 18 | * ç¨ã§ããå½¢ã§åé
|
---|
| 19 | å¸ããå ´åã«ã¯ï¼åé
|
---|
| 20 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
| 21 | * è
|
---|
| 22 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
| 23 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 24 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
| 25 | * ç¨ã§ããªãå½¢ã§åé
|
---|
| 26 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
| 27 | * ã¨ï¼
|
---|
| 28 | * (a) åé
|
---|
| 29 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
| 30 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
| 31 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
| 32 | * (b) åé
|
---|
| 33 | å¸ã®å½¢æ
|
---|
| 34 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
| 35 | * å ±åãããã¨ï¼
|
---|
| 36 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
| 37 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 38 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
| 39 | 責ãããã¨ï¼
|
---|
| 40 | * ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®ã¦ã¼ã¶ã¾ãã¯ã¨ã³ãã¦ã¼ã¶ããã®ãããªãç
|
---|
| 41 | * ç±ã«åºã¥ãè«æ±ãããï¼ä¸è¨èä½æ¨©è
|
---|
| 42 | ããã³TOPPERSããã¸ã§ã¯ãã
|
---|
| 43 | * å
|
---|
| 44 | 責ãããã¨ï¼
|
---|
| 45 | *
|
---|
| 46 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
| 47 | ã
|
---|
| 48 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ç¹å®ã®ä½¿ç¨ç®ç
|
---|
| 49 | * ã«å¯¾ããé©åæ§ãå«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§
|
---|
| 50 | * ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ã
|
---|
| 51 | * ã®è²¬ä»»ãè² ããªãï¼
|
---|
| 52 | *
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * ã·ãªã¢ã«ãã©ã¤ãï¼STM32F USARTç¨ï¼
|
---|
| 57 | */
|
---|
| 58 |
|
---|
| 59 | #include <kernel.h>
|
---|
| 60 | #include <sil.h>
|
---|
| 61 | #include "usart.h"
|
---|
| 62 | #include "target_syssvc.h"
|
---|
| 63 |
|
---|
| 64 | /*
|
---|
| 65 | * ã¬ã¸ã¹ã¿è¨å®å¤
|
---|
| 66 | */
|
---|
| 67 | #define PORT2SIOPID(x) ((x) + 1)
|
---|
| 68 | #define INDEX_PORT(x) ((x) - 1)
|
---|
| 69 | #define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * USARTã¬ã¸ã¹ã¿å®ç¾©
|
---|
| 73 | */
|
---|
| 74 | #define USART_SR(x) (x)
|
---|
| 75 | #define USART_DR(x) (x + 0x04)
|
---|
| 76 | #define USART_BRR(x) (x + 0x08)
|
---|
| 77 | #define USART_CR1(x) (x + 0x0C)
|
---|
| 78 | #define USART_CR2(x) (x + 0x10)
|
---|
| 79 | #define USART_CR3(x) (x + 0x14)
|
---|
| 80 | #define USART_GTPR(x) (x + 0x18)
|
---|
| 81 |
|
---|
| 82 | #define SR_TXE (0x0080)
|
---|
| 83 | #define SR_RXNE (0x0020)
|
---|
| 84 | #define SR_ORE (0x0008)
|
---|
| 85 | #define SR_FE (0x0002)
|
---|
| 86 | #define SR_PE (0x0001)
|
---|
| 87 | #define CR1_UE (0x2000)
|
---|
| 88 | #define CR1_TXEIE (0x0080)
|
---|
| 89 | #define CR1_RXNEIE (0x0020)
|
---|
| 90 | #define CR1_TE (0x0008)
|
---|
| 91 | #define CR1_RE (0x0004)
|
---|
| 92 | #define CR3_EIE (0x0001)
|
---|
| 93 |
|
---|
| 94 | /*
|
---|
| 95 | * ã·ãªã¢ã«ãã¼ãã®ç®¡çãããã¯
|
---|
| 96 | */
|
---|
| 97 | struct sio_port_control_block {
|
---|
| 98 | ID port;
|
---|
| 99 | uint32_t reg;
|
---|
| 100 | intptr_t exinf;
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | /*
|
---|
| 104 | * ã·ãªã¢ã«I/Oãã¼ã管çãããã¯ã¨ãªã¢
|
---|
| 105 | */
|
---|
| 106 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
| 107 |
|
---|
| 108 | static const uint32_t sioreg_table[TNUM_PORT] = {
|
---|
| 109 | USART1_BASE,
|
---|
| 110 | #if (TNUM_PORT >= 2)
|
---|
| 111 | USART2_BASE
|
---|
| 112 | #endif
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | Inline bool_t
|
---|
| 116 | sio_putready(SIOPCB* siopcb)
|
---|
| 117 | {
|
---|
| 118 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_TXE) != 0;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | Inline bool_t
|
---|
| 122 | sio_getready(SIOPCB* siopcb)
|
---|
| 123 | {
|
---|
| 124 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & SR_RXNE) != 0;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * ã¿ã¼ã²ããã®ã·ãªã¢ã«åæå
|
---|
| 129 | */
|
---|
| 130 | void
|
---|
| 131 | usart_init(ID siopid)
|
---|
| 132 | {
|
---|
| 133 | uint32_t tmp, usartdiv, fraction;
|
---|
| 134 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 135 | uint32_t src_clock;
|
---|
| 136 |
|
---|
| 137 | /* USARTã®ç¡å¹å */
|
---|
| 138 | sil_andw((void*)USART_CR1(reg), ~CR1_UE);
|
---|
| 139 |
|
---|
| 140 | /* 1STOP BIT */
|
---|
| 141 | sil_wrw_mem((void*)USART_CR2(reg), 0);
|
---|
| 142 |
|
---|
| 143 | /* 1START BIT, 8DATA bits, Parityãªã */
|
---|
| 144 | sil_wrw_mem((void*)USART_CR1(reg), 0);
|
---|
| 145 |
|
---|
| 146 | /* CR3åæå */
|
---|
| 147 | sil_wrw_mem((void*)USART_CR3(reg), 0);
|
---|
| 148 |
|
---|
| 149 | /* éä¿¡é度è¨å® */
|
---|
| 150 | if (siopid == 1) {
|
---|
| 151 | /* USART1ã®ã¿PCLK2ã使ç¨ãã */
|
---|
| 152 | src_clock = PCLK2_CLOCK;
|
---|
| 153 | } else {
|
---|
| 154 | src_clock = PCLK1_CLOCK;
|
---|
| 155 | }
|
---|
| 156 | tmp = (1000 * (src_clock / 100)) / ((BPS_SETTING / 100) * 16);
|
---|
| 157 | usartdiv = (tmp / 1000) << 4;
|
---|
| 158 | fraction = tmp - ((usartdiv >> 4) * 1000);
|
---|
| 159 | fraction = ((16 * fraction) + 500) / 1000;
|
---|
| 160 | usartdiv |= (fraction & 0x0F);
|
---|
| 161 | sil_wrw_mem((void*)USART_BRR(reg), usartdiv);
|
---|
| 162 |
|
---|
| 163 | /* éåä¿¡ã®æå¹åãã¨ã©ã¼å²è¾¼ã¿ã®æå¹å */
|
---|
| 164 | sil_orw((void*)USART_CR1(reg), CR1_RE | CR1_TE);
|
---|
| 165 | sil_orw((void*)USART_CR3(reg), CR3_EIE);
|
---|
| 166 |
|
---|
| 167 | /* USARTã®æå¹å */
|
---|
| 168 | sil_orw((void*)USART_CR1(reg), CR1_UE);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /*
|
---|
| 172 | * ã¿ã¼ã²ããã®ã·ãªã¢ã«çµäº
|
---|
| 173 | */
|
---|
| 174 | static void
|
---|
| 175 | usart_term(ID siopid)
|
---|
| 176 | {
|
---|
| 177 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 178 |
|
---|
| 179 | /* USARTã®ç¡å¹å */
|
---|
| 180 | sil_andw((void*)USART_CR1(reg), ~CR1_UE);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /*
|
---|
| 184 | * SIOåæå
|
---|
| 185 | */
|
---|
| 186 | void
|
---|
| 187 | sio_initialize(intptr_t exinf)
|
---|
| 188 | {
|
---|
| 189 | int i;
|
---|
| 190 |
|
---|
| 191 | for (i = 0; i < TNUM_PORT; i++) {
|
---|
| 192 | siopcb_table[i].port = i;
|
---|
| 193 | siopcb_table[i].reg = sioreg_table[i];
|
---|
| 194 | siopcb_table[i].exinf = 0;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /*
|
---|
| 199 | * ã·ãªã¢ã«ãªã¼ãã³
|
---|
| 200 | */
|
---|
| 201 | SIOPCB
|
---|
| 202 | *sio_opn_por(ID siopid, intptr_t exinf)
|
---|
| 203 | {
|
---|
| 204 | SIOPCB* siopcb;
|
---|
| 205 |
|
---|
| 206 | if (siopid > TNUM_PORT) {
|
---|
| 207 | return NULL;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | siopcb = GET_SIOPCB(siopid);
|
---|
| 211 | siopcb->exinf = exinf;
|
---|
| 212 |
|
---|
| 213 | usart_init(siopid);
|
---|
| 214 |
|
---|
| 215 | return siopcb;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /*
|
---|
| 219 | * ã·ãªã¢ã«ã¯ãã¼ãº
|
---|
| 220 | */
|
---|
| 221 | void
|
---|
| 222 | sio_cls_por(SIOPCB *p_siopcb)
|
---|
| 223 | {
|
---|
| 224 | usart_term(PORT2SIOPID(p_siopcb->port));
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /*
|
---|
| 228 | * å²è¾¼ã¿ãµã¼ãã¹ã«ã¼ãã³
|
---|
| 229 | */
|
---|
| 230 | void
|
---|
| 231 | sio_isr(intptr_t exinf)
|
---|
| 232 | {
|
---|
| 233 | SIOPCB* siopcb = GET_SIOPCB(exinf);
|
---|
| 234 |
|
---|
| 235 | if (sio_putready(siopcb)) {
|
---|
| 236 | sio_irdy_snd(siopcb->exinf);
|
---|
| 237 | }
|
---|
| 238 | if (sio_getready(siopcb)) {
|
---|
| 239 | sio_irdy_rcv(siopcb->exinf);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /*
|
---|
| 244 | * 1æåéä¿¡
|
---|
| 245 | */
|
---|
| 246 | bool_t
|
---|
| 247 | sio_snd_chr(SIOPCB *siopcb, char c)
|
---|
| 248 | {
|
---|
| 249 | if (sio_putready(siopcb)) {
|
---|
| 250 | sil_wrw_mem((void*)USART_DR(siopcb->reg), c);
|
---|
| 251 |
|
---|
| 252 | return true;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | return false;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /*
|
---|
| 259 | * 1æååä¿¡
|
---|
| 260 | */
|
---|
| 261 | int_t
|
---|
| 262 | sio_rcv_chr(SIOPCB *siopcb)
|
---|
| 263 | {
|
---|
| 264 | int_t c = -1;
|
---|
| 265 |
|
---|
| 266 | if (sio_getready(siopcb)) {
|
---|
| 267 | c = sil_rew_mem((void*)USART_DR(siopcb->reg)) & 0xFF;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | return c;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /*
|
---|
| 274 | * ã³ã¼ã«ããã¯ã®è¨±å¯
|
---|
| 275 | */
|
---|
| 276 | void
|
---|
| 277 | sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 278 | {
|
---|
| 279 | switch (cbrtn) {
|
---|
| 280 | case SIO_RDY_SND:
|
---|
| 281 | sil_orw((void*)USART_CR1(siopcb->reg), CR1_TXEIE);
|
---|
| 282 | break;
|
---|
| 283 | case SIO_RDY_RCV:
|
---|
| 284 | sil_orw((void*)USART_CR1(siopcb->reg), CR1_RXNEIE);
|
---|
| 285 | break;
|
---|
| 286 | default:
|
---|
| 287 | break;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /*
|
---|
| 292 | * ã³ã¼ã«ããã¯ã®ç¦æ¢
|
---|
| 293 | */
|
---|
| 294 | void
|
---|
| 295 | sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 296 | {
|
---|
| 297 | switch (cbrtn) {
|
---|
| 298 | case SIO_RDY_SND:
|
---|
| 299 | sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_TXEIE);
|
---|
| 300 | break;
|
---|
| 301 | case SIO_RDY_RCV:
|
---|
| 302 | sil_andw((void*)USART_CR1(siopcb->reg), ~CR1_RXNEIE);
|
---|
| 303 | break;
|
---|
| 304 | default:
|
---|
| 305 | break;
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | * 1æååºåï¼ãã¼ãªã³ã°ã§ã®åºåï¼
|
---|
| 311 | */
|
---|
| 312 | void
|
---|
| 313 | sio_pol_snd_chr(char c, ID siopid)
|
---|
| 314 | {
|
---|
| 315 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 316 |
|
---|
| 317 | sil_wrw_mem((void*)USART_DR(reg), c);
|
---|
| 318 |
|
---|
| 319 | while ((sil_rew_mem((void*)USART_SR(reg)) & SR_TXE) == 0) ;
|
---|
| 320 | }
|
---|