Skip to content

Commit e90489f

Browse files
authored
Merge pull request #35 from JorgeMinjares/main
Lab 12 - UART and PWM
2 parents 148efc6 + b54d1af commit e90489f

7 files changed

Lines changed: 519 additions & 0 deletions

File tree

Additional_Labs/Lab_12/README.md

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
# **Lab 12 UART and LEDC (PWM)**
2+
3+
# **Objective**
4+
The objective for this lab is to understand how to use [`Espressif`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html#) UART driver. Universal Asynchronous Receiver/Transmitter is a peripherl used to receive and send data, this protocol is a serial communication type. The ESP32 has three UART channels: ***UART 0, UART 1, UART 2.*** In this configuration we will use ***UART 1*** which allows you to select custom pins for the RX and TX setup. Students must receive information from the sender using UART and control the PWM peripheral to dim and brighten an LED to show LOW, MEDIUM and HIGH.
5+
6+
| Tasks | Objective |
7+
| :--- | :--- |
8+
| UART TASK | Receive data and send through Queue |
9+
| PWM TASK | Receive Queue and change duty cycle |
10+
11+
12+
| GPIO Pin | UART |
13+
| :--- | :--- |
14+
| 5 | TX |
15+
| 4 | RX |
16+
17+
# Bonus
18+
* Undergrad Bonus
19+
* Send the current state of the LED back to sender
20+
* Grad Bonus
21+
* Add another LED and send both LEDs' states back to the sender
22+
## **ESP32 Pinout**
23+
~~~
24+
+-----------------------+
25+
| O | USB | O |
26+
| ------- |
27+
3V3 | [ ] [ ] | VIN
28+
GND | [ ] [ ] | GND
29+
Touch3 / HSPI_CS0 / ADC2_3 / GPIO15 | [ ] [ ] | GPIO13 / ADC2_4 / HSPI_ID / Touch4
30+
CS / Touch2 / HSPI_WP / ADC2_2 / GPIO2 | [ ] [ ] | GPIO12 / ADC2_5 / HSPI_Q / Touch5
31+
Touch0 / HSPI_HD / ADC2_0 / GPIO4 | [ ] [ ] | GPIO14 / ADC2_6 / HSPI_CLK / Touch6
32+
U2_RXD / GPIO16 | [ ] [ ] | GPIO27 / ADC2_7 / Touch7
33+
U2_TXD / GPIO17 | [ ] [ ] | GPIO26 / ADC2_9 / DAC2
34+
V_SPI_CS0 / GPIO5 | [ ] ___________ [ ] | GPIO25 / ADC2_8 / DAC1
35+
SCK / V_SPI_CLK / GPIO18 | [ ] | | [ ] | GPIO33 / ADC1_5 / Touch8 / XTAL32
36+
U0_CTS / MSIO / V_SPI_Q / GPIO19 | [ ] | | [ ] | GPIO32 / ADC1_4 / Touch9 / XTAL32
37+
SDA / V_SPI_HD / GPIO21 | [ ] | | [ ] | GPIO35 / ADC1_7
38+
CLK2 / U0_RXD / GPIO3 | [ ] | | [ ] | GPIO34 / ADC1_6
39+
CLK3 / U0_TXD / GPIO1 | [ ] | | [ ] | GPIO39 / ADC1_3 / SensVN
40+
SCL / U0_RTS / V_SPI_WP / GPIO22 | [ ] | | [ ] | GPIO36 / ADC1_0 / SensVP
41+
MOSI / V_SPI_WP / GPIO23 | [ ] |___________| [ ] | EN
42+
| |
43+
| | | ____ ____ | |
44+
| | | | | | | | |
45+
| |__|__| |__| |__| |
46+
| O O |
47+
+-----------------------+
48+
~~~
49+
## **Example**
50+
Here is an example of a UART receiving data and printing Hello World.
51+
52+
~~~c
53+
#include "freertos/FreeRTOS.h"
54+
#include "freertos/task.h"
55+
#include "driver/uart.h"
56+
#include "freertos/queue.h"
57+
#include <driver/ledc.h>
58+
#include "freertos/queue.h"
59+
#include <stdio.h>
60+
#include <string.h>
61+
62+
63+
#define UART_NUM UART_NUM_2
64+
#define BUF_SIZE 1024
65+
#define TASK_MEMORY 1024
66+
#define ONBOARD_LED 2
67+
68+
static void uart_init()
69+
{
70+
uart_config_t uart_config = {
71+
.baud_rate = 9600,
72+
.data_bits = UART_DATA_8_BITS,
73+
.parity = UART_PARITY_DISABLE,
74+
.stop_bits = UART_STOP_BITS_1,
75+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
76+
.source_clk = UART_SCLK_APB,
77+
};
78+
79+
/* Installing UART Driver, Setting Pins and Configuration */
80+
uart_param_config(UART_NUM, &uart_config);
81+
uart_set_pin(UART_NUM, 5, 4, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
82+
uart_driver_install(UART_NUM, BUF_SIZE, BUF_SIZE, 0, NULL, 0);
83+
84+
}
85+
86+
static void uart_task(void *arg)
87+
{
88+
89+
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
90+
91+
while(1){
92+
bzero(data,BUF_SIZE);
93+
94+
int len = uart_read_bytes(UART_NUM, data, BUF_SIZE, pdMS_TO_TICKS(100));
95+
char* send1_str = "Onboard LED ON! ";
96+
char* send2_str = "Onboard LED OFF! ";
97+
98+
for(size_t i = 0; i < len; i++)
99+
{
100+
char value = data[i];
101+
102+
switch (value)
103+
{
104+
case 'q':
105+
printf(“Hello World”);
106+
break;
107+
108+
109+
default:
110+
111+
break;
112+
113+
}
114+
115+
free(data);
116+
117+
}
118+
}
119+
}
120+
121+
void app_main(void)
122+
{
123+
124+
uart_init();
125+
126+
xTaskCreate(uart_task, "uart_task", TASK_MEMORY, NULL, 5, NULL);
127+
}
128+
129+
~~~
130+
131+
## **Lab Template**
132+
Here is the Lab Template need for the completion of the Lab.
133+
~~~c
134+
#include "freertos/FreeRTOS.h"
135+
#include "freertos/task.h"
136+
#include "driver/uart.h"
137+
#include "freertos/queue.h"
138+
#include <driver/ledc.h>
139+
#include "freertos/queue.h"
140+
#include <stdio.h>
141+
#include <string.h>
142+
143+
#define UART_NUM UART_NUM_2
144+
#define BUF_SIZE 1024
145+
#define TASK_MEMORY 1024
146+
147+
QueueHandle_t LEDQueue;
148+
149+
static void uart_init()
150+
{
151+
152+
}
153+
154+
void sledc_setup(void){
155+
156+
}
157+
158+
void LEDC_task(void *pvParameters)
159+
{
160+
161+
}
162+
163+
static void uart_task(void *arg)
164+
{
165+
166+
}
167+
168+
void app_main(void)
169+
{
170+
171+
uart_init();
172+
ledc_setup();
173+
174+
LEDQueue = xQueueCreate(5, sizeof(int));
175+
176+
xTaskCreate(uart_task, "uart_task", TASK_MEMORY, NULL, 5, NULL);
177+
xTaskCreate(LEDC_task, "LEDC_task", 2048, NULL, 5, NULL);
178+
}
179+
~~~
180+
181+
## **C Helpful Functions**
182+
For this lab there are additional functions from [`Espressif`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html#) that are important to use the UART peripheral. As previously mentioned, ESP32 has 3 UART channels. So to be able to send information through UART you need to use the uart_write_bytes() function, here is an example of how to use it from the [`Espressif`](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html#) documentation.
183+
184+
~~~c
185+
/* Write Data to UART. */
186+
char * test_str = "This is a test string.\n";
187+
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
188+
~~~
189+
190+
UART peripheral also need functions to set up communication parameters uart_param_config() this function uses a uart_config_t data structure:
191+
192+
~~~c
193+
const uart_port_t uart_num = UART_NUM_2;
194+
uart_config_t uart_config = {
195+
.baurd_rate = 115200,
196+
.data_bits = UART_DATA_8_BITS,
197+
.parity = UART_PARITY_DISABLE,
198+
.stop_bits = UART_STOP_BITS_1,
199+
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
200+
.rx_flow_ctrl_thresh = 122,
201+
};
202+
/* Configure UART Parameters */
203+
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
204+
~~~
205+
To set communication pins use uart_set_pin():
206+
~~~c
207+
/* Set UART Pins (TX: IO4, RX:IO5 RTS: IO18, CTS: IO19) */
208+
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2,4,5,18,19));
209+
~~~
210+
Finally Driver Installation with uart_driver_install() to state size of TX buffer, size of RX buffer, Event Queue handle and size and Flags to allocate interrupt:
211+
~~~c
212+
/* Setup UART buffered IO with Event Queue */
213+
const int uart_buffer_size = (1024 * 2);
214+
QueueHandle_t uart_queue;
215+
/* Install UART Driver using an Event Queue*/
216+
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, uart_buffer_size, 10, &uart_queue, 0));
217+
~~~
218+
219+
To be able to complete this lab we will use an external hardware device that will help you communicate a serial writer software to the ESP32. The HC-05 Bluetooth module is to be connected to the ESP32 and paired to the user's PC Bluetooth. When connecting the HC-05 be sure to select the correct module and enter default password: 1234. It is important to open the Device Manager of your PC to see the name of the port in which the HC-05 module is connected to. To be able to send information the serial writer software must have the same configuration as the UART in the uart_config_t. There are a lot of serial write software out there but the recommened ones are Putty and Dock-light. Flow the Links below for more information about hardware and parity setup.
220+
221+
<img width="" alt = "Bluetooth" src="../../images/Bluetooth_and_other_devices.png">
222+
223+
<img width="" alt = "Device Manager" src="../../images/Device_manager.png">
224+
225+
<img width="" alt = "PuTTy Configuration" src="../../images/Putty_Configuration.png">
226+
227+
<img width="" alt = "PuTTy Configuration 1" src="../../images/Putty_Configuration_1.png">
228+
229+
## **Additional Links**
230+
* [Espressif UART Driver API](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/uart.html#_CPPv416uart_write_bytes11uart_port_tPKv6size_t)
231+
* [Espressif LEDC Driver API](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html)
232+
* [HC-05 Bluetooth Module](https://www.gme.cz/data/attachments/dsh.772-148.1.pdf)
233+
234+
235+
## Author
236+
* Irvin Ortiz
237+
* **Bachelor of Science in Electrical Engineering with concentration in Computer Engineering**

Additional_Labs/Lab_12/main.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "freertos/FreeRTOS.h"
2+
#include "freertos/task.h"
3+
#include "driver/uart.h"
4+
#include "freertos/queue.h"
5+
#include <driver/ledc.h>
6+
#include "freertos/queue.h"
7+
#include <stdio.h>
8+
#include <string.h>
9+
10+
#define UART_NUM UART_NUM_2
11+
#define BUF_SIZE 1024
12+
#define TASK_MEMORY 1024
13+
14+
QueueHandle_t LEDQueue;
15+
16+
static void uart_init()
17+
{
18+
19+
}
20+
21+
void sledc_setup(void){
22+
23+
}
24+
25+
void LEDC_task(void *pvParameters)
26+
{
27+
28+
}
29+
30+
static void UART_task(void *arg)
31+
{
32+
33+
}
34+
35+
void app_main(void)
36+
{
37+
38+
uart_init();
39+
ledc_setup();
40+
41+
LEDQueue = xQueueCreate(5, sizeof(int));
42+
43+
xTaskCreate(UART_task, "UART_task", TASK_MEMORY, NULL, 5, NULL);
44+
xTaskCreate(LEDC_task, "LEDC_task", 2048, NULL, 5, NULL);
45+
}

0 commit comments

Comments
 (0)