modules: canopennode: move glue code to modules directory

Move the Zephyr-specific interface and support code for CANopenNode into
the modules directory. Consolidate the CMakeLists.txt files into one.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
diff --git a/modules/Kconfig b/modules/Kconfig
index 5144af3..abae133 100644
--- a/modules/Kconfig
+++ b/modules/Kconfig
@@ -9,7 +9,6 @@
 
 source "modules/Kconfig.altera"
 source "modules/Kconfig.atmel"
-source "modules/Kconfig.canopennode"
 source "modules/Kconfig.civetweb"
 source "modules/Kconfig.cmsis"
 source "modules/Kconfig.cypress"
@@ -64,6 +63,9 @@
 comment "loramac-node module not available."
 	depends on !ZEPHYR_LORAMAC_NODE_MODULE
 
+comment "CANopenNode module not available."
+	depends on !ZEPHYR_CANOPENNODE_MODULE
+
 # This ensures that symbols are available in Kconfig for dependency checking
 # and referencing, while keeping the settings themselves unavailable when the
 # modules are not present in the workspace
diff --git a/modules/Kconfig.canopennode b/modules/Kconfig.canopennode
deleted file mode 100644
index da04336..0000000
--- a/modules/Kconfig.canopennode
+++ /dev/null
@@ -1,9 +0,0 @@
-# CANopenNode CANopen protocol stack
-
-# Copyright (c) 2019 Vestas Wind System A/S
-# SPDX-License-Identifier: Apache-2.0
-
-config CANOPENNODE
-	bool "CANopenNode Support"
-	help
-	  This option enables the CANopenNode library.
diff --git a/modules/canopennode/CMakeLists.txt b/modules/canopennode/CMakeLists.txt
new file mode 100644
index 0000000..380f8a9
--- /dev/null
+++ b/modules/canopennode/CMakeLists.txt
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: Apache-2.0
+
+if(CONFIG_CANOPENNODE)
+
+  set(CANOPENNODE_DIR ${ZEPHYR_CURRENT_MODULE_DIR})
+
+  zephyr_library()
+
+  zephyr_include_directories(
+    ${CANOPENNODE_DIR}
+    ${CANOPENNODE_DIR}/stack
+    .
+  )
+
+  zephyr_library_sources(
+    ${CANOPENNODE_DIR}/CANopen.c
+    ${CANOPENNODE_DIR}/stack/CO_Emergency.c
+    ${CANOPENNODE_DIR}/stack/CO_HBconsumer.c
+    ${CANOPENNODE_DIR}/stack/CO_LSSmaster.c
+    ${CANOPENNODE_DIR}/stack/CO_LSSslave.c
+    ${CANOPENNODE_DIR}/stack/CO_NMT_Heartbeat.c
+    ${CANOPENNODE_DIR}/stack/CO_PDO.c
+    ${CANOPENNODE_DIR}/stack/CO_SDO.c
+    ${CANOPENNODE_DIR}/stack/CO_SDOmaster.c
+    ${CANOPENNODE_DIR}/stack/CO_SYNC.c
+    ${CANOPENNODE_DIR}/stack/CO_TIME.c
+    ${CANOPENNODE_DIR}/stack/CO_trace.c
+    CO_driver.c
+  )
+
+  zephyr_library_sources_ifdef(CONFIG_CANOPEN_SYNC_THREAD canopen_sync.c)
+  zephyr_library_sources_ifdef(CONFIG_CANOPEN_STORAGE canopen_storage.c)
+  zephyr_library_sources_ifdef(CONFIG_CANOPEN_LEDS canopen_leds.c)
+  zephyr_library_sources_ifdef(CONFIG_CANOPEN_PROGRAM_DOWNLOAD canopen_program.c)
+
+endif()
diff --git a/subsys/canbus/canopen/CO_driver.c b/modules/canopennode/CO_driver.c
similarity index 100%
rename from subsys/canbus/canopen/CO_driver.c
rename to modules/canopennode/CO_driver.c
diff --git a/subsys/canbus/canopen/CO_driver_target.h b/modules/canopennode/CO_driver_target.h
similarity index 94%
rename from subsys/canbus/canopen/CO_driver_target.h
rename to modules/canopennode/CO_driver_target.h
index 16afaa0..15d90fc 100644
--- a/subsys/canbus/canopen/CO_driver_target.h
+++ b/modules/canopennode/CO_driver_target.h
@@ -4,8 +4,8 @@
  * SPDX-License-Identifier: Apache-2.0
  */
 
-#ifndef ZEPHYR_SUBSYS_CANBUS_CANOPEN_CO_DRIVER_H
-#define ZEPHYR_SUBSYS_CANBUS_CANOPEN_CO_DRIVER_H
+#ifndef ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H
+#define ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H
 
 /*
  * Zephyr RTOS CAN driver interface and configuration for CANopenNode
@@ -120,4 +120,4 @@
 }
 #endif
 
-#endif /* ZEPHYR_SUBSYS_CANBUS_CANOPEN_CO_DRIVER_H */
+#endif /* ZEPHYR_MODULES_CANOPENNODE_CO_DRIVER_H */
diff --git a/subsys/canbus/canopen/Kconfig b/modules/canopennode/Kconfig
similarity index 88%
rename from subsys/canbus/canopen/Kconfig
rename to modules/canopennode/Kconfig
index 68f8b09..97143cf 100644
--- a/subsys/canbus/canopen/Kconfig
+++ b/modules/canopennode/Kconfig
@@ -1,22 +1,18 @@
-# CANopen configuration options
+# CANopenNode CANopen protocol stack configuration options
 
 # Copyright (c) 2019 Vestas Wind Systems A/S
 # SPDX-License-Identifier: Apache-2.0
 
