void init_uart(void) { int uart_num = UART_NUM_0; uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .rx_flow_ctrl_thresh = 122, }; uart_param_config(uart_num, &uart_config); // No need to set the pins as the defaults are correct for UART0 // Install driver with a receive buffer but no transmit buffer // and no event queue. uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0); } // Routines for the ccalls[] table in textend.c. Add new ones // as necessary. void ms(int msecs) { vTaskDelay(msecs/ portTICK_PERIOD_MS); }
void doGPS() { ESP_LOGD(tag, ">> doGPS"); uart_config_t myUartConfig; myUartConfig.baud_rate = 9600; myUartConfig.data_bits = UART_DATA_8_BITS; myUartConfig.parity = UART_PARITY_DISABLE; myUartConfig.stop_bits = UART_STOP_BITS_1; myUartConfig.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; myUartConfig.rx_flow_ctrl_thresh = 120; uart_param_config(UART_NUM_1, &myUartConfig); uart_set_pin(UART_NUM_1, UART_PIN_NO_CHANGE, // TX GPS_TX_PIN, // RX UART_PIN_NO_CHANGE, // RTS UART_PIN_NO_CHANGE // CTS ); uart_driver_install(UART_NUM_1, 2048, 2048, 10, 17, NULL); while(1) { char *line = readLine(UART_NUM_1); //ESP_LOGD(tag, "%s", line); switch(minmea_sentence_id(line, false)) { case MINMEA_SENTENCE_RMC: ESP_LOGD(tag, "Sentence - MINMEA_SENTENCE_RMC"); struct minmea_sentence_rmc frame; if (minmea_parse_rmc(&frame, line)) { ESP_LOGD(tag, "$xxRMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d", frame.latitude.value, frame.latitude.scale, frame.longitude.value, frame.longitude.scale, frame.speed.value, frame.speed.scale); ESP_LOGD(tag, "$xxRMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d", minmea_rescale(&frame.latitude, 1000), minmea_rescale(&frame.longitude, 1000), minmea_rescale(&frame.speed, 1000)); ESP_LOGD(tag, "$xxRMC floating point degree coordinates and speed: (%f,%f) %f", minmea_tocoord(&frame.latitude), minmea_tocoord(&frame.longitude), minmea_tofloat(&frame.speed)); } else { ESP_LOGD(tag, "$xxRMC sentence is not parsed\n"); } break; case MINMEA_SENTENCE_GGA: //ESP_LOGD(tag, "Sentence - MINMEA_SENTENCE_GGA"); break; case MINMEA_SENTENCE_GSV: //ESP_LOGD(tag, "Sentence - MINMEA_SENTENCE_GSV"); break; default: //ESP_LOGD(tag, "Sentence - other"); break; } } } // doGPS
void init() { const uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE }; uart_param_config(UART_NUM_1, &uart_config); uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // We won't use a buffer for sending data. uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0); } int sendData(const char* logName, const char* data) { const int len = strlen(data); const int txBytes = uart_write_bytes(UART_NUM_1, data, len); ESP_LOGI(logName, "Wrote %d bytes", txBytes); return txBytes; } static void tx_task() { static const char *TX_TASK_TAG = "TX_TASK"; esp_log_level_set(TX_TASK_TAG, ESP_LOG_INFO); while (1) { sendData(TX_TASK_TAG, "Hello world"); vTaskDelay(2000 / portTICK_PERIOD_MS); } } static void rx_task() { static const char *RX_TASK_TAG = "RX_TASK"; esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO); uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1); while (1) { const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS); if (rxBytes > 0) { data[rxBytes] = 0; ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data); ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO); } } free(data); } void app_main() { init(); xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, configMAX_PRIORITIES, NULL); xTaskCreate(tx_task, "uart_tx_task", 1024*2, NULL, configMAX_PRIORITIES-1, NULL); }
void initialize_console() { /* Disable buffering on stdin */ setvbuf(stdin, NULL, _IONBF, 0); /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */ esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR); /* Move the caret to the beginning of the next line on '\n' */ esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF); /* Configure UART. Note that REF_TICK is used so that the baud rate remains * correct while APB frequency is changing in light sleep mode. */ uart_config_t uart_config; memset(&uart_config, 0, sizeof(uart_config)); uart_config.baud_rate = CONFIG_CONSOLE_UART_BAUDRATE; uart_config.data_bits = UART_DATA_8_BITS; uart_config.parity = UART_PARITY_DISABLE; uart_config.stop_bits = UART_STOP_BITS_1; uart_config.use_ref_tick = true; ESP_ERROR_CHECK(uart_param_config((uart_port_t) CONFIG_CONSOLE_UART_NUM, &uart_config)); /* Install UART driver for interrupt-driven reads and writes */ ESP_ERROR_CHECK(uart_driver_install((uart_port_t) CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0)); /* Tell VFS to use UART driver */ esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM); /* Initialize the console */ esp_console_config_t console_config; memset(&console_config, 0, sizeof(console_config)); console_config.max_cmdline_args = 8; console_config.max_cmdline_length = 256; #if CONFIG_LOG_COLORS console_config.hint_color = atoi(LOG_COLOR_CYAN); #endif ESP_ERROR_CHECK(esp_console_init(&console_config)); /* Configure linenoise line completion library */ /* Enable multiline editing. If not set, long commands will scroll within * single line. */ linenoiseSetMultiLine(1); /* Tell linenoise where to get command completions and hints */ linenoiseSetCompletionCallback(&esp_console_get_completion); linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint); /* Set command history size */ linenoiseHistorySetMaxLen(100); }
static void uart_evt_task(void *pvParameters) { int uart_num = UART_NUM_0; uart_config_t uart_config = { .baud_rate = 115200, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, .rx_flow_ctrl_thresh = 122, }; //Set UART parameters uart_param_config(uart_num, &uart_config); //Set UART log level esp_log_level_set(TAG, ESP_LOG_INFO); //Install UART driver, and get the queue. uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0); //Set UART pins,(-1: default pin, no change.) //For UART0, we can just use the default pins. //uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); //Set uart pattern detect function. uart_enable_pattern_det_intr(uart_num, '+', 3, 10000, 10, 10); //Create a task to handler UART event from ISR xTaskCreate(uart_task, "uart_task", UART_TASK_STACK_SIZE, NULL, UART_TASK_PRIO, NULL); //process data uint8_t* data = (uint8_t*) malloc(BUF_SIZE); do { int len = uart_read_bytes(uart_num, data, BUF_SIZE, 100 / portTICK_RATE_MS); if(len > 0) { //ESP_LOGI(TAG, "uart read : %d", len); //uart_write_bytes(uart_num, (const char*)data, len); uart_input_handler((const char*)data, len); } } while(1); } void uart_init(void) { memset(&rcvMsg, 0, sizeof(rcvMsg)); rcvMsg.RcvBuffSize = RECV_BUF_SIZE; rcvMsg.pRcvMsgBuff = NULL; rcvMsg.pRcvMsgBuff = (uint8_t *) malloc(RECV_BUF_SIZE); if (rcvMsg.pRcvMsgBuff == NULL) { ESP_LOGE(TAG, "receive buffer alloc failed"); return; } rcvMsg.pReadPos = rcvMsg.pRcvMsgBuff; rcvMsg.pWritePos = rcvMsg.pRcvMsgBuff; xTaskCreate(uart_evt_task, "uart_evt_task", UART_EVT_TASK_STACK_SIZE, NULL, UART_EVT_TASK_PRIO, NULL); }
modem_dte_t *esp_modem_dte_init(const esp_modem_dte_config_t *config) { esp_err_t res; /* malloc memory for esp_dte object */ esp_modem_dte_t *esp_dte = calloc(1, sizeof(esp_modem_dte_t)); MODEM_CHECK(esp_dte, "calloc esp_dte failed", err_dte_mem); /* malloc memory to storing lines from modem dce */ esp_dte->buffer = calloc(1, ESP_MODEM_LINE_BUFFER_SIZE); MODEM_CHECK(esp_dte->buffer, "calloc line memory failed", err_line_mem); /* Set attributes */ esp_dte->uart_port = config->port_num; esp_dte->parent.flow_ctrl = config->flow_control; /* Bind methods */ esp_dte->parent.send_cmd = esp_modem_dte_send_cmd; esp_dte->parent.send_data = esp_modem_dte_send_data; esp_dte->parent.send_wait = esp_modem_dte_send_wait; esp_dte->parent.change_mode = esp_modem_dte_change_mode; esp_dte->parent.process_cmd_done = esp_modem_dte_process_cmd_done; esp_dte->parent.deinit = esp_modem_dte_deinit; /* Config UART */ uart_config_t uart_config = { .baud_rate = config->baud_rate, .data_bits = config->data_bits, .parity = config->parity, .stop_bits = config->stop_bits, .flow_ctrl = (config->flow_control == MODEM_FLOW_CONTROL_HW) ? UART_HW_FLOWCTRL_CTS_RTS : UART_HW_FLOWCTRL_DISABLE }; MODEM_CHECK(uart_param_config(esp_dte->uart_port, &uart_config) == ESP_OK, "config uart parameter failed", err_uart_config); if (config->flow_control == MODEM_FLOW_CONTROL_HW) { res = uart_set_pin(esp_dte->uart_port, CONFIG_MODEM_TX_PIN, CONFIG_MODEM_RX_PIN, CONFIG_MODEM_RTS_PIN, CONFIG_MODEM_CTS_PIN); } else { res = uart_set_pin(esp_dte->uart_port, CONFIG_MODEM_TX_PIN, CONFIG_MODEM_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); } MODEM_CHECK(res == ESP_OK, "config uart gpio failed", err_uart_config); /* Set flow control threshold */ if (config->flow_control == MODEM_FLOW_CONTROL_HW) { res = uart_set_hw_flow_ctrl(esp_dte->uart_port, UART_HW_FLOWCTRL_CTS_RTS, UART_FIFO_LEN - 8); } else if (config->flow_control == MODEM_FLOW_CONTROL_SW) { res = uart_set_sw_flow_ctrl(esp_dte->uart_port, true, 8, UART_FIFO_LEN - 8); } MODEM_CHECK(res == ESP_OK, "config uart flow control failed", err_uart_config); /* Install UART driver and get event queue used inside driver */ res = uart_driver_install(esp_dte->uart_port, CONFIG_UART_RX_BUFFER_SIZE, CONFIG_UART_TX_BUFFER_SIZE, CONFIG_UART_EVENT_QUEUE_SIZE, &(esp_dte->event_queue), 0); MODEM_CHECK(res == ESP_OK, "install uart driver failed", err_uart_config); /* Set pattern interrupt, used to detect the end of a line. */ res = uart_enable_pattern_det_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); /* Set pattern queue size */ res |= uart_pattern_queue_reset(esp_dte->uart_port, CONFIG_UART_PATTERN_QUEUE_SIZE); MODEM_CHECK(res == ESP_OK, "config uart pattern failed", err_uart_pattern); /* Create Event loop */ esp_event_loop_args_t loop_args = { .queue_size = ESP_MODEM_EVENT_QUEUE_SIZE, .task_name = NULL }; MODEM_CHECK(esp_event_loop_create(&loop_args, &esp_dte->event_loop_hdl) == ESP_OK, "create event loop failed", err_eloop); /* Create semaphore */ esp_dte->process_sem = xSemaphoreCreateBinary(); MODEM_CHECK(esp_dte->process_sem, "create process semaphore failed", err_sem); /* Create UART Event task */ BaseType_t ret = xTaskCreate(uart_event_task_entry, //Task Entry "uart_event", //Task Name CONFIG_UART_EVENT_TASK_STACK_SIZE, //Task Stack Size(Bytes) esp_dte, //Task Parameter CONFIG_UART_EVENT_TASK_PRIORITY, //Task Priority & (esp_dte->uart_event_task_hdl) //Task Handler ); MODEM_CHECK(ret == pdTRUE, "create uart event task failed", err_tsk_create); return &(esp_dte->parent); /* Error handling */ err_tsk_create: vSemaphoreDelete(esp_dte->process_sem); err_sem: esp_event_loop_delete(esp_dte->event_loop_hdl); err_eloop: uart_disable_pattern_det_intr(esp_dte->uart_port); err_uart_pattern: uart_driver_delete(esp_dte->uart_port); err_uart_config: free(esp_dte->buffer); err_line_mem: free(esp_dte); err_dte_mem: return NULL; } esp_err_t esp_modem_add_event_handler(modem_dte_t *dte, esp_event_handler_t handler, void *handler_args) { esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); return esp_event_handler_register_with(esp_dte->event_loop_hdl, ESP_MODEM_EVENT, ESP_EVENT_ANY_ID, handler, handler_args); }