|
| 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