diff --git a/components/util/component.mk b/components/util/component.mk index 2341ae7b..8221403a 100644 --- a/components/util/component.mk +++ b/components/util/component.mk @@ -5,3 +5,7 @@ COMPONENT_ADD_INCLUDEDIRS := include COMPONENT_SRCDIRS := src + +ifndef IS_BOOTLOADER_BUILD +CFLAGS += -DUSING_IBUS_FASTER_GET +endif diff --git a/components/util/include/ibus_data.h b/components/util/include/ibus_data.h new file mode 100644 index 00000000..70a35bd6 --- /dev/null +++ b/components/util/include/ibus_data.h @@ -0,0 +1,93 @@ +// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +/** + * @brief ESP8266's IBus(instruction bus) only can read data of word type by 4 bytes align, + * so if users' code read a bytes or half word from IBus, an load/store exception will occure. + * + * The exception function will read the word by align and transform it to be the right + * data that users' code want to read. + * + * CPU read data from IRAM or flash by IBus. And "const" type value or string will locate at flash by default. + * So users' code read "const" type data whose type is byte or half word should lead to exception, + * and the exception operation is so slow. + * + * For example: + * + * static const char s_output[] = "123456789"; + * + * char get_byte(int index) + * { + * return s_output(index); + * } + * + * By above way, reading data may be not by 4 bytes align and word type. + * + * For this reason, we add the following function to read byte or half word without leading to exception. Firstly the function reads + * a word and then transform it to be the right data that users' code want to read. Users can modify above code to be following: + * + * static const char s_output[] = "123456789"; + * + * char get_byte(int index) + * { + * return ESP_IBUS_GET_U8_DATA(index, s_output); + * } + */ + +#ifdef USING_IBUS_FASTER_GET + +#ifndef DISABLE_IBUS_INLINE_FUNC +#define IBUS_INLINE inline +#else +#define IBUS_INLINE +#endif + +typedef union _ibus_data { + uint32_t u32_data[1]; + uint16_t u16_data[2]; + uint8_t u8_data[4]; +} ibus_data_t; + +#define __ESP_IBUS_GET_DATA(_type) \ + static IBUS_INLINE _type __esp_ibus_get_## _type ## _data(size_t index, const void *pbuf) \ + { \ + ibus_data_t data; \ + const uint8_t num = index / (sizeof(uint32_t) / sizeof(_type)); \ + const uint8_t off = index % (sizeof(uint32_t) / sizeof(_type)); \ + const uint32_t *ptable = (const uint32_t *)pbuf; \ + \ + data.u32_data[0] = ptable[num]; \ + \ + if (sizeof(_type) == sizeof(uint8_t)) \ + return data.u8_data[off]; \ + else if (sizeof(_type) == sizeof(uint16_t)) \ + return data.u16_data[off]; \ + } + +__ESP_IBUS_GET_DATA(uint8_t) +__ESP_IBUS_GET_DATA(uint16_t) + +#define ESP_IBUS_GET_U8_DATA(_index, _pbuf) __esp_ibus_get_uint8_t_data(_index, _pbuf) +#define ESP_IBUS_GET_U16_DATA(_index, _pbuf) __esp_ibus_get_uint16_t_data(_index, _pbuf) +#else +#define ESP_IBUS_GET_U8_DATA(_index, _pbuf) ((const uint8_t *)_pbuf)[_index] +#define ESP_IBUS_GET_U16_DATA(_index, _pbuf) ((const uint16_t *)_pbuf)[_index] +#endif + + diff --git a/components/util/src/base64.c b/components/util/src/base64.c index 615bb8ad..dcea1a3c 100644 --- a/components/util/src/base64.c +++ b/components/util/src/base64.c @@ -14,6 +14,7 @@ #include "sdkconfig.h" #include "esp_base64.h" +#include "ibus_data.h" #include #include @@ -44,26 +45,6 @@ static const uint32_t s_base64_dec_lut[32] = { 0x2c2b2a29, 0x302f2e2d, 0x7f333231, 0x7f7f7f7f }; -static inline uint8_t __map_base64_enc_data(uint8_t index) -{ - cache_t tmp; - uint8_t num = index / 4; - uint8_t off = index % 4; - tmp.u32d = s_base64_enc_lut[num]; - - return tmp.u8d[off]; -} - -static inline uint8_t __map_base64_dec_data(uint8_t index) -{ - cache_t tmp; - uint8_t num = index / 4; - uint8_t off = index % 4; - tmp.u32d = s_base64_dec_lut[num]; - - return tmp.u8d[off]; -} - /* * Encode a buffer into base64 format */ @@ -93,21 +74,21 @@ int esp_base64_encode(const void *p_src, uint32_t slen, void *p_dst, uint32_t dl C2 = *src++; C3 = *src++; - *p++ = __map_base64_enc_data((C1 >> 2) & 0x3F); - *p++ = __map_base64_enc_data((((C1 & 3) << 4) + (C2 >> 4)) & 0x3F); - *p++ = __map_base64_enc_data((((C2 & 15) << 2) + (C3 >> 6)) & 0x3F); - *p++ = __map_base64_enc_data(C3 & 0x3F); + *p++ = ESP_IBUS_GET_U8_DATA((C1 >> 2) & 0x3F, s_base64_enc_lut); + *p++ = ESP_IBUS_GET_U8_DATA((((C1 & 3) << 4) + (C2 >> 4)) & 0x3F, s_base64_enc_lut); + *p++ = ESP_IBUS_GET_U8_DATA((((C2 & 15) << 2) + (C3 >> 6)) & 0x3F, s_base64_enc_lut); + *p++ = ESP_IBUS_GET_U8_DATA(C3 & 0x3F, s_base64_enc_lut); } if (i < slen) { C1 = *src++; C2 = ((i + 1) < slen) ? *src++ : 0; - *p++ = __map_base64_enc_data((C1 >> 2) & 0x3F); - *p++ = __map_base64_enc_data((((C1 & 3) << 4) + (C2 >> 4)) & 0x3F); + *p++ = ESP_IBUS_GET_U8_DATA((C1 >> 2) & 0x3F, s_base64_enc_lut); + *p++ = ESP_IBUS_GET_U8_DATA((((C1 & 3) << 4) + (C2 >> 4)) & 0x3F, s_base64_enc_lut); if ((i + 1) < slen) - *p++ = __map_base64_enc_data(((C2 & 15) << 2) & 0x3F); + *p++ = ESP_IBUS_GET_U8_DATA(((C2 & 15) << 2) & 0x3F, s_base64_enc_lut); else *p++ = '='; @@ -161,10 +142,10 @@ int esp_base64_decode(const void *p_src, uint32_t slen, void *p_dst, uint32_t dl if (src[i] == '=' && ++j > 2) return -EINVAL; - if (src[i] > 127 || __map_base64_dec_data(src[i]) == 127) + if (src[i] > 127 || ESP_IBUS_GET_U8_DATA(src[i], s_base64_dec_lut) == 127) return -EINVAL; - if (__map_base64_dec_data(src[i]) < 64 && j != 0) + if (ESP_IBUS_GET_U8_DATA(src[i], s_base64_dec_lut) < 64 && j != 0) return -EINVAL; n++; @@ -183,8 +164,8 @@ int esp_base64_decode(const void *p_src, uint32_t slen, void *p_dst, uint32_t dl if (*src == '\r' || *src == '\n' || *src == ' ') continue; - j -= (__map_base64_dec_data(*src) == 64); - x = (x << 6) | (__map_base64_dec_data(*src) & 0x3F); + j -= (ESP_IBUS_GET_U8_DATA(*src, s_base64_dec_lut) == 64); + x = (x << 6) | (ESP_IBUS_GET_U8_DATA(*src, s_base64_dec_lut) & 0x3F); if (++n == 4) { n = 0;