![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
For test
ESP8266.cpp@4:962bf18523f4, 2015-01-30 (annotated)
- Committer:
- shennongmin
- Date:
- Fri Jan 30 11:45:49 2015 +0000
- Revision:
- 4:962bf18523f4
- Parent:
- 1:44f2f0e5d685
Add uartWiFi.h/cpp files. ready to transplant.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shennongmin | 0:7868fa4f17d0 | 1 | #include "ESP8266.h" |
shennongmin | 0:7868fa4f17d0 | 2 | |
shennongmin | 1:44f2f0e5d685 | 3 | static Serial uart(p28, p27); |
shennongmin | 1:44f2f0e5d685 | 4 | static ring_buffer rx_buffer = { { 0 }, 0, 0}; |
shennongmin | 1:44f2f0e5d685 | 5 | |
shennongmin | 1:44f2f0e5d685 | 6 | static void write_char(unsigned char c) |
shennongmin | 1:44f2f0e5d685 | 7 | { |
shennongmin | 1:44f2f0e5d685 | 8 | int i = (unsigned int)(rx_buffer.head + 1) % SERIAL_BUFFER_SIZE; |
shennongmin | 1:44f2f0e5d685 | 9 | |
shennongmin | 1:44f2f0e5d685 | 10 | // if we should be storing the received character into the location |
shennongmin | 1:44f2f0e5d685 | 11 | // just before the tail (meaning that the head would advance to the |
shennongmin | 1:44f2f0e5d685 | 12 | // current location of the tail), we're about to overflow the buffer |
shennongmin | 1:44f2f0e5d685 | 13 | // and so we don't write the character or advance the head. |
shennongmin | 1:44f2f0e5d685 | 14 | if (i != rx_buffer.tail) { |
shennongmin | 1:44f2f0e5d685 | 15 | rx_buffer.buffer[rx_buffer.head] = c; |
shennongmin | 1:44f2f0e5d685 | 16 | rx_buffer.head = i; |
shennongmin | 1:44f2f0e5d685 | 17 | } |
shennongmin | 1:44f2f0e5d685 | 18 | } |
shennongmin | 1:44f2f0e5d685 | 19 | |
shennongmin | 1:44f2f0e5d685 | 20 | static void uart_irq_callback() |
shennongmin | 1:44f2f0e5d685 | 21 | { |
shennongmin | 1:44f2f0e5d685 | 22 | // Note: you need to actually read from the serial to clear the RX interrupt |
shennongmin | 1:44f2f0e5d685 | 23 | while(uart.readable()) { |
shennongmin | 1:44f2f0e5d685 | 24 | write_char(uart.getc()); |
shennongmin | 1:44f2f0e5d685 | 25 | } |
shennongmin | 1:44f2f0e5d685 | 26 | } |
shennongmin | 1:44f2f0e5d685 | 27 | |
shennongmin | 1:44f2f0e5d685 | 28 | int read_char(void) |
shennongmin | 1:44f2f0e5d685 | 29 | { |
shennongmin | 1:44f2f0e5d685 | 30 | // if the head isn't ahead of the tail, we don't have any characters |
shennongmin | 1:44f2f0e5d685 | 31 | if (rx_buffer.head == rx_buffer.tail) { |
shennongmin | 1:44f2f0e5d685 | 32 | return -1; |
shennongmin | 1:44f2f0e5d685 | 33 | } else { |
shennongmin | 1:44f2f0e5d685 | 34 | unsigned char c = rx_buffer.buffer[rx_buffer.tail]; |
shennongmin | 1:44f2f0e5d685 | 35 | rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % SERIAL_BUFFER_SIZE; |
shennongmin | 1:44f2f0e5d685 | 36 | return c; |
shennongmin | 1:44f2f0e5d685 | 37 | } |
shennongmin | 1:44f2f0e5d685 | 38 | } |
shennongmin | 1:44f2f0e5d685 | 39 | |
shennongmin | 1:44f2f0e5d685 | 40 | int available(void) |
shennongmin | 1:44f2f0e5d685 | 41 | { |
shennongmin | 1:44f2f0e5d685 | 42 | return (unsigned int)(SERIAL_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % SERIAL_BUFFER_SIZE; |
shennongmin | 1:44f2f0e5d685 | 43 | } |
shennongmin | 1:44f2f0e5d685 | 44 | |
shennongmin | 1:44f2f0e5d685 | 45 | /** |
shennongmin | 1:44f2f0e5d685 | 46 | * 使用uart.printf或者uart.puts方法时, 当发送数据大于14个字符时, 就会出现异常(串口不断的输出不可显示的字符) |
shennongmin | 1:44f2f0e5d685 | 47 | * 因此, 相关字符串的打印使用下面的函数来完成, 虽然很简陋, 但是很管用! |
shennongmin | 1:44f2f0e5d685 | 48 | */ |
shennongmin | 1:44f2f0e5d685 | 49 | void uart_send_string(const char *str) |
shennongmin | 1:44f2f0e5d685 | 50 | { |
shennongmin | 1:44f2f0e5d685 | 51 | if (!str) |
shennongmin | 1:44f2f0e5d685 | 52 | return; |
shennongmin | 1:44f2f0e5d685 | 53 | |
shennongmin | 1:44f2f0e5d685 | 54 | while(*str) |
shennongmin | 1:44f2f0e5d685 | 55 | uart.putc(*str++); |
shennongmin | 1:44f2f0e5d685 | 56 | } |
shennongmin | 1:44f2f0e5d685 | 57 | |
shennongmin | 1:44f2f0e5d685 | 58 | void ESP8266_init(void) |
shennongmin | 1:44f2f0e5d685 | 59 | { |
shennongmin | 1:44f2f0e5d685 | 60 | uart.attach(&uart_irq_callback); |
shennongmin | 1:44f2f0e5d685 | 61 | } |
shennongmin | 1:44f2f0e5d685 | 62 | |
shennongmin | 1:44f2f0e5d685 | 63 | |
shennongmin | 1:44f2f0e5d685 | 64 |