Skip to content

Commit 01ae8f9

Browse files
committed
shift_reg: add shift_reg component
The feature is a driver to interface the microcontroller with shift registers just using 3 pins: Clock, Data and Latch. For further information, please, take a look at the component [README.md](components/shift_reg/README.md). Feature requested by issue #457.
1 parent fca4578 commit 01ae8f9

File tree

15 files changed

+564
-0
lines changed

15 files changed

+564
-0
lines changed

.github/labeler.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ labels:
536536
- "components/sgp40/**"
537537
- "components/sgp40/.eil.yml"
538538
- "examples/sgp40/**"
539+
- label: "area:components:shift_reg"
540+
sync: true
541+
matcher:
542+
files:
543+
any:
544+
- "components/shift_reg/**"
545+
- "components/shift_reg/.eil.yml"
546+
- "examples/shift_reg/**"
539547
- label: "area:components:sht3x"
540548
sync: true
541549
matcher:

components/shift_reg/.eil.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
components:
3+
- name: shift_reg
4+
description: Driver for generic shift register to expend I/O by writing or reading from it in a serial communication.
5+
group: misc
6+
groups: []
7+
code_owners: jaimealbq
8+
depends:
9+
# FIXME conditional depends
10+
- name: driver
11+
- name: log
12+
- name: esp_idf_lib_helpers
13+
thread_safe: yes
14+
targets:
15+
- name: esp32
16+
- name: esp8266
17+
- name: esp32s2
18+
- name: esp32c3
19+
licenses:
20+
- name: ISC
21+
copyrights:
22+
- author:
23+
name: jaimealbq
24+
year: 2022
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if(${IDF_TARGET} STREQUAL esp8266)
2+
set(req esp8266 log esp_idf_lib_helpers)
3+
else()
4+
set(req driver log esp_idf_lib_helpers)
5+
endif()
6+
7+
idf_component_register(
8+
SRCS shift_reg.c
9+
INCLUDE_DIRS .
10+
REQUIRES ${req}
11+
)

components/shift_reg/LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2022 Jaime Albuquerque <[email protected]>
2+
3+
Permission to use, copy, modify, and distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

