Code: Select all
int pins[3][4] = {
{ JOS_UART0_TXD_PIN, JOS_UART0_RXD_PIN, JOS_UART0_RTS_PIN, JOS_UART0_CTS_PIN },
{ JOS_UART1_TXD_PIN, JOS_UART1_RXD_PIN, JOS_UART1_RTS_PIN, JOS_UART1_CTS_PIN },
{ JOS_UART2_TXD_PIN, JOS_UART2_RXD_PIN, JOS_UART2_RTS_PIN, JOS_UART2_CTS_PIN },
};
uart_config_t config = {
.baud_rate = JOS_UART_BAUDRATE,
.data_bits = JOS_UART_WORD_LENGTH,
.parity = JOS_UART_PARITY,
.stop_bits = JOS_UART_STOP_BITS,
.flow_ctrl = JOS_UART_FLOWCONTROL
};
uart_port_t port = 2;
ESP_LOGI(TAG, "Initializing.");
ESP_LOGI(TAG, "UART%d: TXD:%d RXD:%d RTS:%d CTS:%d", port, pins[port][0], pins[port][1], pins[port][2], pins[port][3]);
// port, tx, rx, rts, cts
uart_set_pin(port, pins[port][0], pins[port][1], UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_param_config(port, &config);
uart_driver_install(port, _buffer_size * 2, 0, 0, NULL, 0);
JOS_UART2_TXD_PIN and JOS_UART2_RXD_PIN are header-defined, which are 16 and 34 respectively. As you can see, only those two are currently being used - we've temporarily cut the lines for the others, and we don't even use CTS and RTS because we need those pins.
Code: Select all
int get_uart(uart_port_t port, uint8_t *data, int timeout)
{
int ticks = 0;
int ret = -1;
int delay = 50 / portTICK_RATE_MS;
while (ticks <= timeout)
{
int len = uart_read_bytes(port, data, _buffer_size, delay);
//ESP_LOGD(TAG, "%d ticks: %d len", ticks, len);
if (len > 0)
{
ESP_LOGD(TAG, "%d bytes received on UART%d", len, port);
if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE)
hexdump((char*)data, len, 40);
ret = len;
return ret;
}
/*if (!_uart_buff_handled && _data_len > 0)
{
_uart_buff_handled = 1;
memcpy(data, _data, _data_len);
return _data_len;
}*/
ticks += delay;
}
return -1;
}
This all looks like one would expect...
Code: Select all
int length = get_uart(port, readbytes, 1000);
if (length == -1)
{
ESP_LOGW(TAG, "No response received!");
return -1;
}
Finally this is where the UART actually gets used. The -1 check goes through when I disable a single line of code... Which is this, in main:
Here is the code for initializing the SD card:
Code: Select all
void sd_card_enable() {
gpio_pulldown_dis(GPIO_NUM_2);
gpio_pullup_en(GPIO_NUM_2);
WAIT(250);
ESP_LOGI(TAG, "Enabling SD card...");
//sdmmc_slot_config_t conf;
sdmmc_host_t _sd_host = SDMMC_HOST_DEFAULT();
// 1-line SD mode
_sd_host.flags = SDMMC_HOST_FLAG_1BIT;
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
slot_config.width = 1;
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and formatted
// in case when mounting fails.
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = true,
.max_files = 5
};
// Use settings defined above to initialize SD card and mount FAT filesystem.
// Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
// Please check its source code and implement error recovery when developing
// production applications.
sdmmc_card_t* card;
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &_sd_host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
} else {
ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
}
return;
}
// Card has been initialized, print its properties
ESP_LOGI(TAG, "SD card enabled.");
if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE)
{
ESP_LOGV(TAG, "Printing SD card properties...");
sdmmc_card_print_info(stdout, card);
}
}