Sfoglia il codice sorgente

feat(hal): add util function to reverse a 8bit byte

morris 2 anni fa
parent
commit
e92a1515be

+ 2 - 10
components/driver/rmt/rmt_encoder.c

@@ -18,6 +18,7 @@
 #include "esp_check.h"
 #include "driver/rmt_encoder.h"
 #include "rmt_private.h"
+#include "hal/hal_utils.h"
 
 static const char *TAG = "rmt";
 
@@ -46,15 +47,6 @@ static esp_err_t rmt_bytes_encoder_reset(rmt_encoder_t *encoder)
     return ESP_OK;
 }
 
-__attribute__((always_inline))
-static inline uint8_t _bitwise_reverse(uint8_t n)
-{
-    n = ((n & 0xf0) >> 4) | ((n & 0x0f) << 4);
-    n = ((n & 0xcc) >> 2) | ((n & 0x33) << 2);
-    n = ((n & 0xaa) >> 1) | ((n & 0x55) << 1);
-    return n;
-}
-
 static size_t IRAM_ATTR rmt_encode_bytes(rmt_encoder_t *encoder, rmt_channel_handle_t channel,
         const void *primary_data, size_t data_size, rmt_encode_state_t *ret_state)
 {
@@ -98,7 +90,7 @@ static size_t IRAM_ATTR rmt_encode_bytes(rmt_encoder_t *encoder, rmt_channel_han
         uint8_t cur_byte = nd[byte_index];
         // bit-wise reverse
         if (bytes_encoder->flags.msb_first) {
-            cur_byte = _bitwise_reverse(cur_byte);
+            cur_byte = hal_utils_bitwise_reverse8(cur_byte);
         }
         while ((len > 0) && (bit_index < 8)) {
             if (cur_byte & (1 << bit_index)) {

+ 3 - 2
components/hal/esp32p4/include/hal/ahb_dma_ll.h

@@ -10,6 +10,7 @@
 #include <stdbool.h>
 #include "hal/assert.h"
 #include "hal/misc.h"
+#include "hal/hal_utils.h"
 #include "hal/gdma_types.h"
 #include "hal/gdma_ll.h"
 #include "soc/ahb_dma_struct.h"
@@ -558,7 +559,7 @@ static inline void ahb_dma_ll_tx_crc_set_lfsr_data_mask(ahb_dma_dev_t *dev, uint
     dev->out_crc[channel].crc_data_en_addr.tx_crc_data_en_addr_chn = crc_bit;
     if (reverse_data_mask) {
         // "& 0xff" because the hardware only support 8-bit data
-        data_mask = _bitwise_reverse(data_mask & 0xFF);
+        data_mask = hal_utils_bitwise_reverse8(data_mask & 0xFF);
     }
     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->out_crc[channel].crc_data_en_wr_data, tx_crc_data_en_wr_data_chn, data_mask);
 }
@@ -619,7 +620,7 @@ static inline void ahb_dma_ll_rx_crc_set_lfsr_data_mask(ahb_dma_dev_t *dev, uint
     dev->in_crc[channel].crc_data_en_addr.rx_crc_data_en_addr_chn = crc_bit;
     if (reverse_data_mask) {
         // "& 0xff" because the hardware only support 8-bit data
-        data_mask = _bitwise_reverse(data_mask & 0xFF);
+        data_mask = hal_utils_bitwise_reverse8(data_mask & 0xFF);
     }
     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->in_crc[channel].crc_data_en_wr_data, rx_crc_data_en_wr_data_chn, data_mask);
 }

+ 3 - 2
components/hal/esp32p4/include/hal/axi_dma_ll.h

@@ -10,6 +10,7 @@
 #include <stdbool.h>
 #include "hal/assert.h"
 #include "hal/misc.h"
+#include "hal/hal_utils.h"
 #include "hal/gdma_types.h"
 #include "hal/gdma_ll.h"
 #include "soc/axi_dma_struct.h"
@@ -504,7 +505,7 @@ static inline void axi_dma_ll_tx_crc_set_lfsr_data_mask(axi_dma_dev_t *dev, uint
     dev->out[channel].crc.tx_crc_data_en_addr.tx_crc_data_en_addr_chn = crc_bit;
     if (reverse_data_mask) {
         // "& 0xff" because the hardware only support 8-bit data
-        data_mask = _bitwise_reverse(data_mask & 0xFF);
+        data_mask = hal_utils_bitwise_reverse8(data_mask & 0xFF);
     }
     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->out[channel].crc.tx_crc_data_en_wr_data, tx_crc_data_en_wr_data_chn, data_mask);
 }
@@ -565,7 +566,7 @@ static inline void axi_dma_ll_rx_crc_set_lfsr_data_mask(axi_dma_dev_t *dev, uint
     dev->in[channel].crc.rx_crc_data_en_addr.rx_crc_data_en_addr_chn = crc_bit;
     if (reverse_data_mask) {
         // "& 0xff" because the hardware only support 8-bit data
-        data_mask = _bitwise_reverse(data_mask & 0xFF);
+        data_mask = hal_utils_bitwise_reverse8(data_mask & 0xFF);
     }
     HAL_FORCE_MODIFY_U32_REG_FIELD(dev->in[channel].crc.rx_crc_data_en_wr_data, rx_crc_data_en_wr_data_chn, data_mask);
 }

+ 0 - 9
components/hal/esp32p4/include/hal/gdma_ll.h

@@ -82,15 +82,6 @@ static inline void gdma_ll_reset_register(int group_id)
 /// the critical section needs to declare the __DECLARE_RCC_ATOMIC_ENV variable in advance
 #define gdma_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; gdma_ll_reset_register(__VA_ARGS__)
 
-__attribute__((always_inline))
-static inline uint8_t _bitwise_reverse(uint8_t n)
-{
-    n = ((n & 0xf0) >> 4) | ((n & 0x0f) << 4);
-    n = ((n & 0xcc) >> 2) | ((n & 0x33) << 2);
-    n = ((n & 0xaa) >> 1) | ((n & 0x55) << 1);
-    return n;
-}
-
 #ifdef __cplusplus
 }
 #endif

