#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "driver/spi_master.h"
#include "driver/sdspi_host.h"
#if SOC_SDMMC_IO_POWER_EXTERNAL
#include "sd_pwr_ctrl_by_on_chip_ldo.h"
#endif
/* Set Communication Parameters - Setting baud rate, data bits, stop bits, etc.
Set Communication Pins - Assigning pins for connection to a device
Install Drivers - Allocating ESP32's resources for the UART driver
Run UART Communication - Sending/receiving data /
*/
// DEFINES
#define EXAMPLE_MAX_CHAR_SIZE 512
#define MOUNT_POINT "/sdcard"
#define BUFF_SIZE (1024)*2
#define SPI_MOSI 13
#define SPI_MISO 12
#define SPI_SCLK 14
#define SPI_CS 15
static const char* TAG = "sdcard";
//PROTOTIPOS
void rol_M(int **,int, int, int);
void uartPutchar(char c)//imprime caracter
{
uart_write_bytes(0, &c, sizeof(c));
}
int uartkbhit(void)// hay datos para imprimir
{
int length = 0;
uart_get_buffered_data_len(0, (size_t*)&length);
return length;
}
char uartgetchar(void)// recupera caracter
{
char c;
while(uartkbhit() == 0)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
}
uart_read_bytes(0, &c, sizeof(0), 0);
return c;
}
static void init_uart(void)
{
const uart_port_t 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,
};
// Configure UART parameters
//ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_0, &uart_config));
// Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
//ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 17,16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, 17,16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
// Setup UART buffered IO with event queue
QueueHandle_t uart_queue;
// Install UART driver using an event queue here
//ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, BUFF_SIZE, BUFF_SIZE, 10, &uart_queue, 0));
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, BUFF_SIZE, 0, 0, NULL, 0));
}
void fill_M(int ROWS, int COLS, int r)
{
printf("inicializar...\n");
int counter = 1, first = 0, endCOLS = COLS - 1, value = counter, endROWS = ROWS-1;
// Reservar memoria para la matriz
int **matriz = (int**)malloc(ROWS * sizeof(int*));
for (size_t i = 0; i < ROWS; i++)
{
matriz[i] = (int*)malloc(COLS * sizeof(int));
}
printf("darle memoria a la matriz...\n");
while (counter <= ROWS*COLS)
{
for (size_t i = first; i <= endCOLS; i++) // [0,0] - [0,3]
{
counter++;
matriz[first][i] = value++;
}
for (size_t i = first + 1; i <= endROWS; i++) // [1,3] - [3,3]
{
counter++;
matriz[i][endCOLS] = value++;
}
for (size_t i = endCOLS - 1; i > first; i--) // [3, 3] - [3,0]
{
counter++;
matriz[endROWS][i] = value++;
}
for (size_t i = endROWS; i > first; i--) // [3,0] - [1,0]
{
counter++;
matriz[i][first] = value++;
}
first++;
endROWS--;
endCOLS--;
value = 1;
}
rol_M(matriz,ROWS, COLS, r);
// Liberar memoria
for (size_t i = 0; i < ROWS; i++)
{
free(matriz[i]);
}
free(matriz);
}
void rol_M(int **MATRIX,int ROWS, int COLS, int r)
{
printf("Estado actual: \n");
for (size_t i = 0; i < ROWS; i++)
{
for (size_t j = 0; j < COLS; j++)
{
printf("%d ", MATRIX[i][j]);
}
printf("\n");
}
while(r>0)
{
for (size_t i = 0; i < ROWS; i++)
{
for (size_t j = 0; j < COLS; j++)
{
if((i >= 0 && j == 0) || (i == 0 && j >= 0) || (i == ROWS-1 && j >= 0) || (i >= 0 && j == COLS-1)) //circulo externo
{
if(MATRIX[i][j] >= ((2*ROWS)+(2*COLS))-4)
{
MATRIX[i][j] = 1;
}
else
{
MATRIX[i][j]++;
}
}
if(!((i >= 0 && j == 0) || (i == 0 && j >= 0) || (i == ROWS-1 && j >= 0) || (i >= 0 && j == COLS-1))) //circulo interno
{
if ((ROWS*COLS) % 2 == 0)
if(MATRIX[i][j] >= (ROWS*COLS)-(((2*ROWS)+(2*COLS))-4))
{
MATRIX[i][j] = 1;
}
else
{
MATRIX[i][j]++;
}
else
if(MATRIX[i][j] >= (ROWS*COLS)-(((2*ROWS)+(2*COLS))-4)-1)
{
MATRIX[i][j] = 1;
}
else
{
MATRIX[i][j]++;
}
}
}
}
r--;
}
printf("Estado Final: \n");
for (size_t i = 0; i < ROWS; i++)
{
for (size_t j = 0; j < COLS; j++)
{
printf("%d ", MATRIX[i][j]);
}
printf("\n");
}
}
void SPI_init()
{
esp_err_t ret;
gpio_set_pull_mode(SPI_MISO, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SPI_MOSI, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SPI_SCLK, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SPI_CS, GPIO_PULLUP_ONLY);
gpio_set_direction(SPI_MISO, GPIO_MODE_INPUT);
gpio_set_direction(SPI_MOSI, GPIO_MODE_OUTPUT);
gpio_set_direction(SPI_SCLK, GPIO_MODE_OUTPUT);
gpio_set_direction(SPI_CS, GPIO_MODE_OUTPUT);
//opciones de montaje del sistema de archivos
esp_vfs_fat_sdmmc_mount_config_t mount_config =
{
.format_if_mount_failed = true,
.max_files = 5,
.allocation_unit_size = (1024 * 16),
};
sdmmc_card_t *card;
const char mount_point[] = MOUNT_POINT;
ESP_LOGI(TAG, "inicializando SD_Card\n");
ESP_LOGI(TAG, "Periferico SPI\n");
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.max_freq_khz = 5000;
//configuraciones del bus para SPI
spi_bus_config_t bus_cfg =
{
.mosi_io_num = SPI_MOSI,
.miso_io_num = SPI_MISO,
.sclk_io_num = SPI_SCLK,
.quadhd_io_num = -1,
.quadwp_io_num = -1,
.max_transfer_sz = 4000,
};
//inicializacion del bus para SPI
spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
//configuraciones para la SD por SPI
sdspi_device_config_t slot_cfg = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_cfg.gpio_cs = SPI_CS;
slot_cfg.host_id = host.slot;
//montaje de sistema de archivos
ESP_LOGI(TAG, "Montando FILESYSTEM\n");
esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_cfg, &mount_config, &card);
ESP_LOGI(TAG, "FILESYSTEM montado correctamente\n");
//mostrar informacion de la SD
sdmmc_card_print_info(stdout, card);
}
void app_main(void)
{
//xTaskCreate((void *)init_uart, "Echo", 2048, NULL, 10, NULL);
init_uart();
//SPI_init();
char M, N, R[2];
uint8_t r=0;
M = uartgetchar();
uartPutchar(M);
//N = uartgetchar();
//M-='0';
//N-='0';
//fill_M(M, N, 1);
}