RTno is communicating library and framework which allows you to make your embedded device capable of communicating with RT-middleware world. RT-middleware is a platform software to realize Robotic system. In RTM, robots are developed by constructing robotics technologies\' elements (components) named RT-component. Therefore, the RTno helps you to create your own RT-component with your mbed and arduino. To know how to use your RTno device, visit here: http://ysuga.net/robot_e/rtm_e/rtc_e/1065?lang=en To know about RT-middleware and RT-component, visit http://www.openrtm.org
Dependencies: EthernetInterface mbed-rtos
UART.cpp@0:5f7bc45bc2e8, 2013-06-24 (annotated)
- Committer:
- ysuga
- Date:
- Mon Jun 24 06:42:11 2013 +0000
- Revision:
- 0:5f7bc45bc2e8
- Child:
- 1:f74116b37bc9
[mbed] converted /RTnoV3/RTnoV3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ysuga | 0:5f7bc45bc2e8 | 1 | #define RTNO_SUBMODULE_DEFINE |
ysuga | 0:5f7bc45bc2e8 | 2 | |
ysuga | 0:5f7bc45bc2e8 | 3 | #include "mbed.h" |
ysuga | 0:5f7bc45bc2e8 | 4 | #include "UART.h" |
ysuga | 0:5f7bc45bc2e8 | 5 | |
ysuga | 0:5f7bc45bc2e8 | 6 | #define UART_RX_BUFFER_SIZE 128 |
ysuga | 0:5f7bc45bc2e8 | 7 | |
ysuga | 0:5f7bc45bc2e8 | 8 | static Serial *m_pSerial; |
ysuga | 0:5f7bc45bc2e8 | 9 | unsigned char uart_rx_buffer[UART_RX_BUFFER_SIZE]; |
ysuga | 0:5f7bc45bc2e8 | 10 | int uart_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 11 | int uart_rx_buffer_pointer_tail = 0; |
ysuga | 0:5f7bc45bc2e8 | 12 | |
ysuga | 0:5f7bc45bc2e8 | 13 | |
ysuga | 0:5f7bc45bc2e8 | 14 | /** |
ysuga | 0:5f7bc45bc2e8 | 15 | * Push data to ring buffer. |
ysuga | 0:5f7bc45bc2e8 | 16 | */ |
ysuga | 0:5f7bc45bc2e8 | 17 | int uart_rx_buffer_push(unsigned char c) { |
ysuga | 0:5f7bc45bc2e8 | 18 | uart_rx_buffer[uart_rx_buffer_pointer_tail] = c; |
ysuga | 0:5f7bc45bc2e8 | 19 | uart_rx_buffer_pointer_tail++; |
ysuga | 0:5f7bc45bc2e8 | 20 | if(uart_rx_buffer_pointer_tail >= UART_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 21 | uart_rx_buffer_pointer_tail = 0; |
ysuga | 0:5f7bc45bc2e8 | 22 | } |
ysuga | 0:5f7bc45bc2e8 | 23 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 24 | } |
ysuga | 0:5f7bc45bc2e8 | 25 | |
ysuga | 0:5f7bc45bc2e8 | 26 | /** |
ysuga | 0:5f7bc45bc2e8 | 27 | * Pop data fron ring buffer |
ysuga | 0:5f7bc45bc2e8 | 28 | */ |
ysuga | 0:5f7bc45bc2e8 | 29 | int uart_rx_buffer_pop(unsigned char *c) { |
ysuga | 0:5f7bc45bc2e8 | 30 | *c = uart_rx_buffer[uart_rx_buffer_pointer_head]; |
ysuga | 0:5f7bc45bc2e8 | 31 | uart_rx_buffer_pointer_head++; |
ysuga | 0:5f7bc45bc2e8 | 32 | if(uart_rx_buffer_pointer_head >= UART_RX_BUFFER_SIZE) { |
ysuga | 0:5f7bc45bc2e8 | 33 | uart_rx_buffer_pointer_head = 0; |
ysuga | 0:5f7bc45bc2e8 | 34 | } |
ysuga | 0:5f7bc45bc2e8 | 35 | return 0; |
ysuga | 0:5f7bc45bc2e8 | 36 | } |
ysuga | 0:5f7bc45bc2e8 | 37 | |
ysuga | 0:5f7bc45bc2e8 | 38 | int uart_rx_buffer_get_size() { |
ysuga | 0:5f7bc45bc2e8 | 39 | int size = uart_rx_buffer_pointer_tail - uart_rx_buffer_pointer_head; |
ysuga | 0:5f7bc45bc2e8 | 40 | if(size < 0) { |
ysuga | 0:5f7bc45bc2e8 | 41 | size += UART_RX_BUFFER_SIZE; |
ysuga | 0:5f7bc45bc2e8 | 42 | } |
ysuga | 0:5f7bc45bc2e8 | 43 | return size; |
ysuga | 0:5f7bc45bc2e8 | 44 | } |
ysuga | 0:5f7bc45bc2e8 | 45 | |
ysuga | 0:5f7bc45bc2e8 | 46 | void rx_isr(void) { |
ysuga | 0:5f7bc45bc2e8 | 47 | uart_rx_buffer_push(m_pSerial->getc()); |
ysuga | 0:5f7bc45bc2e8 | 48 | } |
ysuga | 0:5f7bc45bc2e8 | 49 | |
ysuga | 0:5f7bc45bc2e8 | 50 | |
ysuga | 0:5f7bc45bc2e8 | 51 | |
ysuga | 0:5f7bc45bc2e8 | 52 | void UART_init(unsigned char num, unsigned long baudrate) |
ysuga | 0:5f7bc45bc2e8 | 53 | { |
ysuga | 0:5f7bc45bc2e8 | 54 | PinName rx, tx; |
ysuga | 0:5f7bc45bc2e8 | 55 | switch(num) { |
ysuga | 0:5f7bc45bc2e8 | 56 | case 0: |
ysuga | 0:5f7bc45bc2e8 | 57 | rx = USBRX; tx = USBTX; |
ysuga | 0:5f7bc45bc2e8 | 58 | break; |
ysuga | 0:5f7bc45bc2e8 | 59 | case 1: |
ysuga | 0:5f7bc45bc2e8 | 60 | tx = p9, rx = p10; |
ysuga | 0:5f7bc45bc2e8 | 61 | break; |
ysuga | 0:5f7bc45bc2e8 | 62 | case 2: |
ysuga | 0:5f7bc45bc2e8 | 63 | tx = p13, rx = p14; |
ysuga | 0:5f7bc45bc2e8 | 64 | break; |
ysuga | 0:5f7bc45bc2e8 | 65 | case 3: |
ysuga | 0:5f7bc45bc2e8 | 66 | tx = p28, rx = p27; |
ysuga | 0:5f7bc45bc2e8 | 67 | break; |
ysuga | 0:5f7bc45bc2e8 | 68 | } |
ysuga | 0:5f7bc45bc2e8 | 69 | |
ysuga | 0:5f7bc45bc2e8 | 70 | m_pSerial = new Serial(tx, rx); |
ysuga | 0:5f7bc45bc2e8 | 71 | m_pSerial->baud(baudrate); |
ysuga | 0:5f7bc45bc2e8 | 72 | m_pSerial->attach(rx_isr, Serial::RxIrq); |
ysuga | 0:5f7bc45bc2e8 | 73 | |
ysuga | 0:5f7bc45bc2e8 | 74 | SerialDevice_putc = UART_putc; |
ysuga | 0:5f7bc45bc2e8 | 75 | SerialDevice_getc = UART_getc; |
ysuga | 0:5f7bc45bc2e8 | 76 | SerialDevice_available = UART_available; |
ysuga | 0:5f7bc45bc2e8 | 77 | } |
ysuga | 0:5f7bc45bc2e8 | 78 | |
ysuga | 0:5f7bc45bc2e8 | 79 | void UART_putc(const char c) { |
ysuga | 0:5f7bc45bc2e8 | 80 | m_pSerial->putc(c); |
ysuga | 0:5f7bc45bc2e8 | 81 | } |
ysuga | 0:5f7bc45bc2e8 | 82 | |
ysuga | 0:5f7bc45bc2e8 | 83 | uint8_t UART_available() |
ysuga | 0:5f7bc45bc2e8 | 84 | { |
ysuga | 0:5f7bc45bc2e8 | 85 | return uart_rx_buffer_get_size(); |
ysuga | 0:5f7bc45bc2e8 | 86 | } |
ysuga | 0:5f7bc45bc2e8 | 87 | |
ysuga | 0:5f7bc45bc2e8 | 88 | |
ysuga | 0:5f7bc45bc2e8 | 89 | char UART_getc() |
ysuga | 0:5f7bc45bc2e8 | 90 | { |
ysuga | 0:5f7bc45bc2e8 | 91 | unsigned char c; |
ysuga | 0:5f7bc45bc2e8 | 92 | uart_rx_buffer_pop(&c); |
ysuga | 0:5f7bc45bc2e8 | 93 | return c; |
ysuga | 0:5f7bc45bc2e8 | 94 | } |