-menuconfig CANOPEN
-	bool "CANopen protocol support"
+config ZEPHYR_CANOPENNODE_MODULE
+	bool
+
+config CANOPENNODE
+	bool "CANopenNode support"
 	depends on CAN
-	select CANOPENNODE
 	help
-	  Enable CANopen (EN 50325-4) (CiA 301) protocol
-	  support. Support is provided by the 3rd party CANopenNode
-	  protocol stack.
+	  This option enables the CANopenNode library.
 
-if CANOPEN
-
-module = CANOPEN
-module-str = CANOPEN
-source "subsys/logging/Kconfig.template.log_config"
+if CANOPENNODE
 
 config CANOPEN_SDO_BUFFER_SIZE
 	int "CANopen SDO buffer size"
@@ -113,4 +109,4 @@
 	  Enable support for program download over CANopen according
 	  to the CiA 302-3 (draft) specification.
 
-endif # CANOPEN
+endif # CANOPENNODE
diff --git a/include/canbus/canopen.h b/modules/canopennode/canbus/canopen.h
similarity index 100%
rename from include/canbus/canopen.h
rename to modules/canopennode/canbus/canopen.h
diff --git a/subsys/canbus/canopen/canopen_leds.c b/modules/canopennode/canopen_leds.c
similarity index 100%
rename from subsys/canbus/canopen/canopen_leds.c
rename to modules/canopennode/canopen_leds.c
diff --git a/subsys/canbus/canopen/canopen_program.c b/modules/canopennode/canopen_program.c
similarity index 100%
rename from subsys/canbus/canopen/canopen_program.c
rename to modules/canopennode/canopen_program.c
diff --git a/subsys/canbus/canopen/canopen_storage.c b/modules/canopennode/canopen_storage.c
similarity index 100%
rename from subsys/canbus/canopen/canopen_storage.c
rename to modules/canopennode/canopen_storage.c
diff --git a/subsys/canbus/canopen/canopen_sync.c b/modules/canopennode/canopen_sync.c
similarity index 100%
rename from subsys/canbus/canopen/canopen_sync.c
rename to modules/canopennode/canopen_sync.c
diff --git a/subsys/canbus/CMakeLists.txt b/subsys/canbus/CMakeLists.txt
index c88cf12..913501f 100644
--- a/subsys/canbus/CMakeLists.txt
+++ b/subsys/canbus/CMakeLists.txt
@@ -1,4 +1,3 @@
 # SPDX-License-Identifier: Apache-2.0
 
-add_subdirectory_ifdef(CONFIG_CANOPEN canopen)
 add_subdirectory_ifdef(CONFIG_ISOTP isotp)
diff --git a/subsys/canbus/Kconfig b/subsys/canbus/Kconfig
index dc68b5a..eb23a3f 100644
--- a/subsys/canbus/Kconfig
+++ b/subsys/canbus/Kconfig
@@ -5,7 +5,7 @@
 
 menu "Controller Area Network (CAN) bus subsystem"
 
-source "subsys/canbus/canopen/Kconfig"
+source "subsys/canbus/Kconfig.canopen"
 source "subsys/canbus/isotp/Kconfig"
 
 endmenu
diff --git a/subsys/canbus/Kconfig.canopen b/subsys/canbus/Kconfig.canopen
new file mode 100644
index 0000000..c680219
--- /dev/null
+++ b/subsys/canbus/Kconfig.canopen
@@ -0,0 +1,19 @@
+# Copyright (c) 2019 - 2021 Vestas Wind Systems A/S
+# SPDX-License-Identifier: Apache-2.0
+
+menuconfig CANOPEN
+	bool "CANopen protocol support"
+	depends on CAN
+	select CANOPENNODE
+	help
+	  Enable CANopen (EN 50325-4) (CiA 301) protocol
+	  support. Support is provided by the 3rd party CANopenNode
+	  protocol stack.
+
+if CANOPEN
+
+module = CANOPEN
+module-str = CANopen
+source "subsys/logging/Kconfig.template.log_config"
+
+endif # CANOPEN
diff --git a/subsys/canbus/canopen/CMakeLists.txt b/subsys/canbus/canopen/CMakeLists.txt
deleted file mode 100644
index f43e7bd..0000000
--- a/subsys/canbus/canopen/CMakeLists.txt
+++ /dev/null
@@ -1,9 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-
-zephyr_library()
-zephyr_library_sources_ifdef(CONFIG_CANOPEN CO_driver.c)
-zephyr_library_sources_ifdef(CONFIG_CANOPEN_SYNC_THREAD canopen_sync.c)
-zephyr_library_sources_ifdef(CONFIG_CANOPEN_STORAGE canopen_storage.c)
-zephyr_library_sources_ifdef(CONFIG_CANOPEN_LEDS canopen_leds.c)
-zephyr_library_sources_ifdef(CONFIG_CANOPEN_PROGRAM_DOWNLOAD canopen_program.c)
-zephyr_include_directories(.)
diff --git a/west.yml b/west.yml
index 41711d9..c7f67a0 100644
--- a/west.yml
+++ b/west.yml
@@ -26,7 +26,7 @@
   # Please add items below based on alphabetical order
   projects:
     - name: canopennode
-      revision: 468d350028a975b96563e58344de48281a0ab371
+      revision: f167efe85c8c7de886f1bc47f9173cfb8a346bb5
       path: modules/lib/canopennode
     - name: civetweb
       revision: 094aeb41bb93e9199d24d665ee43e9e05d6d7b1c