Skip to content

Commit 970d1a1

Browse files
eren-terziogluxiaoxiang781216
authored andcommitted
esp32[c3|c6|h2]: Add efuse support
1 parent 10793d1 commit 970d1a1

File tree

15 files changed

+590
-4
lines changed

15 files changed

+590
-4
lines changed

arch/risc-v/src/common/espressif/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,12 @@ config ESPRESSIF_DMA
389389
default n
390390
select ARCH_DMA
391391

392+
config ESPRESSIF_EFUSE
393+
bool "EFUSE support"
394+
default n
395+
---help---
396+
Enable efuse support.
397+
392398
config ESPRESSIF_HR_TIMER
393399
bool
394400
default RTC_DRIVER

arch/risc-v/src/common/espressif/Make.defs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ ifeq ($(CONFIG_ESPRESSIF_DMA),y)
7373
CHIP_CSRCS += esp_dma.c
7474
endif
7575

76+
ifeq ($(CONFIG_ESPRESSIF_EFUSE),y)
77+
CHIP_CSRCS += esp_efuse.c
78+
endif
79+
7680
ifeq ($(CONFIG_ESPRESSIF_TWAI),y)
7781
CHIP_CSRCS += esp_twai.c
7882
endif
@@ -152,7 +156,7 @@ endif
152156

153157
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
154158
ifndef ESP_HAL_3RDPARTY_VERSION
155-
ESP_HAL_3RDPARTY_VERSION = e3899a2324c8e326db20f99f208e890fdd7a5b92
159+
ESP_HAL_3RDPARTY_VERSION = 87ccbf88b6fd490dae1993524e70f51bb2ea181d
156160
endif
157161