+ 15 - 0
components/hal/include/hal/hal_utils.h

@@ -85,6 +85,21 @@ uint32_t hal_utils_calc_clk_div_frac_accurate(const hal_utils_clk_info_t *clk_in
  */
 uint32_t hal_utils_calc_clk_div_integer(const hal_utils_clk_info_t *clk_info, uint32_t *int_div);
 
+/**
+ * @brief Reverse the bit order of an 8-bit unsigned integer
+ *
+ * @param n The 8-bit unsigned integer to be reversed
+ * @return The 8-bit unsigned integer after reversing
+ */
+__attribute__((always_inline))
+static inline uint8_t hal_utils_bitwise_reverse8(uint8_t n)
+{
+    n = ((n & 0xf0) >> 4) | ((n & 0x0f) << 4);
+    n = ((n & 0xcc) >> 2) | ((n & 0x33) << 2);
+    n = ((n & 0xaa) >> 1) | ((n & 0x55) << 1);
+    return n;
+}
+
 #ifdef __cplusplus
 }
 #endif

+ 1 - 0
components/hal/test_apps/hal_utils/main/CMakeLists.txt

@@ -1,5 +1,6 @@
 idf_component_register(SRCS "test_app_main.c"
                             "test_calc_clk_div.c"
+                            "test_hal_utils_misc.c"
                     INCLUDE_DIRS "."
                     REQUIRES unity hal
                     WHOLE_ARCHIVE)

+ 21 - 0
components/hal/test_apps/hal_utils/main/test_hal_utils_misc.c

@@ -0,0 +1,21 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdio.h>
+#include "unity.h"
+#include "hal/hal_utils.h"
+
+TEST_CASE("test_hal_utils_bitwise_reverse", "[bit_ops]")
+{
+    uint8_t input_data[] = {0x00, 0x0F, 0xAA, 0x55, 0xC3};
+    uint8_t expected_data[] = {0x00, 0xF0, 0x55, 0xAA, 0xC3};
+    int num_test_cases = sizeof(input_data) / sizeof(input_data[0]);
+
+    for (int i = 0; i < num_test_cases; i++) {
+        uint8_t result = hal_utils_bitwise_reverse8(input_data[i]);
+        TEST_ASSERT_EQUAL_HEX8(expected_data[i], result);
+    }
+}