Skip to content

Commit 39f6aeb

Browse files
Kainarxsuda-morris
authored andcommitted
feat(bitscrambler): add enable and disable function
1 parent 7cf5dac commit 39f6aeb

File tree

8 files changed

+73
-8
lines changed

8 files changed

+73
-8
lines changed

components/esp_driver_bitscrambler/include/driver/bitscrambler.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,30 @@ esp_err_t bitscrambler_start(bitscrambler_handle_t handle);
114114
*/
115115
esp_err_t bitscrambler_reset(bitscrambler_handle_t handle);
116116

117+
/**
118+
* @brief Enable BitScrambler
119+
* @note This function should be called before bitscrambler_load_program, bitscrambler_load_lut, bitscrambler_reset and bitscrambler_start.
120+
*
121+
* @param handle BitScrambler handle
122+
*
123+
* @return
124+
* - ESP_OK
125+
* - ESP_ERR_INVALID_ARG: Invalid handle
126+
*/
127+
esp_err_t bitscrambler_enable(bitscrambler_handle_t handle);
128+
129+
/**
130+
* @brief Disable BitScrambler
131+
* @note This function should be called before bitscrambler_free.
132+
*
133+
* @param handle BitScrambler handle
134+
*
135+
* @return
136+
* - ESP_OK
137+
* - ESP_ERR_INVALID_ARG: Invalid handle
138+
*/
139+
esp_err_t bitscrambler_disable(bitscrambler_handle_t handle);
140+
117141
#ifdef __cplusplus
118142
}
119143
#endif

components/esp_driver_bitscrambler/linker.lf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ entries:
44
if BITSCRAMBLER_CTRL_FUNC_IN_IRAM = y:
55
bitscrambler: bitscrambler_reset (noflash)
66
bitscrambler: bitscrambler_start (noflash)
7+
bitscrambler: bitscrambler_load_program (noflash)
8+
bitscrambler: bitscrambler_enable (noflash)
9+
bitscrambler: bitscrambler_disable (noflash)

