[303] | 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,2015 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 | #include "stm32f4xx_hal.h"
|
---|
| 64 |
|
---|
| 65 | /*
|
---|
| 66 | * ã¬ã¸ã¹ã¿è¨å®å¤
|
---|
| 67 | */
|
---|
| 68 | #define PORT2SIOPID(x) ((x) + 1)
|
---|
| 69 | #define INDEX_PORT(x) ((x) - 1)
|
---|
| 70 | #define GET_SIOPCB(x) (&siopcb_table[INDEX_PORT(x)])
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * USARTã¬ã¸ã¹ã¿å®ç¾©
|
---|
| 74 | */
|
---|
| 75 | #define USART_SR(x) (x)
|
---|
| 76 | #define USART_DR(x) (x + 0x04)
|
---|
| 77 | #define USART_BRR(x) (x + 0x08)
|
---|
| 78 | #define USART_CR1(x) (x + 0x0C)
|
---|
| 79 | #define USART_CR2(x) (x + 0x10)
|
---|
| 80 | #define USART_CR3(x) (x + 0x14)
|
---|
| 81 | #define USART_GTPR(x) (x + 0x18)
|
---|
| 82 |
|
---|
| 83 | /*
|
---|
| 84 | * ã·ãªã¢ã«ãã¼ãã®ç®¡çãããã¯
|
---|
| 85 | */
|
---|
| 86 | struct sio_port_control_block {
|
---|
| 87 | ID port;
|
---|
| 88 | uint32_t reg;
|
---|
| 89 | intptr_t exinf;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | /*
|
---|
| 93 | * ã·ãªã¢ã«I/Oãã¼ã管çãããã¯ã¨ãªã¢
|
---|
| 94 | */
|
---|
| 95 | SIOPCB siopcb_table[TNUM_PORT];
|
---|
| 96 |
|
---|
| 97 | static const uint32_t sioreg_table[TNUM_PORT] = {
|
---|
| 98 | USART1_BASE,
|
---|
| 99 | #if (TNUM_PORT >= 2)
|
---|
| 100 | USART2_BASE,
|
---|
| 101 | #endif
|
---|
| 102 | };
|
---|
| 103 |
|
---|
| 104 | Inline bool_t
|
---|
| 105 | sio_putready(SIOPCB* siopcb)
|
---|
| 106 | {
|
---|
| 107 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & USART_SR_TXE) != 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | Inline bool_t
|
---|
| 111 | sio_getready(SIOPCB* siopcb)
|
---|
| 112 | {
|
---|
| 113 | return (sil_rew_mem((void*)USART_SR(siopcb->reg)) & USART_SR_RXNE) != 0;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | * ã¿ã¼ã²ããã®ã·ãªã¢ã«åæå
|
---|
| 118 | */
|
---|
| 119 | void
|
---|
| 120 | usart_init(ID siopid)
|
---|
| 121 | {
|
---|
| 122 | uint32_t tmp, usartdiv, fraction;
|
---|
| 123 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 124 | uint32_t src_clock;
|
---|
| 125 |
|
---|
| 126 | /* USARTã®ç¡å¹å */
|
---|
| 127 | sil_andw((void*)USART_CR1(reg), ~USART_CR1_UE);
|
---|
| 128 |
|
---|
| 129 | /* 1STOP BIT */
|
---|
| 130 | sil_wrw_mem((void*)USART_CR2(reg), 0);
|
---|
| 131 |
|
---|
| 132 | /* 1START BIT, 8DATA bits, Parityãªã */
|
---|
| 133 | sil_wrw_mem((void*)USART_CR1(reg), 0);
|
---|
| 134 |
|
---|
| 135 | /* CR3åæå */
|
---|
| 136 | sil_wrw_mem((void*)USART_CR3(reg), 0);
|
---|
| 137 |
|
---|
| 138 | /* éä¿¡é度è¨å® */
|
---|
| 139 | if (siopid == 1) {
|
---|
| 140 | /* USART1ã®ã¿PCLK2ã使ç¨ãã */
|
---|
| 141 | src_clock = HAL_RCC_GetPCLK2Freq();
|
---|
| 142 | } else {
|
---|
| 143 | src_clock = HAL_RCC_GetPCLK1Freq();
|
---|
| 144 | }
|
---|
| 145 | tmp = (1000 * (src_clock / 100)) / ((BPS_SETTING / 100) * 16);
|
---|
| 146 | usartdiv = (tmp / 1000) << 4;
|
---|
| 147 | fraction = tmp - ((usartdiv >> 4) * 1000);
|
---|
| 148 | fraction = ((16 * fraction) + 500) / 1000;
|
---|
| 149 | usartdiv |= (fraction & 0x0F);
|
---|
| 150 | sil_wrw_mem((void*)USART_BRR(reg), usartdiv);
|
---|
| 151 |
|
---|
| 152 | /* éåä¿¡ã®æå¹åãã¨ã©ã¼å²è¾¼ã¿ã®æå¹å */
|
---|
| 153 | sil_orw((void*)USART_CR1(reg), USART_CR1_RE | USART_CR1_TE);
|
---|
| 154 | sil_orw((void*)USART_CR3(reg), USART_CR3_EIE);
|
---|
| 155 |
|
---|
| 156 | /* USARTã®æå¹å */
|
---|
| 157 | sil_orw((void*)USART_CR1(reg), USART_CR1_UE);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
| 161 | * ã¿ã¼ã²ããã®ã·ãªã¢ã«çµäº
|
---|
| 162 | */
|
---|
| 163 | static void
|
---|
| 164 | usart_term(ID siopid)
|
---|
| 165 | {
|
---|
| 166 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 167 |
|
---|
| 168 | /* USARTã®ç¡å¹å */
|
---|
| 169 | sil_andw((void*)USART_CR1(reg), ~USART_CR1_UE);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /*
|
---|
| 173 | * SIOåæå
|
---|
| 174 | */
|
---|
| 175 | void
|
---|
| 176 | sio_initialize(intptr_t exinf)
|
---|
| 177 | {
|
---|
| 178 | int i;
|
---|
| 179 |
|
---|
| 180 | for (i = 0; i < TNUM_PORT; i++) {
|
---|
| 181 | siopcb_table[i].port = i;
|
---|
| 182 | siopcb_table[i].reg = sioreg_table[i];
|
---|
| 183 | siopcb_table[i].exinf = 0;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /*
|
---|
| 188 | * ã·ãªã¢ã«ãªã¼ãã³
|
---|
| 189 | */
|
---|
| 190 | SIOPCB
|
---|
| 191 | *sio_opn_por(ID siopid, intptr_t exinf)
|
---|
| 192 | {
|
---|
| 193 | SIOPCB* siopcb;
|
---|
| 194 |
|
---|
| 195 | if (siopid > TNUM_PORT) {
|
---|
| 196 | return NULL;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | siopcb = GET_SIOPCB(siopid);
|
---|
| 200 | siopcb->exinf = exinf;
|
---|
| 201 |
|
---|
| 202 | usart_init(siopid);
|
---|
| 203 |
|
---|
| 204 | return siopcb;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /*
|
---|
| 208 | * ã·ãªã¢ã«ã¯ãã¼ãº
|
---|
| 209 | */
|
---|
| 210 | void
|
---|
| 211 | sio_cls_por(SIOPCB *p_siopcb)
|
---|
| 212 | {
|
---|
| 213 | usart_term(PORT2SIOPID(p_siopcb->port));
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /*
|
---|
| 217 | * å²è¾¼ã¿ãµã¼ãã¹ã«ã¼ãã³
|
---|
| 218 | */
|
---|
| 219 | void
|
---|
| 220 | sio_isr(intptr_t exinf)
|
---|
| 221 | {
|
---|
| 222 | SIOPCB* siopcb = GET_SIOPCB(exinf);
|
---|
| 223 |
|
---|
| 224 | if (sio_putready(siopcb)) {
|
---|
| 225 | sio_irdy_snd(siopcb->exinf);
|
---|
| 226 | }
|
---|
| 227 | if (sio_getready(siopcb)) {
|
---|
| 228 | sio_irdy_rcv(siopcb->exinf);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /*
|
---|
| 233 | * 1æåéä¿¡
|
---|
| 234 | */
|
---|
| 235 | bool_t
|
---|
| 236 | sio_snd_chr(SIOPCB *siopcb, char c)
|
---|
| 237 | {
|
---|
| 238 | if (sio_putready(siopcb)) {
|
---|
| 239 | sil_wrw_mem((void*)USART_DR(siopcb->reg), c);
|
---|
| 240 |
|
---|
| 241 | return true;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | return false;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /*
|
---|
| 248 | * 1æååä¿¡
|
---|
| 249 | */
|
---|
| 250 | int_t
|
---|
| 251 | sio_rcv_chr(SIOPCB *siopcb)
|
---|
| 252 | {
|
---|
| 253 | int_t c = -1;
|
---|
| 254 |
|
---|
| 255 | if (sio_getready(siopcb)) {
|
---|
| 256 | c = sil_rew_mem((void*)USART_DR(siopcb->reg)) & 0xFF;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return c;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | /*
|
---|
| 263 | * ã³ã¼ã«ããã¯ã®è¨±å¯
|
---|
| 264 | */
|
---|
| 265 | void
|
---|
| 266 | sio_ena_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 267 | {
|
---|
| 268 | switch (cbrtn) {
|
---|
| 269 | case SIO_RDY_SND:
|
---|
| 270 | sil_orw((void*)USART_CR1(siopcb->reg), USART_CR1_TXEIE);
|
---|
| 271 | break;
|
---|
| 272 | case SIO_RDY_RCV:
|
---|
| 273 | sil_orw((void*)USART_CR1(siopcb->reg), USART_CR1_RXNEIE);
|
---|
| 274 | break;
|
---|
| 275 | default:
|
---|
| 276 | break;
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /*
|
---|
| 281 | * ã³ã¼ã«ããã¯ã®ç¦æ¢
|
---|
| 282 | */
|
---|
| 283 | void
|
---|
| 284 | sio_dis_cbr(SIOPCB *siopcb, uint_t cbrtn)
|
---|
| 285 | {
|
---|
| 286 | switch (cbrtn) {
|
---|
| 287 | case SIO_RDY_SND:
|
---|
| 288 | sil_andw((void*)USART_CR1(siopcb->reg), ~USART_CR1_TXEIE);
|
---|
| 289 | break;
|
---|
| 290 | case SIO_RDY_RCV:
|
---|
| 291 | sil_andw((void*)USART_CR1(siopcb->reg), ~USART_CR1_RXNEIE);
|
---|
| 292 | break;
|
---|
| 293 | default:
|
---|
| 294 | break;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | /*
|
---|
| 299 | * 1æååºåï¼ãã¼ãªã³ã°ã§ã®åºåï¼
|
---|
| 300 | */
|
---|
| 301 | void
|
---|
| 302 | sio_pol_snd_chr(char c, ID siopid)
|
---|
| 303 | {
|
---|
| 304 | uint32_t reg = sioreg_table[INDEX_PORT(siopid)];
|
---|
| 305 |
|
---|
| 306 | sil_wrw_mem((void*)USART_DR(reg), c);
|
---|
| 307 |
|
---|
| 308 | while ((sil_rew_mem((void*)USART_SR(reg)) & USART_SR_TXE) == 0) ;
|
---|
| 309 | }
|
---|