components/shift_reg/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# `Shift Register`
2+
This driver can be used as an interface with a shift register in (still in progress) and out (such as [74HC595](https://www.ti.com/lit/ds/symlink/sn74hc595.pdf)).
3+
4+
## Usage
5+
To use the library, it needs to be configured before initialize. To do so a `shift_reg_config_s` needs to be set.
6+
7+
### Configuration
8+
The `shift_reg_config_s` struct store all necessaries configurtions and value of aech register.
9+
10+
* `uint8_t num_reg` - Number of registers which will be used
11+
* `uint8_t *reg_value` - Vector for the last value of all registers; it can be used for know what is the actual value of the registers
12+
* `struct mode`
13+
* `shift_reg_dir_t dir` - Direction mode of the shift register
14+
* `shift_reg_bit_mode_t bit_mode` - Bit mode
15+
* `struct pin`
16+
* `gpio_num_t clk` - Clock pin
17+
* `gpio_num_t data` - Data/Signal pin
18+
* `gpio_num_t latch` - Latch pin
19+
20+
#### Direcition mode
21+
The `shift_reg_dir_t` says the direction of the register.
22+
* `SHIFT_DIR_OUTPUT` - Set the register as output
23+
* `SHIFT_DIR_INPUT` - Set the register as input
24+
* `SHIFT_DIR_INPUT_OUTPUT` - Set the register as output and input
25+
26+
#### First bit configuration
27+
The `shift_reg_bit_mode_t` says the bit mode of the data.
28+
* `SHIFT_BIT_MODE_LSB` - Start send data from the lowest significant bit
29+
* `SHIFT_BIT_MODE_MSB` - Start send data from the most significandt bit
30+
31+
### Initialization
32+
To initialize the shift register it is going to need to call the `esp_err_t shift_reg_init(shift_reg_config_s *shft)`, by passing the created shift register configurations.
33+
34+
### De-initialization
35+
Since the `uint8_t *reg_value` is created accordingly of the number of registers, the vector allocate the necessary size in the heap memory, so `esp_err_t shift_reg_deinit(shift_reg_config_s *shft)` can be used to free this memory.
36+
37+
### Sending the data
38+
To send the intended data, call the `esp_err_t shift_reg_send(uint8_t *data, uint8_t len, shift_reg_config_s *shft)` function, where:
39+
* `data` - the address of the bening of the data
40+
* `len` - the length of the data in bytes
41+
* `shft` - the shift register interface to be used
42+
43+
**NOTE**: The data sent will be just in the internal memory (register(s)) of the shift register(s), and not reflected in the pins. See lataching pins to see how the pins can be set.
44+
45+
### Latching pins
46+
To set the pins as it is in the internal memory of the register(s), the function `esp_err_t shift_reg_latch(shift_reg_config_s *shft);` needs to be called. It will be latched by the passed shifter interface.
47+
48+
## TODO
49+
* [ ] Implement input shift register
50+
* [ ] Interface with [hd44780](../hd44780/)
51+
52+
# Author
53+
Jaime Albuquerque
54+
* GitHub: [jaimealbq](https://github.com/jaimealbq)
55+
* GitLab: [jaimealbq](https://gitlab.com/jaimealbq)
56+
* LinkedIn: [Jaime Albuquerque](https://www.linkedin.com/in/jaimealbq)

components/shift_reg/component.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
COMPONENT_ADD_INCLUDEDIRS = .
2+
3+
ifdef CONFIG_IDF_TARGET_ESP8266
4+
COMPONENT_DEPENDS = esp8266 log esp_idf_lib_helpers
5+
else
6+
COMPONENT_DEPENDS = driver log esp_idf_lib_helpers
7+
endif

components/shift_reg/shift_reg.c

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Copyright (c) 2022 Jaime Albuquerque <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include "shift_reg.h"
18+
19+
static char *tag = "shift_reg";
20+
21+
esp_err_t shift_reg_init(shift_reg_config_s *shft)
22+
{
23+
esp_err_t err = ESP_FAIL;
24+
25+
if (shft == NULL)
26+
{
27+
ESP_LOGE(tag, "%s: must have the configuration of the shift register", __func__);
28+
err = ESP_ERR_INVALID_ARG;
29+
return err;
30+
}
31+
32+
shft->reg_value = (uint8_t *)malloc(shft->num_reg); // Create an array with all registers
33+
34+
if (shft->reg_value == NULL)
35+
{
36+
ESP_LOGE(tag, "%s: no heap memory to allocate reg_value", __func__);
37+
err = ESP_ERR_NO_MEM;
38+
return err;
39+
}
40+
41+
memset(shft->reg_value, 0, shft->num_reg); // Start all registers as 0
42+
43+
gpio_config_t *io_conf = (gpio_config_t *)malloc(sizeof(gpio_config_t));
44+
45+
// disable interrupt
46+
io_conf->intr_type = GPIO_INTR_DISABLE;
47+
48+
switch (shft->mode.dir)
49+
{
50+
case SHIFT_DIR_OUTPUT:
51+
// set as output mode
52+
io_conf->mode = GPIO_MODE_OUTPUT;
53+
uint32_t buf32_0 = 0;
54+
uint32_t buf32_1 = 0;
55+
uint64_t result = 0;
56+
57+
if (shft->pin.clk >= 32)
58+
buf32_1 |= 1 << (shft->pin.clk - 32);
59+
else
60+
buf32_0 |= 1 << shft->pin.clk;
61+
62+
if (shft->pin.latch >= 32)
63+
buf32_1 |= 1 << (shft->pin.latch - 32);
64+
else
65+
buf32_0 |= 1 << shft->pin.latch;
66+
67+
if (shft->pin.data >= 32)
68+
buf32_1 |= 1 << (shft->pin.data - 32);
69+
else
70+
buf32_0 |= 1 << shft->pin.data;
71+
72+
result = ((uint64_t)buf32_1 << 32) | ((uint64_t)buf32_0 << 0);
73+
74+
io_conf->pin_bit_mask = result;
75+
76+
break;
77+
78+
case SHIFT_DIR_INPUT:
79+
ESP_LOGE(tag, "%s: Input shift register not done yet", __func__);
80+
err = ESP_ERR_NOT_FOUND;
81+
break;
82+
83+
case SHIFT_DIR_INPUT_OUTPUT:
84+
ESP_LOGE(tag, "%s: Input output shift register not done yet", __func__);
85+
err = ESP_ERR_NOT_FOUND;
86+
break;
87+
88+
default:
89+
ESP_LOGE(tag, "%s: Mode of shift register not found", __func__);
90+
err = ESP_ERR_NOT_FOUND;
91+
break;
92+
}
93+
// disable pull-down mode
94+
io_conf->pull_down_en = 0;
95+
// disable pull-up mode
96+
io_conf->pull_up_en = 0;
97+
// configure GPIO with the given settings
98+
err = gpio_config(io_conf);
99+
100+
free(io_conf);
101+
102+
return err;
103+
}
104+
105+
esp_err_t shift_reg_deinit(shift_reg_config_s *shft)
106+
{
107+
free(shft->reg_value);
108+
return ESP_OK;
109+
}
110+
111+
esp_err_t shift_reg_send(uint8_t *data, uint8_t len, shift_reg_config_s *shft)
112+
{
113+
esp_err_t err = ESP_FAIL;
114+
115+
if (shft == NULL || len > shft->num_reg || data == NULL)
116+
{
117+
ESP_LOGE(tag, "%s: must have a valid argument;", __func__);
118+
err = ESP_ERR_INVALID_ARG;
119+
return err;
120+
}
121+
122+
if (shft->mode.bit_mode == SHIFT_BIT_MODE_MSB)
123+
{
124+
for (uint8_t i = 0; i < len; i++)
125+
{
126+
shift_reg_send8bits(data[i], shft);
127+
shft->reg_value[i] = data[i];
128+
}
129+
}
130+
else
131+
{
132+
for (int8_t i = len - 1; i >= 0; i--)
133+
{
134+
shift_reg_send8bits(data[i], shft);
135+
shft->reg_value[i] = data[i];
136+
}
137+
}
138+
139+
err = ESP_OK;
140+
141+
return err;
142+
}
143+
144+
esp_err_t shift_reg_send8bits(uint8_t data, shift_reg_config_s *shft)
145+
{
146+
esp_err_t err = ESP_FAIL;
147+
148+
if (shft == NULL)
149+
{
150+
ESP_LOGE(tag, "%s: must have a valid argument;", __func__);
151+
err = ESP_ERR_INVALID_ARG;
152+
return err;
153+
}
154+
155+
if (shft->mode.bit_mode == SHIFT_BIT_MODE_MSB)
156+
{
157+
// MSB Mode
158+
for (int8_t i = 7; i >= 0; i--)
159+
{
160+
if ((data >> i) & 1)
161+
{
162+
SETPIN(shft->pin.data);
163+
}
164+
else
165+
{
166+
CLRPIN(shft->pin.data);
167+
}
168+
169+
SETPIN(shft->pin.clk);
170+
_DELAY_US(1);
171+
CLRPIN(shft->pin.clk);
172+
_DELAY_US(1);
173+
}
174+
}
175+
else
176+
{
177+
// LSB Mode
178+
for (int8_t i = 0; i < 8; i++)
179+
{
180+
if ((data >> i) & 1)
181+
{
182+
SETPIN(shft->pin.data);
183+
}
184+
else
185+
{
186+
CLRPIN(shft->pin.data);
187+
}
188+
189+
SETPIN(shft->pin.clk);
190+
_DELAY_US(1);
191+
CLRPIN(shft->pin.clk);
192+
_DELAY_US(1);
193+
}
194+
}
195+
196+
err = ESP_OK;
197+
198+
return err;
199+
}
200+
201+
esp_err_t shift_reg_latch(shift_reg_config_s *shft)
202+
{
203+
SETPIN(shft->pin.latch);
204+
_DELAY_US(1);
205+
CLRPIN(shft->pin.latch);
206+
_DELAY_US(1);
207+
208+
return ESP_OK;
209+
}

0 commit comments

Comments
 (0)