irq map
drivers/irqchip/irq-bcm2835.c
divided into 3 banks.
0-31 bank 0 => Basic Interrupt enable register. offset 0x218, bit 0-7
32-63 bank 1 => Interrupt enable register 1. offset 0x210, bit 0-31
64-95 bank 2 => Interrupt enable register 2. offset 0x214, bit 0-31
bank 1 and bank 2 are GPU interrupt.
the system timer compare interrupt 0-3 => bank 1, 0-3 => hw irq num is 32-35
compare 0, 2 used by gpu.
compare 1, 3 can be used by ARM.
UART idle状态下保持高电平,传输数据时,先下降为低电平,为start bit,在传输数据。传输结束后,在保持高电平,为stop bit。
一个bit传输的时间为1s/buard rate。例如波特率为9600,那么bit传输时间为1000000/9600=104us
一帧的传输时间为:(1+8+1)*104=1.04ms。
实现思路是,使用起始位的下降沿作为中断触发源。然后关闭该中断,使用定时器作为中断触发源,在每个传输位的中点时间触发中断,读取GPIO电平,一个帧完毕后,在开启GPIO中断。
因此用到了两个中断:RX GPIO中断,System Timer中断。前者用来表示新数据开始接收。后者用来接收每个位。
具体代码实现如下:
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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | /* * Soft UART Implementation using generic GPIO and System Timer * * Copyright (C) 2018 Yuan Jianpeng <yuanjp@hust.edu.cn> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */ #include <linux/module.h> #include <linux/gpio.h> #include <linux/interrupt.h> #include <linux/delay.h> #include <linux/kfifo.h> #include <linux/wait.h> #include <linux/cdev.h> #define DEBUG_SU 1 #define SYS_TIMER_REG_BASE 0x3f003000 #define SYS_TIMER_IRQNUM_BASE 32 #define SOFT_UART_FIFO 32 #define SOFTUART_PORT_NR 1 enum { SU_RX_STATE_SYNC = 0, SU_RX_STATE_IDLE, SU_RX_STATE_START, SU_RX_STATE_DATA0, SU_RX_STATE_PARITY = SU_RX_STATE_DATA0 + 32, SU_RX_STATE_STOP, }; /* See BCM2835-ARM-Peripherals.pdf Chapter 12 System Timer */ struct system_timer { uint32_t contrl_status; uint32_t count_low32; uint32_t count_high32; uint32_t compare[4]; }; struct soft_uart_port { dev_t dev; struct cdev cdev; spinlock_t lock; wait_queue_head_t waitqueue; int line; unsigned long flags; int systimer; /* 0,2 is used by GPU, 1 for port 0, 3 for port 1 */ int rx_gpio; int tx_gpio; unsigned int buard_rate; int start_bits; int data_bits; int parity_bits; int stop_bits; unsigned int bit_time; unsigned int half_bit_time; int rx_irq_num; int tx_irq_num; char rx_label[32]; int rx_state; int sync_bits; int rx_intr_disabled; unsigned int rx_char; unsigned int rx_ts; DECLARE_KFIFO(rxfifo,unsigned char ,SOFT_UART_FIFO); DECLARE_KFIFO(txfifo,unsigned char ,SOFT_UART_FIFO); #if DEBUG_SU int rx_isr_in_irq; int rx_isr_num; unsigned int rx_isr_ts[128]; int timer_isr_num; unsigned int timer_isr_state[128]; unsigned int timer_isr_cmp[128]; unsigned int timer_isr_ts[128]; unsigned int timer_isr_end_ts[128]; int tempdata_len; int tempdata[128]; #endif }; struct soft_uart_driver { volatile struct system_timer *sys_timer_regs; struct soft_uart_port port[SOFTUART_PORT_NR]; }; #define SOFTUART_DRIVER(p) container_of(p-p->line,struct soft_uart_driver,port[0]) #if DEBUG_SU void dump_port_rx( struct soft_uart_port *port) { int i; printk( "rx state %d\n" , port->rx_state); printk( "rx isr %d, timer isr %d\n" , port->rx_isr_num, port->timer_isr_num); for (i = port->rx_isr_num - 1; port->rx_isr_num - i <= ARRAY_SIZE(port->rx_isr_ts) && i >= 0; i--) printk( "rx isr %u\n" , port->rx_isr_ts[i%ARRAY_SIZE(port->rx_isr_ts)]); for (i = port->timer_isr_num -1; port->timer_isr_num - i <= ARRAY_SIZE(port->timer_isr_ts) && i >= 0; i--) printk( "idx %d, timer isr %u end %u cmp %u state %d\n" , i, port->timer_isr_ts[i%ARRAY_SIZE(port->timer_isr_ts)], port->timer_isr_end_ts[i%ARRAY_SIZE(port->timer_isr_end_ts)], port->timer_isr_cmp[i%ARRAY_SIZE(port->timer_isr_cmp)], port->timer_isr_state[i%ARRAY_SIZE(port->timer_isr_state)]); } void dump_port_rx_data( struct soft_uart_port *port) { char buf[128]; int buflen = 0; int i; memset (buf, 0, sizeof (buf)); port->tempdata[port->tempdata_len++] = port->rx_char; if (port->tempdata_len >= 9) { for (i = 0; i < 9; i++) buflen += snprintf(buf+buflen, 127-buflen, "%02x" , port->tempdata[i]); printk( "%s\n" , buf); port->tempdata_len = 0; } } #endif static inline void disable_rx_intr( struct soft_uart_port *port, int nosync) { if (!port->rx_intr_disabled) { port->rx_intr_disabled = 1; /* using in_irq() may walk into dead lock gpio isr may run at thread */ if (nosync) disable_irq_nosync(port->rx_irq_num); else disable_irq(port->rx_irq_num); } } static inline void enable_rx_intr( struct soft_uart_port *port) { if (port->rx_intr_disabled) { port->rx_intr_disabled = 0; enable_irq(port->rx_irq_num); } } static inline void trigger_timer( struct soft_uart_port *port, unsigned int ts) { struct soft_uart_driver *driver = SOFTUART_DRIVER(port); int systimer = port->systimer; driver->sys_timer_regs->compare[systimer] = ts; #if DEBUG_SU if (port->rx_state != SU_RX_STATE_SYNC) { unsigned int margin = ts - driver->sys_timer_regs->count_low32; port->timer_isr_cmp[port->timer_isr_num%ARRAY_SIZE(port->timer_isr_cmp)] = ts; if (margin < 16 || margin > 10000) printk( "timer err trigger %u now %u\n" , ts, driver->sys_timer_regs->count_low32); } #endif } static inline void clear_timer( struct soft_uart_port *port) { struct soft_uart_driver *driver = SOFTUART_DRIVER(port); int systimer = port->systimer; driver->sys_timer_regs->contrl_status = (1 << systimer); } static inline uint32_t timer_ts( struct soft_uart_port *port) { struct soft_uart_driver *driver = SOFTUART_DRIVER(port); return driver->sys_timer_regs->count_low32; } static inline int get_rx_value( struct soft_uart_port *port) { return !!gpio_get_value(port->rx_gpio); } static irqreturn_t st_isr( int irqnum, void *data) { unsigned long flags; struct soft_uart_port *port = ( struct soft_uart_port *)data; int value = get_rx_value(port); #if DEBUG_SU int idx = -1; #endif spin_lock_irqsave(&port->lock, flags); clear_timer(port); #if DEBUG_SU if (port->rx_state != SU_RX_STATE_SYNC) { idx = (port->timer_isr_num++)%ARRAY_SIZE(port->timer_isr_ts); port->timer_isr_ts[idx] = timer_ts(port); port->timer_isr_state[idx] = port->rx_state; } #endif if (port->rx_state == SU_RX_STATE_SYNC) { if (value) { port->sync_bits++; if (port->sync_bits > 4 * 16) { #if DEBUG_SU printk( "sync done\n" ); #endif port->rx_state = SU_RX_STATE_IDLE; enable_rx_intr(port); } else trigger_timer(port, timer_ts(port) + port->bit_time / 4); } else port->sync_bits = 0; } else if (port->rx_state == SU_RX_STATE_START) { if (value) { /* unstable interrupt triggered by gpio so we reenter IDLE state */ port->rx_state = SU_RX_STATE_IDLE; enable_rx_intr(port); } else { trigger_timer(port, port->rx_ts + port->half_bit_time + 1*port->bit_time); port->rx_state = SU_RX_STATE_DATA0; port->rx_char = 0; } } else if (port->rx_state >= SU_RX_STATE_DATA0 && port->rx_state < SU_RX_STATE_PARITY) { port->rx_char |= (value << (port->rx_state - SU_RX_STATE_DATA0)); port->rx_state++; trigger_timer(port, port->rx_ts + port->half_bit_time + (1+port->rx_state-SU_RX_STATE_DATA0)*port->bit_time); if (port->rx_state >= SU_RX_STATE_DATA0 + port->data_bits) { port->rx_state = SU_RX_STATE_STOP; } } else if (port->rx_state == SU_RX_STATE_STOP) { if (value) { kfifo_put(&port->rxfifo, (unsigned char )port->rx_char); wake_up_interruptible(&port->waitqueue); #if DEBUG_SU dump_port_rx_data(port); #endif } else { printk( "ts %u\n" , timer_ts(port) - port->rx_ts); printk( "low stop bit\n" ); } port->rx_state = SU_RX_STATE_IDLE; enable_rx_intr(port); } #if DEBUG_SU if (idx != -1) port->timer_isr_end_ts[idx] = timer_ts(port); #endif spin_unlock_irqrestore(&port->lock, flags); return IRQ_HANDLED; } static irqreturn_t softuart_rx_isr( int irqnum, void *data) { unsigned long flags; struct soft_uart_port *port = ( struct soft_uart_port *)data; spin_lock_irqsave(&port->lock, flags); if (port->rx_state == SU_RX_STATE_SYNC) { spin_unlock_irqrestore(&port->lock, flags); return IRQ_HANDLED; } #if DEBUG_SU port->rx_isr_ts[(port->rx_isr_num++)%ARRAY_SIZE(port->rx_isr_ts)] = timer_ts(port); port->rx_isr_in_irq = in_irq(); if (port->rx_state != SU_RX_STATE_IDLE) { printk( "invalid state %d\n" , port->rx_state); } #endif disable_rx_intr(port, 1); port->rx_ts = timer_ts(port); port->rx_state = SU_RX_STATE_START; trigger_timer(port, port->rx_ts + port->half_bit_time); spin_unlock_irqrestore(&port->lock, flags); return IRQ_HANDLED; } static int softuart_rx_gpio_init( struct soft_uart_port *port) { int ret; int gpionum = port->rx_gpio; int irqnum; sprintf (port->rx_label, "softuart_rx%d\n" , gpionum); ret = gpio_request(gpionum, port->rx_label); if (ret) { printk(KERN_ERR "request gpio failed: %d\n" , ret); return ret; } ret = gpio_direction_input(gpionum); if (ret) { printk(KERN_ERR "set gpio to input failed: %d\n" , ret); gpio_free(gpionum); return ret; } ret = gpio_to_irq(gpionum); if (ret < 0) { printk(KERN_ERR "gpio to irq failed: %d\n" , ret); gpio_free(gpionum); return ret; } irqnum = ret; printk( "gpio %d to irq %d\n" , gpionum, irqnum); ret = request_irq(irqnum, softuart_rx_isr, IRQF_TRIGGER_FALLING, port->rx_label, port); if (ret) { printk(KERN_ERR "request irq failed\n" ); return ret; } port->rx_irq_num = irqnum; return 0; } static void softuart_rx_gpio_free( struct soft_uart_port *port) { printk( "free state %d\n" , port->rx_state); if (!free_irq(port->rx_irq_num, port)) { printk(KERN_ERR "free irq failed\n" ); } gpio_free(port->rx_gpio); } static int softuart_startup( struct soft_uart_port *port) { int ret; port->bit_time = 1000000 / port->buard_rate; port->half_bit_time = 500000 / port->buard_rate; port->rx_state = SU_RX_STATE_SYNC; port->rx_intr_disabled = 0; smp_wmb(); ret = request_irq(SYS_TIMER_IRQNUM_BASE+port->systimer, st_isr, 0, "system timer" , port); if (ret < 0) { printk( "request sys timer irq failed\n" ); return ret; } ret = softuart_rx_gpio_init(port); if (ret) { printk(KERN_ERR "softuart_rx_init failed\n" ); return ret; } /* disable rx interrupt and trigger sys timer to synchronize to idle state */ disable_rx_intr(port, 0); trigger_timer(port, timer_ts(port)+10); return 0; } static void softuart_shutdown( struct soft_uart_port *port) { free_irq(SYS_TIMER_IRQNUM_BASE+port->systimer, port); softuart_rx_gpio_free(port); } /********************** char device ***************************/ ssize_t softuart_read( struct file *fp, char __user *data, size_t len, loff_t *off) { struct soft_uart_port *port = fp->private_data; int ret; int copied; ret = wait_event_interruptible(port->waitqueue, !kfifo_is_empty(&port->rxfifo)); if (ret == -ERESTARTSYS) { return -EINTR; } ret = kfifo_to_user(&port->rxfifo,data,len,&copied); if (ret) return ret; return copied; } ssize_t softuart_write ( struct file *fp, const char __user *data, size_t len, loff_t *off) { printk( "softuart write\n" ); return 0; } int softuart_open( struct inode *inode, struct file *fp) { int ret; struct cdev *cdev = inode->i_cdev; struct soft_uart_port *port = container_of(cdev, struct soft_uart_port,cdev); fp->private_data = port; ret = softuart_startup(port); if (ret < 0) { printk( "softuart_startup failed\n" ); return ret; } printk( "softuart open ok\n" ); return 0; } int softuart_release( struct inode *inode, struct file *fp) { struct cdev *cdev = inode->i_cdev; struct soft_uart_port *port = container_of(cdev, struct soft_uart_port,cdev); softuart_shutdown(port); printk( "softuart release\n" ); return 0; } struct file_operations softuart_fops = { .owner = THIS_MODULE, .read = softuart_read, .write = softuart_write, .open = softuart_open, .release = softuart_release, }; static struct soft_uart_driver softuart_driver = { .port = { { .line = 0, .systimer = 1, } } }; static int buard_rate = 9600; static int rx_gpio = 21; static int tx_gpio = 20; static int start_bits = 1; static int data_bits = 8; static int parity_bits = 0; static int stop_bits = 1; static int __init mod_init( void ) { int err; dev_t *dev; struct cdev *cdev; struct soft_uart_port *port; softuart_driver.sys_timer_regs = ioremap(SYS_TIMER_REG_BASE, sizeof ( struct system_timer)); port = &softuart_driver.port[0]; port->buard_rate = buard_rate; port->rx_gpio = rx_gpio; port->tx_gpio = tx_gpio; port->start_bits = start_bits; port->data_bits = data_bits; port->parity_bits = parity_bits; port->stop_bits = stop_bits; spin_lock_init(&port->lock); INIT_KFIFO(port->rxfifo); INIT_KFIFO(port->txfifo); init_waitqueue_head(&port->waitqueue); dev = &softuart_driver.port[0].dev; cdev = &softuart_driver.port[0].cdev; err = alloc_chrdev_region(dev, 0, 1, "softuart" ); if (err < 0) { printk(KERN_WARNING "alloc_chrdev_region() failed\n" ); return err; } cdev_init(cdev, &softuart_fops); cdev->owner = THIS_MODULE; err = cdev_add(cdev, *dev, 1); if (err < 0){ printk(KERN_WARNING "cdev_add() failed\n" ); unregister_chrdev_region(*dev, 1); return err; } return 0; } static void __exit mod_exit( void ) { dev_t *dev = &softuart_driver.port[0].dev; struct cdev *cdev = &softuart_driver.port[0].cdev; unregister_chrdev_region(*dev, 1); cdev_del(cdev); iounmap(softuart_driver.sys_timer_regs); } module_init(mod_init); module_exit(mod_exit); MODULE_LICENSE( "GPL" ); MODULE_AUTHOR( "Yuan Jianpeng" ); |
使用char device作为读取数据,没有使用UART层,主要是不熟悉UART层的接口。目前只实现了RX。TX相对更简单一点。
注意:中断必须在一个bit的时间内及时处理,否则数据将丢失。目前测试,主要是printk会关中断比较长的时间,导致问题。
References
http://xinu-os.org/BCM2835_Interrupt_Controller
http://xinu-os.org/BCM2835_System_Timer
Adrienne Prahler Jaffe. Implementation of a Software UART on TMS320C54x Using General-Purpose I/O Pins.