MeterLogger
uart.c
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2013-2014 Espressif Systems (Wuxi)
3  *
4  * FileName: uart.c
5  *
6  * Description: Two UART mode configration and interrupt handler.
7  * Check your hardware connection while use this mode.
8  *
9  * Modification history:
10  * 2014/3/12, v1.0 create this file.
11 *******************************************************************************/
12 #include <esp8266.h>
13 #include "driver/uart.h"
14 #include "driver/uart_register.h"
15 #include "user_interface.h"
16 #ifndef EN61107
17  #include "kmp_request.h"
18 #else
19  #include "en61107_request.h"
20 #endif
21 //#include "ssc.h"
22 
23 
24 // UartDev is defined and initialized in rom code.
25 extern UartDevice UartDev;
26 //extern os_event_t at_recvTaskQueue[at_recvTaskQueueLen];
27 
28 LOCAL void uart0_rx_intr_handler(void *para);
29 
30 /******************************************************************************
31  * FunctionName : uart_config
32  * Description : Internal used function
33  * UART0 used for data TX/RX, RX buffer size is 0x100, interrupt enabled
34  * UART1 just used for debug output
35  * Parameters : uart_no, use UART0 or UART1 defined ahead
36  * Returns : NONE
37 *******************************************************************************/
40 {
41  if (uart_no == UART1)
42  {
44  }
45  else
46  {
47  /* rcv_buff size if 0x100 */
52  }
53 
54  uart_div_modify(uart_no, UART_CLK_FREQ / (UartDev.baut_rate));
55 
57  WRITE_PERI_REG(UART_CONF0(uart_no),
59  ((UartDev.parity & UART_PARITY_M) << UART_PARITY_S ) |
61  ((UartDev.data_bits & UART_BIT_NUM) << UART_BIT_NUM_S));
62 
63  //clear rx and tx fifo,not ready
66 
67  //set rx fifo trigger
68 // WRITE_PERI_REG(UART_CONF1(uart_no),
69 // ((UartDev.rcv_buff.TrigLvl & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S) |
70 // ((96 & UART_TXFIFO_EMPTY_THRHD) << UART_TXFIFO_EMPTY_THRHD_S) |
71 // UART_RX_FLOW_EN);
72  if (uart_no == UART0)
73  {
74  //set rx fifo trigger
75  WRITE_PERI_REG(UART_CONF1(uart_no),
83  }
84  else
85  {
86  WRITE_PERI_REG(UART_CONF1(uart_no),
88  }
89 
90  //clear all interrupt
91  WRITE_PERI_REG(UART_INT_CLR(uart_no), 0xffff);
92  //enable rx_interrupt
94 }
95 
96 /******************************************************************************
97  * FunctionName : uart1_tx_one_char
98  * Description : Internal used function
99  * Use uart1 interface to transfer one char
100  * Parameters : uint8 TxChar - character to tx
101  * Returns : OK
102 *******************************************************************************/
103 STATUS
105 {
106  while (true)
107  {
109  if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) < 126) {
110  break;
111  }
112  }
113 
114  WRITE_PERI_REG(UART_FIFO(uart) , TxChar);
115  return OK;
116 }
117 
118 /******************************************************************************
119  * FunctionName : uart1_write_char
120  * Description : Internal used function
121  * Do some special deal while tx char is '\r' or '\n'
122  * Parameters : char c - character to tx
123  * Returns : NONE
124 *******************************************************************************/
127 {
128  if (c == '\n')
129  {
130  uart_tx_one_char(UART1, '\r');
131  uart_tx_one_char(UART1, '\n');
132  }
133  else if (c == '\r')
134  {
135  }
136  else
137  {
139  }
140 }
141 
144 {
145  if (c == '\n')
146  {
147  uart_tx_one_char(UART0, '\r');
148  uart_tx_one_char(UART0, '\n');
149  }
150  else
151  {
153  }
154 }
155 /******************************************************************************
156  * FunctionName : uart0_tx_buffer
157  * Description : use uart0 to transfer buffer
158  * Parameters : uint8 *buf - point to send buffer
159  * uint16 len - buffer len
160  * Returns :
161 *******************************************************************************/
164 {
165  uint16 i;
166 
167  for (i = 0; i < len; i++)
168  {
169  uart_tx_one_char(UART0, buf[i]);
170  }
171 }
172 
173 /******************************************************************************
174  * FunctionName : uart0_sendStr
175  * Description : use uart0 to transfer buffer
176  * Parameters : uint8 *buf - point to send buffer
177  * uint16 len - buffer len
178  * Returns :
179 *******************************************************************************/
181 uart0_sendStr(const char *str)
182 {
183  while(*str)
184  {
185  uart_tx_one_char(UART0, *str++);
186  }
187 }
188 
189 /******************************************************************************
190  * FunctionName : uart0_rx_intr_handler
191  * Description : Internal used function
192  * UART0 interrupt handler, add self handle code inside
193  * Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
194  * Returns : NONE
195 *******************************************************************************/
196 //extern void at_recvTask(void);
197 
198 LOCAL void
200 {
201  /* uart0 and uart1 intr combine togther, when interrupt occur, see reg 0x3ff20020, bit2, bit0 represents
202  * uart1 and uart0 respectively
203  */
204 // RcvMsgBuff *pRxBuff = (RcvMsgBuff *)para;
205 
206  uint8 RcvChar;
207  uint8 uart_no = UART0;//UartDev.buff_uart_no;
208 
209 
211  //os_printf("FRM_ERR\r\n");
213  }
214 
216  //ETS_UART_INTR_DISABLE();/////////
218  //WRITE_PERI_REG(0X60000914, 0x73); //WTD
219  RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
220 #ifdef EN61107
221  en61107_fifo_put(RcvChar);
222  if (en61107_is_eod_char(RcvChar)) { // if end of mc66 frame received
224  }
225 #elif defined IMPULSE
226  // nothing
227 #else
228  kmp_fifo_put(RcvChar);
229  if ((RcvChar == '\r') || (RcvChar == 0x06)) { // if end of kmp frame received or acknowledge
231  }
232 #endif
233  }
234  }
236  //ETS_UART_INTR_DISABLE();/////////
238  //WRITE_PERI_REG(0X60000914, 0x73); //WTD
239  RcvChar = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
240 #ifdef EN61107
241  en61107_fifo_put(RcvChar);
242  if (en61107_is_eod_char(RcvChar)) { // if end of mc66 frame received
244  }
245 #elif defined IMPULSE
246  // nothing
247 #else
248  kmp_fifo_put(RcvChar);
249  if ((RcvChar == '\r') || (RcvChar == 0x06)) { // if end of kmp frame received or acknowledge
251  }
252 #endif
253  }
254  }
255 
256  if (UART_RXFIFO_FULL_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_FULL_INT_ST)) {
258  }
259  else if (UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(UART0)) & UART_RXFIFO_TOUT_INT_ST)) {
261  }
262  //ETS_UART_INTR_ENABLE();
263 }
264 
265 /******************************************************************************
266  * FunctionName : uart_init
267  * Description : user interface for init uart
268  * Parameters : UartBautRate uart0_br - uart0 bautrate
269  * UartBautRate uart1_br - uart1 bautrate
270  * Returns : NONE
271 *******************************************************************************/
274 {
275  // rom use 74880 baut_rate, here reinitialize
276 #ifndef EN61107
277  UartDev.baut_rate = uart0_br;
278  UartDev.exist_parity = STICK_PARITY_DIS;
279  UartDev.data_bits = EIGHT_BITS;
280  UartDev.parity = NONE_BITS;
281  UartDev.stop_bits = TWO_STOP_BIT;
282 #else
283  UartDev.baut_rate = uart0_br;
284  UartDev.exist_parity = STICK_PARITY_EN;
285  UartDev.data_bits = SEVEN_BITS;
286  UartDev.parity = EVEN_BITS;
287  UartDev.stop_bits = TWO_STOP_BIT;
288 #endif
290 #ifndef EN61107
291  UartDev.baut_rate = uart1_br;
292  UartDev.exist_parity = STICK_PARITY_DIS;
293  UartDev.data_bits = EIGHT_BITS;
294  UartDev.parity = NONE_BITS;
295  UartDev.stop_bits = TWO_STOP_BIT;
296 #else
297  UartDev.baut_rate = uart1_br;
298  UartDev.exist_parity = STICK_PARITY_EN;
299  UartDev.data_bits = SEVEN_BITS;
300  UartDev.parity = EVEN_BITS;
301  UartDev.stop_bits = TWO_STOP_BIT;
302 #endif
305 
306  // install uart1 putc callback
308 }
309 
311 uart_set_word_length(uint8_t uart_no, UartBitsNum4Char len) {
313 }
314 
316 uart_set_stop_bits(uint8_t uart_no, UartStopBitsNum bit_num) {
318 }
319 
321 uart_set_line_inverse(uint8_t uart_no, UART_LineLevelInverse inverse_mask) {
323  SET_PERI_REG_MASK(UART_CONF0(uart_no), inverse_mask);
324 }
325 
327 uart_set_parity(uint8_t uart_no, UartParityMode Parity_mode) {
329  if (Parity_mode == NONE_BITS) {
330  }
331  else {
332  SET_PERI_REG_MASK(UART_CONF0(uart_no), Parity_mode| UART_PARITY_EN);
333  }
334 }
335 
337 uart_set_baudrate(uint8 uart_no, uint32_t baud_rate) {
338  uart_div_modify(uart_no, UART_CLK_FREQ / baud_rate);
339 }
340 
bool system_os_post(uint8 prio, os_signal_t sig, os_param_t par)
#define PIN_FUNC_SELECT(PIN_NAME, FUNC)
Definition: eagle_soc.h:270
#define UART_TXFIFO_CNT_S
Definition: uart_register.h:90
#define UART_RXFIFO_RST
Definition: uart.h:57
void ICACHE_FLASH_ATTR uart_set_parity(uint8_t uart_no, UartParityMode Parity_mode)
Definition: uart.c:327
#define UART_RX_FLOW_THRHD
#define FUNC_U0RTS
Definition: eagle_soc.h:227
void ICACHE_FLASH_ATTR uart0_tx_buffer(uint8 *buf, uint16 len)
Definition: uart.c:163
#define UART_TXFIFO_CNT
Definition: uart_register.h:89
#define UART_CONF0(i)
Definition: uart_register.h:97
void ICACHE_FLASH_ATTR uart0_sendStr(const char *str)
Definition: uart.c:181
#define UART_RX_TOUT_EN
LOCAL void uart0_rx_intr_handler(void *para)
Definition: uart.c:199
#define READ_PERI_REG(addr)
Definition: eagle_soc.h:68
#define UART_RX_TOUT_THRHD
#define UART_STOP_BIT_NUM_S
Definition: uart.h:55
#define UART_FIFO(i)
Definition: uart_register.h:28
#define ETS_UART_INTR_ENABLE()
Definition: ets_sys.h:93
#define UART_RXFIFO_CNT
Definition: uart_register.h:94
void ICACHE_FLASH_ATTR uart0_write_char(char c)
Definition: uart.c:143
UartBautRate baut_rate
Definition: uart.h:135
#define PERIPHS_IO_MUX_GPIO2_U
Definition: eagle_soc.h:258
unsigned short uint16
Definition: c_types.h:50
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
STATUS uart_tx_one_char(uint8 uart, uint8 TxChar)
Definition: uart.c:104
#define UART_RXFIFO_FULL_THRHD_S
#define UART_RXFIFO_CNT_S
Definition: uart_register.h:95
#define UART_RXFIFO_FULL_INT_ST
Definition: uart_register.h:52
UartParityMode parity
Definition: uart.h:138
#define UART_PARITY_EN
#define UART_CLK_FREQ
Definition: eagle_soc.h:78
#define WRITE_PERI_REG(addr, val)
Definition: eagle_soc.h:69
UartBitsNum4Char
Definition: uart.h:41
#define UART_CONF1(i)
#define UART_FRM_ERR_INT_CLR
Definition: uart_register.h:71
#define UART_FRM_ERR_INT_ENA
Definition: uart_register.h:60
#define CLEAR_PERI_REG_MASK(reg, mask)
Definition: eagle_soc.h:70
#define FUNC_U1TXD_BK
Definition: eagle_soc.h:260
#define UART_RXFIFO_TOUT_INT_ENA
Definition: uart_register.h:55
#define UART_RXFIFO_TOUT_INT_CLR
Definition: uart_register.h:66
#define UART1
Definition: uart.h:38
#define os_install_putc1
Definition: osapi.h:33
#define UART_FRM_ERR_INT_ST
Definition: uart_register.h:49
UartParityMode
Definition: uart.h:54
#define UART_STATUS(i)
Definition: uart_register.h:85
#define UART_RXFIFO_FULL_THRHD
void ICACHE_FLASH_ATTR uart_set_line_inverse(uint8_t uart_no, UART_LineLevelInverse inverse_mask)
Definition: uart.c:321
LOCAL void ICACHE_FLASH_ATTR uart_config(uint8 uart_no)
Definition: uart.c:39
#define UART_BIT_NUM_S
unsigned char uint8
Definition: c_types.h:45
#define FUNC_U0TXD
Definition: eagle_soc.h:231
#define UART_RXFIFO_TOUT_INT_ST
Definition: uart_register.h:44
uint8 TrigLvl
Definition: uart.h:117
#define UART_RX_FLOW_THRHD_S
#define UART_TXFIFO_RST
#define UART_PARITY_S
#define UART_STOP_BIT_NUM
#define UART_PARITY_EN_S
STATUS
Definition: c_types.h:79
UartBitsNum4Char data_bits
Definition: uart.h:136
Definition: c_types.h:80
unsigned char en61107_fifo_put(unsigned char c)
UartExistParity exist_parity
Definition: uart.h:137
UART_LineLevelInverse
Definition: uart.h:65
#define PERIPHS_IO_MUX_MTDO_U
Definition: eagle_soc.h:225
void ICACHE_FLASH_ATTR uart_set_stop_bits(uint8_t uart_no, UartStopBitsNum bit_num)
Definition: uart.c:316
#define UART_RXFIFO_FULL_INT_ENA
Definition: uart_register.h:63
unsigned int uint32
Definition: c_types.h:54
void ICACHE_FLASH_ATTR uart_set_word_length(uint8_t uart_no, UartBitsNum4Char len)
Definition: uart.c:311
#define UART_INT_ST(i)
Definition: uart_register.h:43
#define ETS_UART_INTR_ATTACH(func, arg)
Definition: ets_sys.h:81
void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br)
Definition: uart.c:273
UartStopBitsNum
Definition: uart.h:48
void ICACHE_FLASH_ATTR uart_set_baudrate(uint8 uart_no, uint32_t baud_rate)
Definition: uart.c:337
UartStopBitsNum stop_bits
Definition: uart.h:139
bool en61107_is_eod_char(uint8_t c)
#define UART0
Definition: uart.h:37
#define UART_RX_FLOW_EN
UartDevice UartDev
#define UART_RXFIFO_FULL_INT_CLR
Definition: uart_register.h:74
#define UART_PARITY_M
RcvMsgBuff rcv_buff
Definition: uart.h:141
void ICACHE_FLASH_ATTR uart1_write_char(char c)
Definition: uart.c:126
#define SET_PERI_REG_MASK(reg, mask)
Definition: eagle_soc.h:71
#define UART_RX_TOUT_THRHD_S
#define UART_LINE_INV_MASK
Definition: uart.h:33
#define PERIPHS_IO_MUX_U0TXD_U
Definition: eagle_soc.h:230
#define UART_PARITY_EN_M
#define UART_BIT_NUM
#define UART_INT_ENA(i)
Definition: uart_register.h:54
#define LOCAL
Definition: c_types.h:72
UartBautRate
Definition: uart.h:74
#define kmp_received_task_prio
Definition: kmp_request.h:3
unsigned char kmp_fifo_put(unsigned char c)
Definition: kmp_request.c:370
#define PIN_PULLUP_DIS(PIN_NAME)
Definition: eagle_soc.h:267
#define UART_INT_CLR(i)
Definition: uart_register.h:65
#define SET_PERI_REG_BITS(reg, bit_map, value, shift)
Definition: eagle_soc.h:73
#define UART_PARITY
#define en61107_received_task_prio