Skip to content

Commit 37e1f5d

Browse files
FelipeMdeOxiaoxiang781216
authored andcommitted
Add spi slave dev to esp32 xtensa
1 parent a0a85e5 commit 37e1f5d

File tree

4 files changed

+175
-3
lines changed

4 files changed

+175
-3
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************
2+
* boards/xtensa/esp32/common/include/esp32_board_spislavedev.h
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
#ifndef __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_SPISLAVEDEV_H
22+
#define __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_SPISLAVEDEV_H
23+
24+
/****************************************************************************
25+
* Included Files
26+
****************************************************************************/
27+
28+
#include <nuttx/config.h>
29+
30+
/****************************************************************************
31+
* Pre-processor Definitions
32+
****************************************************************************/
33+
34+
#ifndef __ASSEMBLY__
35+
36+
#undef EXTERN
37+
#if defined(__cplusplus)
38+
#define EXTERN extern "C"
39+
extern "C"
40+
{
41+
#else
42+
#define EXTERN extern
43+
#endif
44+
45+
/****************************************************************************
46+
* Public Function Prototypes
47+
****************************************************************************/
48+
49+
/****************************************************************************
50+
* Name: board_spislavedev_initialize
51+
*
52+
* Description:
53+
* Initialize SPI Slave driver and register the /dev/spislv device.
54+
*
55+
* Input Parameters:
56+
* bus - The SPI bus number, used to build the device path as /dev/spislvN
57+
*
58+
* Returned Value:
59+
* Zero (OK) is returned on success; A negated errno value is returned
60+
* to indicate the nature of any failure.
61+
*
62+
****************************************************************************/
63+
64+
#ifdef CONFIG_SPI_SLAVE
65+
int board_spislavedev_initialize(int bus);
66+
#endif
67+
68+
#undef EXTERN
69+
#if defined(__cplusplus)
70+
}
71+
#endif
72+
73+
#endif /* __ASSEMBLY__ */
74+
#endif /* __BOARDS_XTENSA_ESP32_COMMON_INCLUDE_ESP32_BOARD_SPISLAVEDEV_H */

boards/xtensa/esp32/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ ifeq ($(CONFIG_SPI_DRIVER),y)
6464
CSRCS += esp32_board_spidev.c
6565
endif
6666

67+
ifeq ($(CONFIG_SPI_SLAVE_DRIVER),y)
68+
CSRCS += esp32_board_spislavedev.c
69+
endif
70+
6771
ifeq ($(CONFIG_ESP32_SPIFLASH),y)
6872
CSRCS += esp32_board_spiflash.c
6973
endif
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/****************************************************************************
2+
* boards/xtensa/esp32/common/src/esp32_board_spislavedev.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
27+
#include <stdio.h>
28+
#include <debug.h>
29+
#include <errno.h>
30+
31+
#include <nuttx/spi/slave.h>
32+
33+
#include "esp32_spi.h"
34+
35+
#include "esp32_board_spislavedev.h"
36+
37+
/****************************************************************************
38+
* Public Functions
39+
****************************************************************************/
40+
41+
/****************************************************************************
42+
* Name: board_spislavedev_initialize
43+
*
44+
* Description:
45+
* Initialize SPI Slave driver and register the /dev/spislv device.
46+
*
47+
* Input Parameters:
48+
* bus - The SPI bus number, used to build the device path as /dev/spislvN
49+
*
50+
* Returned Value:
51+
* Zero (OK) is returned on success; A negated errno value is returned
52+
* to indicate the nature of any failure.
53+
*
54+
****************************************************************************/
55+
56+
int board_spislavedev_initialize(int bus)
57+
{
58+
int ret;
59+
60+
struct spi_slave_ctrlr_s *ctrlr;
61+
62+
spiinfo("Initializing /dev/spislv%d...\n", bus);
63+
64+
/* Initialize SPI Slave controller device */
65+
66+
ctrlr = esp32_spislv_ctrlr_initialize(bus);
67+
if (ctrlr == NULL)
68+
{
69+
spierr("Failed to initialize SPI%d as slave.\n", bus);
70+
return -ENODEV;
71+
}
72+
73+
ret = spi_slave_register(ctrlr, bus);
74+
if (ret < 0)
75+
{
76+
spierr("Failed to register /dev/spislv%d: %d\n", bus, ret);
77+
78+
esp32_spislv_ctrlr_uninitialize(ctrlr);
79+
}
80+
81+
return ret;
82+
}

boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@
153153
# include "esp32_spi.h"
154154
#endif
155155

156+
#ifdef CONFIG_SPI_SLAVE_DRIVER
157+
# include "esp32_spi.h"
158+
# include "esp32_board_spislavedev.h"
159+
#endif
160+
156161
#ifdef CONFIG_LCD_BACKPACK
157162
# include "esp32_lcd_backpack.h"
158163
#endif
@@ -702,15 +707,22 @@ int esp32_bringup(void)
702707
}
703708
#endif
704709

705-
#ifdef CONFIG_SPI_DRIVER
706-
# ifdef CONFIG_ESP32_SPI2
710+
#if defined(CONFIG_ESP32_SPI2) && defined(CONFIG_SPI_DRIVER)
707711
ret = board_spidev_initialize(ESP32_SPI2);
708712
if (ret < 0)
709713
{
710714
syslog(LOG_ERR, "Failed to initialize SPI%d driver: %d\n",
711715
ESP32_SPI2, ret);
712716
}
713-
# endif
717+
#endif
718+
719+
# if defined(CONFIG_ESP32_SPI2) && defined(CONFIG_SPI_SLAVE_DRIVER)
720+
ret = board_spislavedev_initialize(ESP32_SPI2);
721+
if (ret < 0)
722+
{
723+
syslog(LOG_ERR, "Failed to initialize SPI%d Slave driver: %d\n",
724+
ESP32_SPI2, ret);
725+
}
714726
#endif
715727

716728
#ifdef CONFIG_WS2812

0 commit comments

Comments
 (0)