Javascript wrapper for SPI in Mbed OS.

Dependents:   ST_SENSOR_JS

Committer:
akhtar.syedzeeshan@gmail.com
Date:
Wed Jan 17 10:57:15 2018 +0100
Revision:
1:1e016cfcd06f
Parent:
0:3a79326cadb9
Code fixes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 1 /**
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 2 ******************************************************************************
akhtar.syedzeeshan@gmail.com 1:1e016cfcd06f 3 * @file SPI-js.cpp
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 4 * @author ST
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 5 * @version V1.0.0
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 6 * @date 25 October 2017
akhtar.syedzeeshan@gmail.com 1:1e016cfcd06f 7 * @brief Implementation of SPI for Javascript.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 8 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 9 * @attention
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 10 *
akhtar.syedzeeshan@gmail.com 1:1e016cfcd06f 11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 12 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 13 * Redistribution and use in source and binary forms, with or without modification,
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 14 * are permitted provided that the following conditions are met:
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 15 * 1. Redistributions of source code must retain the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 16 * this list of conditions and the following disclaimer.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 18 * this list of conditions and the following disclaimer in the documentation
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 19 * and/or other materials provided with the distribution.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 21 * may be used to endorse or promote products derived from this software
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 22 * without specific prior written permission.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 23 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 34 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 35 ******************************************************************************
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 36 */
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 37
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 38
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 39 /* Includes ------------------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 40
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 41 #include "jerryscript-mbed-util/logging.h"
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 42 #include "jerryscript-mbed-library-registry/wrap_tools.h"
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 43
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 44 // Load the library that we'll wrap
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 45 #include "SPI.h"
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 46
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 47 #include "mbed.h"
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 48
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 49 /* Class Implementation ------------------------------------------------------*/
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 50
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 51 /**
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 52 * SPI#destructor
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 53 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 54 * Called if/when the SPI object is GC'ed.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 55 */
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 56 void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(SPI) (void *void_ptr) {
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 57 delete static_cast<SPI*>(void_ptr);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 58 }
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 59
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 60 /**
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 61 * Type infomation of the native SPI pointer
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 62 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 63 * Set SPI#destructor as the free callback.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 64 */
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 65 static const jerry_object_native_info_t native_obj_type_info = {
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 66 .free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(SPI)
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 67 };
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 68
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 69 /**
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 70 * SPI (native JavaScript constructor)
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 71 *
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 72 * @param sda mbed pin for SPI data
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 73 * @param scl mbed pin for SPI clock
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 74 * @returns a JavaScript object representing the SPI bus.
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 75 */
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 76 DECLARE_CLASS_CONSTRUCTOR(SPI) {
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 77 CHECK_ARGUMENT_COUNT(SPI, __constructor, (args_count == 3));
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 78 CHECK_ARGUMENT_TYPE_ALWAYS(SPI, __constructor, 0, number);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 79 CHECK_ARGUMENT_TYPE_ALWAYS(SPI, __constructor, 1, number);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 80 CHECK_ARGUMENT_TYPE_ALWAYS(SPI, __constructor, 2, number);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 81
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 82 int mosi = jerry_get_number_value(args[0]);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 83 int miso = jerry_get_number_value(args[1]);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 84 int sclk = jerry_get_number_value(args[2]);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 85
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 86 SPI *native_ptr = new SPI((PinName)mosi, (PinName)miso, (PinName)sclk);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 87
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 88 jerry_value_t js_object = jerry_create_object();
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 89 jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 90
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 91 /*
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 92 //Future upgrades
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 93 ATTACH_CLASS_FUNCTION(js_object, SPI, frequency);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 94 ATTACH_CLASS_FUNCTION(js_object, SPI, read);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 95 ATTACH_CLASS_FUNCTION(js_object, SPI, write);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 96 ATTACH_CLASS_FUNCTION(js_object, SPI, start);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 97 ATTACH_CLASS_FUNCTION(js_object, SPI, stop);
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 98 */
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 99
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 100 return js_object;
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 101
akhtar.syedzeeshan@gmail.com 0:3a79326cadb9 102 }