components/esp_driver_bitscrambler/src/bitscrambler.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ static esp_err_t init_from_config(bitscrambler_t *bs, const bitscrambler_config_
9393
{
9494
bs->cfg = *config; //Copy config over
9595
bs->hw = BITSCRAMBLER_LL_GET_HW(0); //there's only one as of now; if there's more, we need to handle them as a pool.
96-
97-
//Attach to indicated peripheral.
98-
bitscrambler_ll_select_peripheral(bs->hw, bs->cfg.dir, config->attach_to);
99-
bitscrambler_ll_enable(bs->hw, bs->cfg.dir);
100-
10196
return ESP_OK;
10297
}
10398

@@ -147,8 +142,6 @@ esp_err_t bitscrambler_init_loopback(bitscrambler_handle_t handle, const bitscra
147142
handle->loopback = true;
148143
enable_clocks(handle);
149144
esp_err_t r = init_from_config(handle, config);
150-
//Loopback mode also needs RX channel set to the selected peripheral, even if it's not used.
151-
bitscrambler_ll_select_peripheral(handle->hw, BITSCRAMBLER_DIR_RX, config->attach_to);
152145
return r;
153146
}
154147

@@ -271,6 +264,40 @@ esp_err_t bitscrambler_register_extra_clean_up(bitscrambler_handle_t handle, bit
271264
return ESP_OK;
272265
}
273266

267+
esp_err_t bitscrambler_enable(bitscrambler_handle_t handle)
268+
{
269+
if (!handle) {
270+
return ESP_ERR_INVALID_ARG;
271+
}
272+
// Attach to indicated peripheral.
273+
bitscrambler_ll_select_peripheral(handle->hw, handle->cfg.dir, handle->cfg.attach_to);
274+
// bitscrambler_ll_enable(handle->hw, handle->cfg.dir);
275+
//enable loopback mode if requested
276+
bitscrambler_ll_enable_loopback(handle->hw, handle->loopback);
277+
if (handle->loopback) {
278+
//Loopback mode also needs RX channel set to the selected peripheral, even if it's not used.
279+
bitscrambler_ll_select_peripheral(handle->hw, BITSCRAMBLER_DIR_RX, handle->cfg.attach_to);
280+
}
281+
bitscrambler_ll_enable(handle->hw, handle->cfg.dir);
282+
return ESP_OK;
283+
}
284+
285+
esp_err_t bitscrambler_disable(bitscrambler_handle_t handle)
286+
{
287+
if (!handle) {
288+
return ESP_ERR_INVALID_ARG;
289+
}
290+
bitscrambler_ll_disable(handle->hw, handle->cfg.dir);
291+
// detach from peripheral
292+
bitscrambler_ll_select_peripheral(handle->hw, handle->cfg.dir, SOC_BITSCRAMBLER_ATTACH_NONE);
293+
if (handle->loopback) {
294+
// detach loopback RX channel as well
295+
bitscrambler_ll_select_peripheral(handle->hw, BITSCRAMBLER_DIR_RX, SOC_BITSCRAMBLER_ATTACH_NONE);
296+
}
297+
298+
return ESP_OK;
299+
}
300+
274301
void bitscrambler_free(bitscrambler_handle_t handle)
275302
{
276303
if (!handle) {

components/esp_driver_bitscrambler/src/bitscrambler_loopback.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ esp_err_t bitscrambler_loopback_create(bitscrambler_handle_t *handle, int attach
141141
.on_recv_eof = trans_done_cb,
142142
};
143143
gdma_register_rx_event_callbacks(bs->rx_channel, &rx_cbs, bs);
144-
144+
ESP_GOTO_ON_ERROR(bitscrambler_enable(&bs->bs), err, TAG, "failed to enable bitscrambler");
145145
*handle = (bitscrambler_handle_t)bs;
146146
return ESP_OK;
147147

@@ -175,6 +175,7 @@ static void bitscrambler_loopback_free(bitscrambler_loopback_t *bsl)
175175
static esp_err_t bitscrambler_loopback_cleanup(bitscrambler_handle_t bs, void* user_ctx)
176176
{
177177
bitscrambler_loopback_t *bsl = (bitscrambler_loopback_t*)user_ctx;
178+
ESP_RETURN_ON_ERROR(bitscrambler_disable(bs), TAG, "failed to disable bitscrambler");
178179
bitscrambler_loopback_free(bsl);
179180
return ESP_OK;
180181
}

components/soc/esp32c5/include/soc/bitscrambler_peri_select.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Note that these are the values written to HP_SYSTEM_BITSCRAMBLER_PERI_SEL_REG.
1111
*/
1212

13+
#define SOC_BITSCRAMBLER_ATTACH_NONE -1
1314
#define SOC_BITSCRAMBLER_ATTACH_GPSPI2 1
1415
#define SOC_BITSCRAMBLER_ATTACH_UHCI 2
1516
#define SOC_BITSCRAMBLER_ATTACH_I2S0 3

components/soc/esp32p4/include/soc/bitscrambler_peri_select.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Note that these are the values written to HP_SYSTEM_BITSCRAMBLER_PERI_SEL_REG.
1111
*/
1212

13+
#define SOC_BITSCRAMBLER_ATTACH_NONE -1
1314
#define SOC_BITSCRAMBLER_ATTACH_LCD_CAM 0
1415
#define SOC_BITSCRAMBLER_ATTACH_GPSPI2 1
1516
#define SOC_BITSCRAMBLER_ATTACH_GPSPI3 2

docs/en/api-reference/peripherals/bitscrambler.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ To use the assembled BitScrambler program, you would refer to it as such:
201201
202202
bitscrambler_handle_t bs;
203203
[...create bitscrambler instance]
204+
bitscrambler_enable(bs);
204205
bitscrambler_load_program(bs, my_bitscrambler_program);
205206
207+
[...]
208+
209+
bitscrambler_disable(bs);
206210
207211
.. _bitscrambler-loopback:
208212

docs/zh_CN/api-reference/peripherals/bitscrambler.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,12 @@ LUT 内容元指令
201201
202202
bitscrambler_handle_t bs;
203203
[...创建比特调节器实例]
204+
bitscrambler_enable(bs);
204205
bitscrambler_load_program(bs, my_bitscrambler_program);
205206
207+
[...]
208+
209+
bitscrambler_disable(bs);
206210
207211
.. _bitscrambler-loopback:
208212

0 commit comments

Comments
 (0)