usart rx working, some cleaning

This commit is contained in:
2016-03-16 18:31:25 +01:00
parent 5867713380
commit bb386fcbbc
17 changed files with 400 additions and 173 deletions
+196
View File
@@ -0,0 +1,196 @@
//Stupid bit of code that does the bare minimum to make os_printf work.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
#include <esp8266.h>
#include "uart_driver.h"
LOCAL void uart0_rx_intr_handler(void *para);
LOCAL void uart_recvTask(os_event_t *events);
#define uart_recvTaskPrio 0
#define uart_recvTaskQueueLen 10
LOCAL os_event_t uart_recvTaskQueue[uart_recvTaskQueueLen];
/** Clear the fifos */
void ICACHE_FLASH_ATTR
clear_rxtx(int uart_no)
{
SET_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
CLEAR_PERI_REG_MASK(UART_CONF0(uart_no), UART_RXFIFO_RST | UART_TXFIFO_RST);
}
/**
* @brief Configure UART 115200-8-N-1
* @param uart_no
*/
void ICACHE_FLASH_ATTR
my_uart_init(UARTn uart_no)
{
UART_SetParity(uart_no, PARITY_NONE);
UART_SetStopBits(uart_no, ONE_STOP_BIT);
UART_SetWordLength(uart_no, EIGHT_BITS);
UART_SetBaudrate(uart_no, BIT_RATE_115200);
UART_ResetFifo(uart_no);
}
void ICACHE_FLASH_ATTR
serialInit()
{
// U0TXD
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
// U0RXD
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
// U1TXD (GPIO2)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
// Configure the UART peripherals
my_uart_init(UART0); // main
my_uart_init(UART1); // debug (output only)
// Select debug port
UART_SetPrintPort(UART1);
// Configure Rx on UART0
// Start the Rx reading task
system_os_task(uart_recvTask, uart_recvTaskPrio, uart_recvTaskQueue, uart_recvTaskQueueLen);
// set handler
ETS_UART_INTR_ATTACH((void *)uart0_rx_intr_handler, &(UartDev.rcv_buff)); // the buf will be used as an arg
// fifo threshold config
uint32_t conf = ((100 & UART_RXFIFO_FULL_THRHD) << UART_RXFIFO_FULL_THRHD_S);
conf |= ((0x10 & UART_TXFIFO_EMPTY_THRHD) << UART_TXFIFO_EMPTY_THRHD_S);
// timeout config
conf |= ((0x02 & UART_RX_TOUT_THRHD) << UART_RX_TOUT_THRHD_S); // timeout threshold
conf |= UART_RX_TOUT_EN; // enable timeout
WRITE_PERI_REG(UART_CONF1(UART0), conf);
// enable TOUT and ERR irqs
SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_TOUT_INT_ENA | UART_FRM_ERR_INT_ENA);
/* clear interrupt flags */
WRITE_PERI_REG(UART_INT_CLR(UART0), 0xffff);
/* enable RX interrupts */
SET_PERI_REG_MASK(UART_INT_ENA(UART0), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_OVF_INT_ENA);
// Enable IRQ in Extensa
ETS_UART_INTR_ENABLE();
}
// ---- receive business ----
void uart_rx_intr_disable(uint8 uart_no)
{
CLEAR_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA);
}
void uart_rx_intr_enable(uint8 uart_no)
{
SET_PERI_REG_MASK(UART_INT_ENA(uart_no), UART_RXFIFO_FULL_INT_ENA | UART_RXFIFO_TOUT_INT_ENA);
}
/**
* @brief get number of bytes in UART tx fifo
* @param UART number
*/
#define UART_GetRxFifoCount(uart_no) ((READ_PERI_REG(UART_STATUS((uart_no))) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT)
LOCAL void ICACHE_FLASH_ATTR
uart_recvTask(os_event_t *events)
{
if (events->sig == 0) {
uint8 fifo_len = UART_GetRxFifoCount(UART0);
// read from the FIFO & print back
for (uint8 idx = 0; idx < fifo_len; idx++) {
uint8 d_tmp = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
UART_WriteChar(UART0, d_tmp, 0);
}
// clear irq flags
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR | UART_RXFIFO_TOUT_INT_CLR);
// enable rx irq again
uart_rx_intr_enable(UART0);
} else if (events->sig == 1) {
// ???
}
}
/******************************************************************************
* FunctionName : uart0_rx_intr_handler
* Description : Internal used function
* UART0 interrupt handler, add self handle code inside
* Parameters : void *para - point to ETS_UART_INTR_ATTACH's arg
* Returns : NONE
*******************************************************************************/
LOCAL void
uart0_rx_intr_handler(void *para)
{
uint32_t status_reg = READ_PERI_REG(UART_INT_ST(UART0));
if (status_reg & UART_FRM_ERR_INT_ST) {
// Framing Error
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_FRM_ERR_INT_CLR);
}
if (status_reg & UART_RXFIFO_FULL_INT_ST) {
// RX fifo full
uart_rx_intr_disable(UART0);
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_FULL_INT_CLR);
// run handler
system_os_post(uart_recvTaskPrio, 0, 0); /* -> notify the polling thread */
}
if (status_reg & UART_RXFIFO_TOUT_INT_ST) {
// Fifo timeout
uart_rx_intr_disable(UART0);
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_TOUT_INT_CLR);
// run handler
system_os_post(uart_recvTaskPrio, 0, 0); /* -> notify the polling thread */
}
if (status_reg & UART_TXFIFO_EMPTY_INT_ST) {
CLEAR_PERI_REG_MASK(UART_INT_ENA(UART0), UART_TXFIFO_EMPTY_INT_ENA);
}
if (status_reg & UART_RXFIFO_OVF_INT_ST) {
WRITE_PERI_REG(UART_INT_CLR(UART0), UART_RXFIFO_OVF_INT_CLR);
}
}
+1 -1
View File
@@ -3,6 +3,6 @@
#include <esp8266.h>
void stdoutInit();
void serialInit();
#endif
+1 -1
View File
@@ -38,7 +38,7 @@ static void ICACHE_FLASH_ATTR resetBtnTimerCb(void *arg) {
} else {
if (resetCnt>=6) { //3 sec pressed
wifi_station_disconnect();
wifi_set_opmode(0x3); //reset to AP+STA mode
wifi_set_opmode(STATIONAP_MODE); //reset to AP+STA mode
os_printf("Reset to AP mode. Restarting system...\n");
system_restart();
}
-69
View File
@@ -1,69 +0,0 @@
//Stupid bit of code that does the bare minimum to make os_printf work.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
#include <esp8266.h>
#include "uart.h"
///** USART1 write routine */
//static void ICACHE_FLASH_ATTR stdoutUart1Txd(char c)
//{
// //Wait until there is room in the FIFO
// while (((READ_PERI_REG(UART_STATUS(0)) >> UART_TXFIFO_CNT_S) & UART_TXFIFO_CNT) >= 126) ;
// //Send the character
// WRITE_PERI_REG(UART_FIFO(0), c);
//}
///** Putchar to USART1 */
//static void ICACHE_FLASH_ATTR stdoutPutcharUSART1(char c)
//{
// //convert \n -> \r\n
// if (c == '\n') stdoutUart1Txd('\r');
// stdoutUart1Txd(c);
//}
/**
* @brief Configure UART 115200-8-N-1
* @param uart_no
*/
void ICACHE_FLASH_ATTR
my_uart_init(UARTn uart_no)
{
UART_SetParity(uart_no, PARITY_NONE);
UART_SetStopBits(uart_no, ONE_STOP_BIT);
UART_SetWordLength(uart_no, EIGHT_BITS);
UART_SetBaudrate(uart_no, BIT_RATE_115200);
UART_ResetFifo(uart_no);
}
void ICACHE_FLASH_ATTR
stdoutInit()
{
// U0TXD
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
// U0RXD
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD);
// U1TXD (GPIO2)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
// Configure the UART peripherals
my_uart_init(UART0); // main
my_uart_init(UART1); // debug (output only)
// Select debug port
UART_SetPrintPort(UART1);
}
+1 -1
View File
@@ -2,7 +2,7 @@
* Driver file for ESP8266 UART, works with the SDK.
*/
#include "uart.h"
#include "uart_driver.h"
#include <esp8266.h>
View File
+15 -3
View File
@@ -21,8 +21,9 @@
// user files
#include "cgi.h"
#include "stdout.h"
#include "cfg_serial.h"
#include "io.h"
#include "uart_driver.h"
/**
@@ -113,6 +114,13 @@ static HttpdBuiltInUrl builtInUrls[] = {
};
static ETSTimer prTestTimer;
static void ICACHE_FLASH_ATTR test_timer_task(void *arg) {
const char * t = "Test\r\n";
UART_WriteBuffer(0, (uint8_t*)t, strlen(t), 1000);
}
/**
* Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
@@ -120,7 +128,7 @@ static HttpdBuiltInUrl builtInUrls[] = {
void user_init(void)
{
// set up the debuging output
stdoutInit();
serialInit();
// reset button etc
ioInit();
@@ -142,8 +150,12 @@ void user_init(void)
httpdInit(builtInUrls, 80);
os_printf("\nReady\n");
// print TEST on the command interface every 500 ms
os_timer_disarm(&prTestTimer);
os_timer_setfn(&prTestTimer, test_timer_task, NULL);
os_timer_arm(&prTestTimer, 500, 1);
}