158162
ifndef ESP_HAL_3RDPARTY_URL
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/****************************************************************************
2+
* arch/risc-v/src/common/espressif/esp_efuse.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <stdlib.h>
28+
#include <debug.h>
29+
#include <debug.h>
30+
#include <errno.h>
31+
#include <assert.h>
32+
#include <string.h>
33+
#include <sys/param.h>
34+
#include <nuttx/efuse/efuse.h>
35+
#include <nuttx/irq.h>
36+
#include <nuttx/kmalloc.h>
37+
#include <nuttx/efuse/efuse.h>
38+
39+
#include "chip.h"
40+
41+
#include "riscv_internal.h"
42+
#include "espressif/esp_efuse.h"
43+
44+
#include "esp_efuse.h"
45+
#include "esp_clk.h"
46+
#include "hal/efuse_hal.h"
47+
#include "esp_efuse_table.h"
48+
#include "esp_efuse_chip.h"
49+
50+
/****************************************************************************
51+
* Private Types
52+
****************************************************************************/
53+
54+
struct esp_efuse_lowerhalf_s
55+
{
56+
const struct efuse_ops_s *ops; /* Lower half operations */
57+
void *upper; /* Pointer to efuse_upperhalf_s */
58+
};
59+
60+
/****************************************************************************
61+
* Private Functions Prototypes
62+
****************************************************************************/
63+
64+
/* "Lower half" driver methods */
65+
66+
static int esp_efuse_lowerhalf_read(struct efuse_lowerhalf_s *lower,
67+
const efuse_desc_t *field[],
68+
uint8_t *data, size_t bits_len);
69+
static int esp_efuse_lowerhalf_write(struct efuse_lowerhalf_s *lower,
70+
const efuse_desc_t *field[],
71+
const uint8_t *data,
72+
size_t bits_len);
73+
static int esp_efuse_lowerhalf_ioctl(struct efuse_lowerhalf_s *lower,
74+
int cmd, unsigned long arg);
75+
76+
/****************************************************************************
77+
* Private Data
78+
****************************************************************************/
79+
80+
/* "Lower half" driver methods */
81+
82+
static const struct efuse_ops_s g_esp_efuse_ops =
83+
{
84+
.read_field = esp_efuse_lowerhalf_read,
85+
.write_field = esp_efuse_lowerhalf_write,
86+
.ioctl = esp_efuse_lowerhalf_ioctl,
87+
};
88+
89+
/* EFUSE lower-half */
90+
91+
static struct esp_efuse_lowerhalf_s g_esp_efuse_lowerhalf =
92+
{
93+
.ops = &g_esp_efuse_ops,
94+
.upper = NULL,
95+
};
96+
97+
/****************************************************************************
98+
* Private functions
99+
****************************************************************************/
100+
101+
/****************************************************************************
102+
* Name: esp_efuse_lowerhalf_read
103+
*
104+
* Description:
105+
* Read value from EFUSE, writing it into an array.
106+
*
107+
* Input Parameters:
108+
* lower - A pointer the publicly visible representation of
109+
* the "lower-half" driver state structure
110+
* field - A pointer to describing the fields of efuse
111+
* dst - A pointer to array that contains the data for reading
112+
* bits_len - The number of bits required to read
113+
*
114+
* Returned Value:
115+
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
116+
*
117+
****************************************************************************/
118+
119+
static int esp_efuse_lowerhalf_read(struct efuse_lowerhalf_s *lower,
120+
const efuse_desc_t *field[],
121+
uint8_t *data, size_t bits_len)
122+
{
123+
int ret = OK;
124+
125+
/* Read the requested field */
126+
127+
ret = esp_efuse_read_field_blob((const esp_efuse_desc_t**)field,
128+
data,
129+
bits_len);
130+
131+
return ret;
132+
}
133+
134+
/****************************************************************************
135+
* Name: esp_efuse_lowerhalf_write
136+
*
137+
* Description:
138+
* Write array to EFUSE.
139+
*
140+
* Input Parameters:
141+
* lower - A pointer the publicly visible representation of
142+
* the "lower-half" driver state structure
143+
* field - A pointer to describing the fields of efuse
144+
* data - A pointer to array that contains the data for writing
145+
* bits_len - The number of bits required to write
146+
*
147+
* Returned Value:
148+
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
149+
*
150+
****************************************************************************/
151+
152+
static int esp_efuse_lowerhalf_write(struct efuse_lowerhalf_s *lower,
153+
const efuse_desc_t *field[],
154+
const uint8_t *data,
155+
size_t bits_len)
156+
{
157+
irqstate_t flags;
158+
int ret = OK;
159+
160+
flags = enter_critical_section();
161+
162+
ret = esp_efuse_write_field_blob((const esp_efuse_desc_t**)field,
163+
data,
164+
bits_len);
165+
166+
leave_critical_section(flags);
167+
168+
if (ret != OK)
169+
{
170+
return ERROR;
171+
}
172+
173+
return ret;
174+
}
175+
176+
/****************************************************************************
177+
* Name: esp_efuse_lowerhalf_ioctl
178+
*
179+
* Description:
180+
* Initialize the efuse driver. The efuse is initialized
181+
* and registered as 'devpath'.
182+
*
183+
* Input Parameters:
184+
* lower - A pointer the publicly visible representation of
185+
* the "lower-half" driver state structure
186+
* cmd - The ioctl command value
187+
* arg - The optional argument that accompanies the 'cmd'
188+
*
189+
* Returned Value:
190+
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
191+
*
192+
****************************************************************************/
193+
194+
static int esp_efuse_lowerhalf_ioctl(struct efuse_lowerhalf_s *lower,
195+
int cmd, unsigned long arg)
196+
{
197+
int ret = OK;
198+
199+
switch (cmd)
200+
{
201+
/* We don't have proprietary EFUSE ioctls */
202+
203+
default:
204+
{
205+
minfo("Unrecognized cmd: %d\n", cmd);
206+
ret = -ENOTTY;
207+
}
208+
break;
209+
}
210+
211+
return ret;
212+
}
213+
214+
/****************************************************************************
215+
* Public Functions
216+
****************************************************************************/
217+
218+
/****************************************************************************
219+
* Name: esp_efuse_initialize
220+
*
221+
* Description:
222+
* Initialize the efuse driver. The efuse is initialized
223+
* and registered as 'devpath'.
224+
*
225+
* Input Parameters:
226+
* devpath - The full path to the efuse device.
227+
* This should be of the form /dev/efuse
228+
*
229+
* Returned Value:
230+
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
231+
*
232+
****************************************************************************/
233+
234+
int esp_efuse_initialize(const char *devpath)
235+
{
236+
struct esp_efuse_lowerhalf_s *lower = NULL;
237+
int ret = OK;
238+
239+
DEBUGASSERT(devpath != NULL);
240+
241+
lower = &g_esp_efuse_lowerhalf;
242+
243+
/* Register the efuse upper driver */
244+
245+
lower->upper = efuse_register(devpath,
246+
(struct efuse_lowerhalf_s *)lower);
247+
248+
if (lower->upper == NULL)
249+
{
250+
/* The actual cause of the failure may have been a failure to allocate
251+
* perhaps a failure to register the efuse driver (such as if the
252+
* 'devpath' were not unique). We know here but we return EEXIST to
253+
* indicate the failure (implying the non-unique devpath).
254+
*/
255+
256+
ret = -EEXIST;
257+
}
258+
259+
return ret;
260+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/****************************************************************************
2+
* arch/risc-v/src/common/espressif/esp_efuse.h
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
#ifndef __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_EFUSE_H
24+
#define __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_EFUSE_H
25+
26+
/****************************************************************************
27+
* Included Files
28+
****************************************************************************/
29+
30+
#include <nuttx/efuse/efuse.h>
31+
32+
#ifndef __ASSEMBLY__
33+
34+
#undef EXTERN
35+
#if defined(__cplusplus)
36+
#define EXTERN extern "C"
37+
extern "C"
38+
{
39+
#else
40+
#define EXTERN extern
41+
#endif
42+
43+
/****************************************************************************
44+
* Public Functions Prototypes
45+
****************************************************************************/
46+
47+
/****************************************************************************
48+
* Name: esp_efuse_initialize
49+
*
50+
* Description:
51+
* Initialize the efuse driver. The efuse is initialized
52+
* and registered as 'devpath'.
53+
*
54+
* Input Parameters:
55+
* devpath - The full path to the efuse device.
56+
* This should be of the form /dev/efuse
57+
*
58+
* Returned Value:
59+
* Zero (OK) is returned on success. Otherwise -1 (ERROR).
60+
*
61+
****************************************************************************/
62+
63+
int esp_efuse_initialize(const char *devpath);
64+
65+
#ifdef __cplusplus
66+
}
67+
#endif
68+
#undef EXTERN
69+
70+
#endif /* __ASSEMBLY__ */
71+
#endif /* __ARCH_RISCV_SRC_COMMON_ESPRESSIF_ESP_EFUSE_H */

arch/xtensa/src/esp32/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ endif
218218

219219
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
220220
ifndef ESP_HAL_3RDPARTY_VERSION
221-
ESP_HAL_3RDPARTY_VERSION = e3899a2324c8e326db20f99f208e890fdd7a5b92
221+
ESP_HAL_3RDPARTY_VERSION = 87ccbf88b6fd490dae1993524e70f51bb2ea181d
222222
endif
223223

224224
ifndef ESP_HAL_3RDPARTY_URL

arch/xtensa/src/esp32s2/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ endif
147147

148148
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
149149
ifndef ESP_HAL_3RDPARTY_VERSION
150-
ESP_HAL_3RDPARTY_VERSION = e3899a2324c8e326db20f99f208e890fdd7a5b92
150+
ESP_HAL_3RDPARTY_VERSION = 87ccbf88b6fd490dae1993524e70f51bb2ea181d
151151
endif
152152

153153
ifndef ESP_HAL_3RDPARTY_URL

arch/xtensa/src/esp32s3/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ endif
220220

221221
ESP_HAL_3RDPARTY_REPO = esp-hal-3rdparty
222222
ifndef ESP_HAL_3RDPARTY_VERSION
223-
ESP_HAL_3RDPARTY_VERSION = e3899a2324c8e326db20f99f208e890fdd7a5b92
223+
ESP_HAL_3RDPARTY_VERSION = 87ccbf88b6fd490dae1993524e70f51bb2ea181d
224224
endif
225225

226226
ifndef ESP_HAL_3RDPARTY_URL

0 commit comments

Comments
 (0)