From 00ba95bac019229acfae8e98bbe0d8c3ee05840d Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Tue, 8 Apr 2014 18:43:26 +0100 Subject: [PATCH 1/1] maa: Initial commit of maa, a python and nodejs I2C skeleton only --- .gitignore | 13 ++++ CMakeLists.txt | 12 +++ COPYING | 20 +++++ README | 13 ++++ api/CMakeLists.txt | 0 examples/CMakeLists.txt | 5 ++ examples/hellomaa.cpp | 10 +++ examples/javascript/example.js | 5 ++ examples/python/readi2c.py | 9 +++ examples/readi2c.cpp | 11 +++ include/linux/README | 5 ++ include/linux/compiler.h | 18 +++++ include/linux/i2c-dev.h | 49 ++++++++++++ include/linux/posix_types.h | 34 +++++++++ include/linux/stddef.h | 19 +++++ include/linux/types.h | 43 +++++++++++ src/CMakeLists.txt | 4 + src/i2c/i2c.cxx | 78 +++++++++++++++++++ src/i2c/i2c.h | 137 ++++++++++++++++++++++++++++++++++ src/javascript/CMakeLists.txt | 27 +++++++ src/javascript/maajs.i | 8 ++ src/maa.cxx | 45 +++++++++++ src/maa.h | 34 +++++++++ src/maa.i | 16 ++++ src/mbed/PeripheralNames.h | 71 ++++++++++++++++++ src/mbed/PinNames.h | 166 +++++++++++++++++++++++++++++++++++++++++ src/mbed/i2c_api.h | 47 ++++++++++++ src/mbed/objects.h | 98 ++++++++++++++++++++++++ src/mbed/pinmap.h | 39 ++++++++++ src/python/CMakeLists.txt | 13 ++++ src/python/pymaa.i | 6 ++ 31 files changed, 1055 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 COPYING create mode 100644 README create mode 100644 api/CMakeLists.txt create mode 100644 examples/CMakeLists.txt create mode 100644 examples/hellomaa.cpp create mode 100644 examples/javascript/example.js create mode 100644 examples/python/readi2c.py create mode 100644 examples/readi2c.cpp create mode 100644 include/linux/README create mode 100644 include/linux/compiler.h create mode 100644 include/linux/i2c-dev.h create mode 100644 include/linux/posix_types.h create mode 100644 include/linux/stddef.h create mode 100644 include/linux/types.h create mode 100644 src/CMakeLists.txt create mode 100644 src/i2c/i2c.cxx create mode 100644 src/i2c/i2c.h create mode 100644 src/javascript/CMakeLists.txt create mode 100644 src/javascript/maajs.i create mode 100644 src/maa.cxx create mode 100644 src/maa.h create mode 100644 src/maa.i create mode 100644 src/mbed/PeripheralNames.h create mode 100644 src/mbed/PinNames.h create mode 100644 src/mbed/i2c_api.h create mode 100644 src/mbed/objects.h create mode 100644 src/mbed/pinmap.h create mode 100644 src/python/CMakeLists.txt create mode 100644 src/python/pymaa.i diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05fbd26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +build/ + +# Object files +*.o +*.lib +*.so +*.lo +*.la +*.pyc + +# Temp files +*.swp +*~ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..adfd8c7 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required (VERSION 2.6) +project (maa) + +set(SWIG_EXECUTABLE /usr/bin/swig) +FIND_PACKAGE(SWIG REQUIRED) +INCLUDE(${SWIG_USE_FILE}) + +SET(CMAKE_SWIG_FLAGS "") + +add_subdirectory (src) +add_subdirectory (api) +add_subdirectory (examples) diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..f3a2393 --- /dev/null +++ b/COPYING @@ -0,0 +1,20 @@ +Copyright © 2014 Intel Corporation + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..dd6f8da --- /dev/null +++ b/README @@ -0,0 +1,13 @@ +MAA - Low Level Skeleton Library for Communication on Intel platforms + +Library for C/C++ to interface with Galileo & other Intel platforms over: + +- I2C +- SPI +- GPIO +- AIO + +In a structured and sane API with port nanmes/numbering that match boards. + +The intent is to make it easier for developers and sensor manufacturers to map +their sensors & actuators on top of Intel hardware. diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..bcae1bc --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable (readi2c readi2c.cpp) +add_executable (hellomaa hellomaa.cpp) + +target_link_libraries (hellomaa maa) +target_link_libraries (readi2c maa) diff --git a/examples/hellomaa.cpp b/examples/hellomaa.cpp new file mode 100644 index 0000000..65f86c5 --- /dev/null +++ b/examples/hellomaa.cpp @@ -0,0 +1,10 @@ +#include "stdio.h" + +#include "../src/maa.h" + +int +main(int argc, char **argv) +{ + fprintf(stdout, "hello maa\n Version: %d\n", get_version()); + return 0; +} diff --git a/examples/javascript/example.js b/examples/javascript/example.js new file mode 100644 index 0000000..19aa624 --- /dev/null +++ b/examples/javascript/example.js @@ -0,0 +1,5 @@ +var m = require("maajs") + +console.log("maa version: " + m.get_version()); + +var r = new m.I2C(20, 21); diff --git a/examples/python/readi2c.py b/examples/python/readi2c.py new file mode 100644 index 0000000..afd200a --- /dev/null +++ b/examples/python/readi2c.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python2 + +import pymaa + +x = pymaa.I2C(27,28) +y= " " +ret = x.read(0x62, y, 2) + +print(y) diff --git a/examples/readi2c.cpp b/examples/readi2c.cpp new file mode 100644 index 0000000..523d865 --- /dev/null +++ b/examples/readi2c.cpp @@ -0,0 +1,11 @@ +#include "../src/maa.h" + +int +main () +{ + maa::I2C i2c(28, 27); + int addr = 0x62; + char data[2]; + int ret = i2c.read(addr, data, 2); + return ret; +} diff --git a/include/linux/README b/include/linux/README new file mode 100644 index 0000000..8d0a432 --- /dev/null +++ b/include/linux/README @@ -0,0 +1,5 @@ +These headers was automatically generated from a Linux kernel header +of the same name, to make information necessary for userspace to +call into the kernel available to libc. It contains only constants, +structures, and macros generated from the original header, and thus, +contains no copyrightable information. diff --git a/include/linux/compiler.h b/include/linux/compiler.h new file mode 100644 index 0000000..8e89655 --- /dev/null +++ b/include/linux/compiler.h @@ -0,0 +1,18 @@ +#ifndef _UAPI_LINUX_COMPILER_H +#define _UAPI_LINUX_COMPILER_H + +/* + * This file is not currently in the Linux kernel tree. + * Upstream uapi headers refer to but there is + * no such uapi file. We've sent this upstream, and are optimistically + * adding it to bionic in the meantime. This should be replaced by + * a scrubbed header from external/kernel-headers when possible. + * + * An alternative to this file is to check in a symbolic link to the + * non-uapi . That's fine for building bionic too. + */ + +#define __user +#define __force + +#endif /* _UAPI_LINUX_COMPILER_H */ diff --git a/include/linux/i2c-dev.h b/include/linux/i2c-dev.h new file mode 100644 index 0000000..92db53d --- /dev/null +++ b/include/linux/i2c-dev.h @@ -0,0 +1,49 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + *** To edit the content of this header, modify the corresponding + *** source file (e.g. under external/kernel-headers/original/) then + *** run bionic/libc/kernel/tools/update_all.py + *** + *** Any manual change here will be lost the next time this script will + *** be run. You've been warned! + *** + **************************************************************************** + ****************************************************************************/ +#ifndef _UAPI_LINUX_I2C_DEV_H +#define _UAPI_LINUX_I2C_DEV_H +#include +#include +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#define I2C_RETRIES 0x0701 +#define I2C_TIMEOUT 0x0702 +#define I2C_SLAVE 0x0703 +#define I2C_SLAVE_FORCE 0x0706 +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#define I2C_TENBIT 0x0704 +#define I2C_FUNCS 0x0705 +#define I2C_RDWR 0x0707 +#define I2C_PEC 0x0708 +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#define I2C_SMBUS 0x0720 +struct i2c_smbus_ioctl_data { + __u8 read_write; + __u8 command; +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ + __u32 size; + union i2c_smbus_data __user *data; +}; +struct i2c_rdwr_ioctl_data { +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ + struct i2c_msg __user *msgs; + __u32 nmsgs; +}; +#define I2C_RDRW_IOCTL_MAX_MSGS 42 +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#endif diff --git a/include/linux/posix_types.h b/include/linux/posix_types.h new file mode 100644 index 0000000..c197519 --- /dev/null +++ b/include/linux/posix_types.h @@ -0,0 +1,34 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + *** To edit the content of this header, modify the corresponding + *** source file (e.g. under external/kernel-headers/original/) then + *** run bionic/libc/kernel/tools/update_all.py + *** + *** Any manual change here will be lost the next time this script will + *** be run. You've been warned! + *** + **************************************************************************** + ****************************************************************************/ +#ifndef _LINUX_POSIX_TYPES_H +#define _LINUX_POSIX_TYPES_H +#include +#undef __FD_SETSIZE +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#define __FD_SETSIZE 1024 +typedef struct { + unsigned long fds_bits[__FD_SETSIZE / (8 * sizeof(long))]; +} __kernel_fd_set; +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +typedef void (*__kernel_sighandler_t)(int); +typedef int __kernel_key_t; +typedef int __kernel_mqd_t; +#include +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#endif diff --git a/include/linux/stddef.h b/include/linux/stddef.h new file mode 100644 index 0000000..8f2ed27 --- /dev/null +++ b/include/linux/stddef.h @@ -0,0 +1,19 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + *** To edit the content of this header, modify the corresponding + *** source file (e.g. under external/kernel-headers/original/) then + *** run bionic/libc/kernel/tools/update_all.py + *** + *** Any manual change here will be lost the next time this script will + *** be run. You've been warned! + *** + **************************************************************************** + ****************************************************************************/ +#include diff --git a/include/linux/types.h b/include/linux/types.h new file mode 100644 index 0000000..4fdfe10 --- /dev/null +++ b/include/linux/types.h @@ -0,0 +1,43 @@ +/**************************************************************************** + **************************************************************************** + *** + *** This header was automatically generated from a Linux kernel header + *** of the same name, to make information necessary for userspace to + *** call into the kernel available to libc. It contains only constants, + *** structures, and macros generated from the original header, and thus, + *** contains no copyrightable information. + *** + *** To edit the content of this header, modify the corresponding + *** source file (e.g. under external/kernel-headers/original/) then + *** run bionic/libc/kernel/tools/update_all.py + *** + *** Any manual change here will be lost the next time this script will + *** be run. You've been warned! + *** + **************************************************************************** + ****************************************************************************/ +#ifndef _UAPI_LINUX_TYPES_H +#define _UAPI_LINUX_TYPES_H +#include +#ifndef __ASSEMBLY__ +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#include +#define __bitwise__ +#define __bitwise +typedef __u16 __bitwise __le16; +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +typedef __u16 __bitwise __be16; +typedef __u32 __bitwise __le32; +typedef __u32 __bitwise __be32; +typedef __u64 __bitwise __le64; +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +typedef __u64 __bitwise __be64; +typedef __u16 __bitwise __sum16; +typedef __u32 __bitwise __wsum; +#define __aligned_u64 __u64 __attribute__((aligned(8))) +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ +#define __aligned_be64 __be64 __attribute__((aligned(8))) +#define __aligned_le64 __le64 __attribute__((aligned(8))) +#endif +#endif +/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..e75ba55 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,4 @@ +add_library (maa i2c/i2c.cxx maa.cxx) + +add_subdirectory(python) +add_subdirectory(javascript) diff --git a/src/i2c/i2c.cxx b/src/i2c/i2c.cxx new file mode 100644 index 0000000..eeb22cc --- /dev/null +++ b/src/i2c/i2c.cxx @@ -0,0 +1,78 @@ +/* + * Copyright (C) Intel Corporation. + * + * Author: Brendan Le Foll + * + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "i2c.h" + +using namespace maa; + +I2C::I2C(unsigned int sda, unsigned int scl) +{ +} + +void +I2C::frequency(int hz) +{ + _hz = hz; +} + +int +I2C::read(int address, char *data, int length, bool repeated) +{ + return 0; +} + +int +I2C::read(int ack) +{ + return 0; +} + +int +I2C::write(int address, const char *data, int length, bool repeated) +{ + return 0; +} + +int +I2C::write(int data) +{ +} + +void +I2C::start() +{ +} + +void +I2C::stop() +{ +} + +void +I2C::aquire() +{ +} diff --git a/src/i2c/i2c.h b/src/i2c/i2c.h new file mode 100644 index 0000000..e8d9fdd --- /dev/null +++ b/src/i2c/i2c.h @@ -0,0 +1,137 @@ +/* + * Copyright (C) Intel Corporation. + * + * Author: Brendan Le Foll + * + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +namespace maa { + +/** An I2C Master, used for communicating with I2C slave devices + * + * Example: + * @code + * // Read from I2C slave at address 0x62 + * + * #include "maa.h" + * + * I2C i2c(p28, p27); + * + * int main() { + * int address = 0x62; + * char data[2]; + * i2c.read(address, data, 2); + * } + * @endcode + */ +class I2C { + +public: + enum RxStatus { + NoData, + MasterGeneralCall, + MasterWrite, + MasterRead + }; + + enum Acknowledge { + NoACK = 0, + ACK = 1 + }; + + /** Create an I2C Master interface, connected to the specified pins + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + */ + I2C(unsigned int sda, unsigned int scl); + + /** Set the frequency of the I2C interface + * + * @param hz The bus frequency in hertz + */ + void frequency(int hz); + + /** Read from an I2C slave + * + * Performs a complete read transaction. The bottom bit of + * the address is forced to 1 to indicate a read. + * + * @param address 8-bit I2C slave address [ addr | 1 ] + * @param data Pointer to the byte-array to read data in to + * @param length Number of bytes to read + * @param repeated Repeated start, true - don't send stop at end + * + * @returns + * 0 on success (ack), + * non-0 on failure (nack) + */ + int read(int address, char *data, int length, bool repeated = false); + + /** Read a single byte from the I2C bus + * + * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) + * + * @returns + * the byte read + */ + int read(int ack); + + /** Write to an I2C slave + * + * Performs a complete write transaction. The bottom bit of + * the address is forced to 0 to indicate a write. + * + * @param address 8-bit I2C slave address [ addr | 0 ] + * @param data Pointer to the byte-array data to send + * @param length Number of bytes to send + * @param repeated Repeated start, true - do not send stop at end + * + * @returns + * 0 on success (ack), + * non-0 on failure (nack) + */ + int write(int address, const char *data, int length, bool repeated = false); + + /** Write single byte out on the I2C bus + * @param data data to write out on bus + * + * @returns + * '1' if an ACK was received, + * '0' otherwise + */ + int write(int data); + + /** Creates a start condition on the I2C bus + */ + void start(void); + + /** Creates a stop condition on the I2C bus + */ + void stop(void); + +protected: + void aquire(); + int _hz; +}; +} diff --git a/src/javascript/CMakeLists.txt b/src/javascript/CMakeLists.txt new file mode 100644 index 0000000..c894bcf --- /dev/null +++ b/src/javascript/CMakeLists.txt @@ -0,0 +1,27 @@ +FIND_PATH(NODE_ROOT_DIR "node/node.h") + +set(NODE_INCLUDE_DIRS + ${NODE_ROOT_DIR}/src + ${NODE_ROOT_DIR}/node + ${NODE_ROOT_DIR}/deps/v8/include + ${NODE_ROOT_DIR}/deps/uv/include +) + +include_directories( + ${NODE_INCLUDE_DIRS} + /usr/include/node + ${CMAKE_CURRENT_SOURCE_DIR}/.. +) + +SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES CPLUSPLUS ON) +SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES SWIG_FLAGS "-includeall") +#SET_SOURCE_FILES_PROPERTIES(../maa.i PROPERTIES SWIG_FLAGS "-no-moduleobject") +SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES SWIG_FLAGS "-node") + +SWIG_ADD_MODULE(maajs javascript maajs.i ../maa.cxx ../i2c/i2c.cxx) +SWIG_LINK_LIBRARIES(maajs ${NODE_LIBRARIES}) + +set_target_properties(maajs PROPERTIES + PREFIX "" + SUFFIX ".node" +) diff --git a/src/javascript/maajs.i b/src/javascript/maajs.i new file mode 100644 index 0000000..c776ba5 --- /dev/null +++ b/src/javascript/maajs.i @@ -0,0 +1,8 @@ +%module maajs +%{ + #include "maa.h" +%} + +%include + +%include ../maa.i diff --git a/src/maa.cxx b/src/maa.cxx new file mode 100644 index 0000000..56da190 --- /dev/null +++ b/src/maa.cxx @@ -0,0 +1,45 @@ +/* + * Copyright (C) Intel Corporation. + * + * Author: Brendan Le Foll + * + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include "maa.h" + +using namespace maa; + +int +get_version() +{ + return MAA_LIBRARY_VERSION; +} + +int +make_a_conn() +{ + maa::I2C i2c(28, 27); + int addr = 0x62; + char data[2]; + i2c.read(addr, data, 2); +} diff --git a/src/maa.h b/src/maa.h new file mode 100644 index 0000000..635cf4d --- /dev/null +++ b/src/maa.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) Intel Corporation. + * + * Author: Brendan Le Foll + * + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include "i2c/i2c.h" + +#define MAA_LIBRARY_VERSION 1 + +int get_version(); diff --git a/src/maa.i b/src/maa.i new file mode 100644 index 0000000..ca1ca0c --- /dev/null +++ b/src/maa.i @@ -0,0 +1,16 @@ +// Now list ANSI C/C++ declarations +int get_version(); + +namespace maa { +class I2C { +public: + I2C(unsigned int sda, unsigned int scl); + void frequency(int hz); + int read(int address, char *data, int length, bool repeated = false); + int read(int ack); + int write(int address, const char *data, int length, bool repeated = false); + int write(int data); + void start(void); + void stop(void); +}; +}; diff --git a/src/mbed/PeripheralNames.h b/src/mbed/PeripheralNames.h new file mode 100644 index 0000000..de9d909 --- /dev/null +++ b/src/mbed/PeripheralNames.h @@ -0,0 +1,71 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2014, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ + +#pragma once + +extern "C" { + +typedef enum { + ADC_1 = (int)ADC1_BASE +} ADCName; + +typedef enum { + DAC_1 = (int)DAC_BASE +} DACName; + +typedef enum { + UART_1 = (int)USART1_BASE, + UART_2 = (int)USART2_BASE, + UART_3 = (int)USART3_BASE +} UARTName; + +#define STDIO_UART_TX PA_2 +#define STDIO_UART_RX PA_3 +#define STDIO_UART UART_2 + +typedef enum { + SPI_2 = (int)SPI2_BASE, + SPI_3 = (int)SPI3_BASE +} SPIName; + +typedef enum { + I2C_1 = (int)I2C1_BASE, + I2C_2 = (int)I2C2_BASE, + I2C_3 = (int)I2C3_BASE +} I2CName; + +typedef enum { + PWM_1 = (int)TIM1_BASE, + PWM_15 = (int)TIM15_BASE, + PWM_16 = (int)TIM16_BASE, + PWM_17 = (int)TIM17_BASE +} PWMName; + +} diff --git a/src/mbed/PinNames.h b/src/mbed/PinNames.h new file mode 100644 index 0000000..21afb37 --- /dev/null +++ b/src/mbed/PinNames.h @@ -0,0 +1,166 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2014, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ + +#pragma once + +extern "C" { + +// MODE (see GPIOMode_TypeDef structure) +// OTYPE (see GPIOOType_TypeDef structure) +// PUPD (see GPIOPuPd_TypeDef structure) +// AFNUM (see AF_mapping constant table, 0xFF is not used) +#define STM_PIN_DATA(MODE, OTYPE, PUPD, AFNUM) (((AFNUM)<<8)|((PUPD)<<4)|((OTYPE)<<2)|((MODE)<<0)) +#define STM_PIN_MODE(X) (((X)>>0) & 0x3) +#define STM_PIN_OTYPE(X) (((X)>>2) & 0x1) +#define STM_PIN_PUPD(X) (((X)>>4) & 0x3) +#define STM_PIN_AFNUM(X) (((X)>>8) & 0xF) + +// High nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, 7=H) +// Low nibble = pin number +#define STM_PORT(X) (((uint32_t)(X) >> 4) & 0xF) +#define STM_PIN(X) ((uint32_t)(X) & 0xF) + +typedef enum { + PIN_INPUT, + PIN_OUTPUT +} PinDirection; + +typedef enum { + PA_0 = 0x00, + PA_1 = 0x01, + PA_2 = 0x02, + PA_3 = 0x03, + PA_4 = 0x04, + PA_5 = 0x05, + PA_6 = 0x06, + PA_7 = 0x07, + PA_8 = 0x08, + PA_9 = 0x09, + PA_10 = 0x0A, + PA_11 = 0x0B, + PA_12 = 0x0C, + PA_13 = 0x0D, + PA_14 = 0x0E, + PA_15 = 0x0F, + + PB_0 = 0x10, + PB_1 = 0x11, + PB_2 = 0x12, + PB_3 = 0x13, + PB_4 = 0x14, + PB_5 = 0x15, + PB_6 = 0x16, + PB_7 = 0x17, + PB_8 = 0x18, + PB_9 = 0x19, + PB_10 = 0x1A, + PB_11 = 0x1B, + PB_12 = 0x1C, + PB_13 = 0x1D, + PB_14 = 0x1E, + PB_15 = 0x1F, + + PC_0 = 0x20, + PC_1 = 0x21, + PC_2 = 0x22, + PC_3 = 0x23, + PC_4 = 0x24, + PC_5 = 0x25, + PC_6 = 0x26, + PC_7 = 0x27, + PC_8 = 0x28, + PC_9 = 0x29, + PC_10 = 0x2A, + PC_11 = 0x2B, + PC_12 = 0x2C, + PC_13 = 0x2D, + PC_14 = 0x2E, + PC_15 = 0x2F, + + PD_2 = 0x32, + + PF_0 = 0x50, + PF_1 = 0x51, + + // Arduino connector namings + A0 = PA_0, + A1 = PA_1, + A2 = PA_4, + A3 = PB_0, + A4 = PC_1, + A5 = PC_0, + D0 = PA_3, + D1 = PA_2, + D2 = PA_10, + D3 = PB_3, + D4 = PB_5, + D5 = PB_4, + D6 = PB_10, + D7 = PA_8, + D8 = PA_9, + D9 = PC_7, + D10 = PB_6, + D11 = PB_15, + D12 = PB_14, + D13 = PB_13, + D14 = PB_9, + D15 = PB_8, + + // Generic signals namings + LED1 = PA_5, + LED2 = PA_5, + LED3 = PA_5, + LED4 = PA_5, + USER_BUTTON = PC_13, + SERIAL_TX = PA_2, + SERIAL_RX = PA_3, + USBTX = PA_2, + USBRX = PA_3, + I2C_SCL = PB_8, + I2C_SDA = PB_9, + SPI_MOSI = PB_15, + SPI_MISO = PB_14, + SPI_SCK = PB_13, + SPI_CS = PB_6, + PWM_OUT = PB_4, + + // Not connected + NC = (int)0xFFFFFFFF +} PinName; + +typedef enum { + PullNone = 0, + PullUp = 1, + PullDown = 2, + OpenDrain = 3, + PullDefault = PullNone +} PinMode; + +} diff --git a/src/mbed/i2c_api.h b/src/mbed/i2c_api.h new file mode 100644 index 0000000..d9600c2 --- /dev/null +++ b/src/mbed/i2c_api.h @@ -0,0 +1,47 @@ +#pragma once + +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * Copyright (c) 2014 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +extern "C" { + +typedef struct i2c_s i2c_t; + +enum { + I2C_ERROR_NO_SLAVE = -1, + I2C_ERROR_BUS_BUSY = -2 +}; + +void i2c_init (i2c_t *obj, PinName sda, PinName scl); +void i2c_frequency (i2c_t *obj, int hz); +int i2c_start (i2c_t *obj); +int i2c_stop (i2c_t *obj); +int i2c_read (i2c_t *obj, int address, char *data, int length, int stop); +int i2c_write (i2c_t *obj, int address, const char *data, int length, int stop); +void i2c_reset (i2c_t *obj); +int i2c_byte_read (i2c_t *obj, int last); +int i2c_byte_write (i2c_t *obj, int data); + +#if DEVICE_I2CSLAVE +void i2c_slave_mode (i2c_t *obj, int enable_slave); +int i2c_slave_receive(i2c_t *obj); +int i2c_slave_read (i2c_t *obj, char *data, int length); +int i2c_slave_write (i2c_t *obj, const char *data, int length); +void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask); +#endif + +} diff --git a/src/mbed/objects.h b/src/mbed/objects.h new file mode 100644 index 0000000..d6b6db7 --- /dev/null +++ b/src/mbed/objects.h @@ -0,0 +1,98 @@ +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2014, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ + +#pragma once + +#include "PinNames.h" +#include "PeripheralNames.h" + +#if 0 +#include "cmsis.h" +#include "PortNames.h" +#endif + +extern "C" { +#if 0 +struct gpio_irq_s { + IRQn_Type irq_n; + uint32_t irq_index; + uint32_t event; +}; + +struct port_s { + PortName port; + uint32_t mask; + PinDirection direction; + __IO uint16_t *reg_in; + __IO uint16_t *reg_out; +}; + +struct analogin_s { + ADCName adc; + PinName pin; +}; + +struct dac_s { + DACName dac; + PinName channel; +}; + +struct serial_s { + UARTName uart; + int index; // Used by irq + uint32_t baudrate; + uint32_t databits; + uint32_t stopbits; + uint32_t parity; +}; + +struct spi_s { + SPIName spi; + uint32_t bits; + uint32_t cpol; + uint32_t cpha; + uint32_t mode; + uint32_t nss; + uint32_t br_presc; +}; +#endif +struct i2c_s { + I2CName i2c; +}; +#if 0 + +struct pwmout_s { + PWMName pwm; + PinName pin; + uint32_t period; + uint32_t pulse; +}; +#endif +} diff --git a/src/mbed/pinmap.h b/src/mbed/pinmap.h new file mode 100644 index 0000000..5752611 --- /dev/null +++ b/src/mbed/pinmap.h @@ -0,0 +1,39 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef MBED_PINMAP_H +#define MBED_PINMAP_H + +#include "PinNames.h" + +extern "C" { + +typedef struct { + PinName pin; + int peripheral; + int function; +} PinMap; + +void pin_function(PinName pin, int function); +void pin_mode (PinName pin, PinMode mode); + +uint32_t pinmap_peripheral(PinName pin, const PinMap* map); +uint32_t pinmap_merge (uint32_t a, uint32_t b); +void pinmap_pinout (PinName pin, const PinMap *map); +uint32_t pinmap_find_peripheral(PinName pin, const PinMap* map); + +} + +#endif diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt new file mode 100644 index 0000000..4c4ac91 --- /dev/null +++ b/src/python/CMakeLists.txt @@ -0,0 +1,13 @@ +FIND_PACKAGE(PythonLibs) +INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/.. + ${PYTHON_INCLUDE_DIRS} +) + +SET_SOURCE_FILES_PROPERTIES(pymaa.i PROPERTIES CPLUSPLUS ON) +SET_SOURCE_FILES_PROPERTIES(pymaa.i PROPERTIES SWIG_FLAGS "-includeall") + +SWIG_ADD_MODULE(pymaa python pymaa.i ../maa.cxx ../i2c/i2c.cxx) +SWIG_LINK_LIBRARIES(pymaa ${PYTHON_LIBRARIES}) diff --git a/src/python/pymaa.i b/src/python/pymaa.i new file mode 100644 index 0000000..4111175 --- /dev/null +++ b/src/python/pymaa.i @@ -0,0 +1,6 @@ +%module pymaa +%{ + #include "maa.h" +%} + +%include ../maa.h -- 2.7.4