We are about to update the us_ticker.c implementation so that we can gain increased granularity for the all timer related classes.master
commit
93df986f58
@ -0,0 +1,117 @@ |
||||
# |
||||
# mbed-2 yotta-compatible build system |
||||
# |
||||
|
||||
# make sure necessary features are enabled: |
||||
project(mbed-classic) |
||||
enable_language(ASM) |
||||
|
||||
# override compilation flags: |
||||
if(CMAKE_C_COMPILER_ID MATCHES GNU) |
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99") |
||||
endif() |
||||
|
||||
# the mbed.a library is built from two sets of source files + include |
||||
# directories: |
||||
# |
||||
# MBED_COMMON_SOURCES: the source files that are the same for all targets, |
||||
# these are easily found by globbing: |
||||
# |
||||
file(GLOB MBED_COMMON_SOURCES "common/*.cpp" "common/*.c") |
||||
# |
||||
# (always include the hal header directory, too) |
||||
set(MBED_COMMON_INCLUDE_DIRS "hal") |
||||
|
||||
# and MBED_TARGET_SOURCES: these depend on which target we are building for. To |
||||
# find these we need to walk the directories in targets/, and wherever we see a |
||||
# TARGET_<something> name, recurse only if <something> matches what we're |
||||
# currently building for |
||||
macro(mbed_find_target_dirs PARENT_DIRECTORY SOURCES_LIST INCLUDES_LIST) |
||||
# append this directory to the search path: |
||||
list(APPEND ${INCLUDES_LIST} "${PARENT_DIRECTORY}") |
||||
# add all source files in this directory to the sources list: |
||||
file(GLOB sources "${PARENT_DIRECTORY}/*.cpp" "${PARENT_DIRECTORY}/*.c" "${PARENT_DIRECTORY}/*.s" "${PARENT_DIRECTORY}/*.S" ) |
||||
list(APPEND ${SOURCES_LIST} ${sources}) |
||||
|
||||
# get a list of all subdirectories that we want to recurse into: |
||||
file(GLOB dir_children RELATIVE "${PARENT_DIRECTORY}" "${PARENT_DIRECTORY}/*") |
||||
set(matching_subdirs "") |
||||
foreach(child ${dir_children}) |
||||
if(IS_DIRECTORY "${PARENT_DIRECTORY}/${child}") |
||||
# is this directory name a magic one? |
||||
if("${child}" MATCHES "^TARGET_") |
||||
# target-magic: recurse if the MBED_LEGACY_TARGET_DEFINITIONS **list** |
||||
# contains a matching value: |
||||
foreach(legacy_magic_def ${MBED_LEGACY_TARGET_DEFINITIONS}) |
||||
# we could probably unroll the list into a single regex if |
||||
# this is a performance problem: |
||||
if("${child}" MATCHES "^TARGET_${legacy_magic_def}$") |
||||
list(APPEND matching_subdirs ${child}) |
||||
break() |
||||
endif() |
||||
endforeach() |
||||
elseif("${child}" MATCHES "^TOOLCHAIN_") |
||||
# toolchain-magic: (recurse if the MBED_LEGACY_TOOLCHAIN matches |
||||
# this name) |
||||
if("${child}" MATCHES "^TOOLCHAIN_${MBED_LEGACY_TOOLCHAIN}$") |
||||
list(APPEND matching_subdirs "${child}") |
||||
endif() |
||||
else() |
||||
# not special: always recurse into this directory |
||||
list(APPEND matching_subdirs "${child}") |
||||
endif() |
||||
endif() |
||||
endforeach() |
||||
#message("matching_subdirs: ${matching_subdirs}") |
||||
|
||||
# recurse: |
||||
foreach(subdir ${matching_subdirs}) |
||||
mbed_find_target_dirs("${PARENT_DIRECTORY}/${subdir}" ${SOURCES_LIST} ${INCLUDES_LIST}) |
||||
endforeach() |
||||
endmacro() |
||||
|
||||
set(MBED_TARGET_SOURCES "") |
||||
set(MBED_TARGET_INCLUDE_DIRS "") |
||||
mbed_find_target_dirs("${CMAKE_CURRENT_SOURCE_DIR}/targets" MBED_TARGET_SOURCES MBED_TARGET_INCLUDE_DIRS) |
||||
#message("found target sources: ${MBED_TARGET_SOURCES}") |
||||
#message("found target include dirs: ${MBED_TARGET_INCLUDE_DIRS}") |
||||
|
||||
# unfortunately, for ARMCC, the startup code needs to be provided as an object |
||||
# on the command line (not as part of an archive). To do this we override the |
||||
# CMake add_executable command. |
||||
if(CMAKE_C_COMPILER_ID STREQUAL "ARMCC") |
||||
set(MBED_TARGET_STARTUP_CODE_SOURCES "") |
||||
foreach(src ${MBED_TARGET_SOURCES}) |
||||
if("${src}" MATCHES .*startup_.*\\.[sS]) |
||||
LIST(APPEND MBED_TARGET_STARTUP_CODE_SOURCES "${src}") |
||||
endif() |
||||
endforeach() |
||||
add_library(mbed_classic_startupcod OBJECT ${MBED_TARGET_STARTUP_CODE_SOURCES}) |
||||
macro (add_executable _name) |
||||
_add_executable(${ARGV} $<TARGET_OBJECTS:mbed_classic_startupcod>) |
||||
endmacro() |
||||
endif() |
||||
|
||||
# we have to append any target-specific include dirs to the global include dirs |
||||
# list, so that any indirect includes (e.g. via mbed.h) of files in those |
||||
# directories will work: |
||||
# (non-target-specific include dirs are listed in extraIncludes in module.json) |
||||
foreach(dir ${MBED_TARGET_INCLUDE_DIRS}) |
||||
set_property(GLOBAL APPEND PROPERTY YOTTA_GLOBAL_INCLUDE_DIRS ${dir}) |
||||
endforeach() |
||||
|
||||
# finally, we can construct a library using the determined set of include paths |
||||
# + source files. Note that the library name must match the name of the yotta |
||||
# module (defined in module.json) for this module to link properly with other |
||||
# yotta modules. |
||||
include_directories(${MBED_COMMON_INCLUDE_DIRS}) |
||||
include_directories(${MBED_TARGET_INCLUDE_DIRS}) |
||||
add_library(mbed-classic |
||||
${MBED_COMMON_SOURCES} |
||||
${MBED_TARGET_SOURCES} |
||||
) |
||||
if(CMAKE_COMPILER_IS_GNUCC) |
||||
target_link_libraries(mbed-classic |
||||
-lnosys |
||||
) |
||||
endif() |
@ -0,0 +1,103 @@ |
||||
/* 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_ANALOGIN_H |
||||
#define MBED_ANALOGIN_H |
||||
|
||||
#include "platform.h" |
||||
|
||||
#if DEVICE_ANALOGIN |
||||
|
||||
#include "analogin_api.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** An analog input, used for reading the voltage on a pin
|
||||
* |
||||
* Example: |
||||
* @code |
||||
* // Print messages when the AnalogIn is greater than 50%
|
||||
* |
||||
* #include "mbed.h" |
||||
* |
||||
* AnalogIn temperature(p20); |
||||
* |
||||
* int main() { |
||||
* while(1) { |
||||
* if(temperature > 0.5) { |
||||
* printf("Too hot! (%f)", temperature.read()); |
||||
* } |
||||
* } |
||||
* } |
||||
* @endcode |
||||
*/ |
||||
class AnalogIn { |
||||
|
||||
public: |
||||
|
||||
/** Create an AnalogIn, connected to the specified pin
|
||||
* |
||||
* @param pin AnalogIn pin to connect to |
||||
* @param name (optional) A string to identify the object |
||||
*/ |
||||
AnalogIn(PinName pin) { |
||||
analogin_init(&_adc, pin); |
||||
} |
||||
|
||||
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
|
||||
* |
||||
* @returns A floating-point value representing the current input voltage, measured as a percentage |
||||
*/ |
||||
float read() { |
||||
return analogin_read(&_adc); |
||||
} |
||||
|
||||
/** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
|
||||
* |
||||
* @returns |
||||
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value |
||||
*/ |
||||
unsigned short read_u16() { |
||||
return analogin_read_u16(&_adc); |
||||
} |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
/** An operator shorthand for read()
|
||||
* |
||||
* The float() operator can be used as a shorthand for read() to simplify common code sequences |
||||
* |
||||
* Example: |
||||
* @code |
||||
* float x = volume.read(); |
||||
* float x = volume; |
||||
* |
||||
* if(volume.read() > 0.25) { ... } |
||||
* if(volume > 0.25) { ... } |
||||
* @endcode |
||||
*/ |
||||
operator float() { |
||||
return read(); |
||||
} |
||||
#endif |
||||
|
||||
protected: |
||||
analogin_t _adc; |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
||||
|
||||
#endif |
@ -0,0 +1,121 @@ |
||||
/* 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_ANALOGOUT_H |
||||
#define MBED_ANALOGOUT_H |
||||
|
||||
#include "platform.h" |
||||
|
||||
#if DEVICE_ANALOGOUT |
||||
|
||||
#include "analogout_api.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** An analog output, used for setting the voltage on a pin
|
||||
* |
||||
* Example: |
||||
* @code |
||||
* // Make a sawtooth output
|
||||
* |
||||
* #include "mbed.h" |
||||
* |
||||
* AnalogOut tri(p18); |
||||
* int main() { |
||||
* while(1) { |
||||
* tri = tri + 0.01; |
||||
* wait_us(1); |
||||
* if(tri == 1) { |
||||
* tri = 0; |
||||
* } |
||||
* } |
||||
* } |
||||
* @endcode |
||||
*/ |
||||
class AnalogOut { |
||||
|
||||
public: |
||||
|
||||
/** Create an AnalogOut connected to the specified pin
|
||||
* |
||||
* @param AnalogOut pin to connect to (18) |
||||
*/ |
||||
AnalogOut(PinName pin) { |
||||
analogout_init(&_dac, pin); |
||||
} |
||||
|
||||
/** Set the output voltage, specified as a percentage (float)
|
||||
* |
||||
* @param value A floating-point value representing the output voltage, |
||||
* specified as a percentage. The value should lie between |
||||
* 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
||||
* Values outside this range will be saturated to 0.0f or 1.0f. |
||||
*/ |
||||
void write(float value) { |
||||
analogout_write(&_dac, value); |
||||
} |
||||
|
||||
/** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
|
||||
* |
||||
* @param value 16-bit unsigned short representing the output voltage, |
||||
* normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) |
||||
*/ |
||||
void write_u16(unsigned short value) { |
||||
analogout_write_u16(&_dac, value); |
||||
} |
||||
|
||||
/** Return the current output voltage setting, measured as a percentage (float)
|
||||
* |
||||
* @returns |
||||
* A floating-point value representing the current voltage being output on the pin, |
||||
* measured as a percentage. The returned value will lie between |
||||
* 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
||||
* |
||||
* @note |
||||
* This value may not match exactly the value set by a previous write(). |
||||
*/ |
||||
float read() { |
||||
return analogout_read(&_dac); |
||||
} |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
/** An operator shorthand for write()
|
||||
*/ |
||||
AnalogOut& operator= (float percent) { |
||||
write(percent); |
||||
return *this; |
||||
} |
||||
|
||||
AnalogOut& operator= (AnalogOut& rhs) { |
||||
write(rhs.read()); |
||||
return *this; |
||||
} |
||||
|
||||
/** An operator shorthand for read()
|
||||
*/ |
||||
operator float() { |
||||
return read(); |
||||
} |
||||
#endif |
||||
|
||||
protected: |
||||
dac_t _dac; |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
||||
|
||||
#endif |
@ -0,0 +1,98 @@ |
||||
/* 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_BUSIN_H |
||||
#define MBED_BUSIN_H |
||||
|
||||
#include "platform.h" |
||||
#include "DigitalIn.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** A digital input bus, used for reading the state of a collection of pins
|
||||
*/ |
||||
class BusIn { |
||||
|
||||
public: |
||||
/* Group: Configuration Methods */ |
||||
|
||||
/** Create an BusIn, connected to the specified pins
|
||||
* |
||||
* @param <n> DigitalIn pin to connect to bus bit <n> (p5-p30, NC) |
||||
* |
||||
* @note |
||||
* It is only required to specify as many pin variables as is required |
||||
* for the bus; the rest will default to NC (not connected) |
||||
*/ |
||||
BusIn(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC, |
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC, |
||||
PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, |
||||
PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); |
||||
|
||||
BusIn(PinName pins[16]); |
||||
|
||||
virtual ~BusIn(); |
||||
|
||||
/** Read the value of the input bus
|
||||
* |
||||
* @returns |
||||
* An integer with each bit corresponding to the value read from the associated DigitalIn pin |
||||
*/ |
||||
int read(); |
||||
|
||||
/** Set the input pin mode
|
||||
* |
||||
* @param mode PullUp, PullDown, PullNone |
||||
*/ |
||||
void mode(PinMode pull); |
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1 |
||||
* |
||||
* @returns |
||||
* Binary mask of connected pins |
||||
*/ |
||||
int mask() { |
||||
return _nc_mask; |
||||
} |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
/** A shorthand for read()
|
||||
*/ |
||||
operator int(); |
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/ |
||||
DigitalIn & operator[] (int index); |
||||
#endif |
||||
|
||||
protected: |
||||
DigitalIn* _pin[16]; |
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected |
||||
* if bit[n] is cleared - pin is not connected (NC) |
||||
*/ |
||||
int _nc_mask; |
||||
|
||||
/* disallow copy constructor and assignment operators */ |
||||
private: |
||||
BusIn(const BusIn&); |
||||
BusIn & operator = (const BusIn&); |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
@ -0,0 +1,117 @@ |
||||
/* 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_BUSINOUT_H |
||||
#define MBED_BUSINOUT_H |
||||
|
||||
#include "DigitalInOut.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** A digital input output bus, used for setting the state of a collection of pins
|
||||
*/ |
||||
class BusInOut { |
||||
|
||||
public: |
||||
|
||||
/** Create an BusInOut, connected to the specified pins
|
||||
* |
||||
* @param p<n> DigitalInOut pin to connect to bus bit p<n> (p5-p30, NC) |
||||
* |
||||
* @note |
||||
* It is only required to specify as many pin variables as is required |
||||
* for the bus; the rest will default to NC (not connected) |
||||
*/ |
||||
BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC, |
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC, |
||||
PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, |
||||
PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); |
||||
|
||||
BusInOut(PinName pins[16]); |
||||
|
||||
virtual ~BusInOut(); |
||||
|
||||
/* Group: Access Methods */ |
||||
|
||||
/** Write the value to the output bus
|
||||
* |
||||
* @param value An integer specifying a bit to write for every corresponding DigitalInOut pin |
||||
*/ |
||||
void write(int value); |
||||
|
||||
/** Read the value currently output on the bus
|
||||
* |
||||
* @returns |
||||
* An integer with each bit corresponding to associated DigitalInOut pin setting |
||||
*/ |
||||
int read(); |
||||
|
||||
/** Set as an output
|
||||
*/ |
||||
void output(); |
||||
|
||||
/** Set as an input
|
||||
*/ |
||||
void input(); |
||||
|
||||
/** Set the input pin mode
|
||||
* |
||||
* @param mode PullUp, PullDown, PullNone |
||||
*/ |
||||
void mode(PinMode pull); |
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1 |
||||
* |
||||
* @returns |
||||
* Binary mask of connected pins |
||||
*/ |
||||
int mask() { |
||||
return _nc_mask; |
||||
} |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
/** A shorthand for write()
|
||||
*/ |
||||
BusInOut& operator= (int v); |
||||
BusInOut& operator= (BusInOut& rhs); |
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/ |
||||
DigitalInOut& operator[] (int index); |
||||
|
||||
/** A shorthand for read()
|
||||
*/ |
||||
operator int(); |
||||
#endif |
||||
|
||||
protected: |
||||
DigitalInOut* _pin[16]; |
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected |
||||
* if bit[n] is cleared - pin is not connected (NC) |
||||
*/ |
||||
int _nc_mask; |
||||
|
||||
/* disallow copy constructor and assignment operators */ |
||||
private: |
||||
BusInOut(const BusInOut&); |
||||
BusInOut & operator = (const BusInOut&); |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
@ -0,0 +1,101 @@ |
||||
/* 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_BUSOUT_H |
||||
#define MBED_BUSOUT_H |
||||
|
||||
#include "DigitalOut.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** A digital output bus, used for setting the state of a collection of pins
|
||||
*/ |
||||
class BusOut { |
||||
|
||||
public: |
||||
|
||||
/** Create an BusOut, connected to the specified pins
|
||||
* |
||||
* @param p<n> DigitalOut pin to connect to bus bit <n> (p5-p30, NC) |
||||
* |
||||
* @note |
||||
* It is only required to specify as many pin variables as is required |
||||
* for the bus; the rest will default to NC (not connected) |
||||
*/ |
||||
BusOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC, |
||||
PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC, |
||||
PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, |
||||
PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); |
||||
|
||||
BusOut(PinName pins[16]); |
||||
|
||||
virtual ~BusOut(); |
||||
|
||||
/** Write the value to the output bus
|
||||
* |
||||
* @param value An integer specifying a bit to write for every corresponding DigitalOut pin |
||||
*/ |
||||
void write(int value); |
||||
|
||||
/** Read the value currently output on the bus
|
||||
* |
||||
* @returns |
||||
* An integer with each bit corresponding to associated DigitalOut pin setting |
||||
*/ |
||||
int read(); |
||||
|
||||
/** Binary mask of bus pins connected to actual pins (not NC pins)
|
||||
* If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1 |
||||
* |
||||
* @returns |
||||
* Binary mask of connected pins |
||||
*/ |
||||
int mask() { |
||||
return _nc_mask; |
||||
} |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
/** A shorthand for write()
|
||||
*/ |
||||
BusOut& operator= (int v); |
||||
BusOut& operator= (BusOut& rhs); |
||||
|
||||
/** Access to particular bit in random-iterator fashion
|
||||
*/ |
||||
DigitalOut& operator[] (int index); |
||||
|
||||
/** A shorthand for read()
|
||||
*/ |
||||
operator int(); |
||||
#endif |
||||
|
||||
protected: |
||||
DigitalOut* _pin[16]; |
||||
|
||||
/** Mask of bus's NC pins
|
||||
* If bit[n] is set to 1 - pin is connected |
||||
* if bit[n] is cleared - pin is not connected (NC) |
||||
*/ |
||||
int _nc_mask; |
||||
|
||||
/* disallow copy constructor and assignment operators */ |
||||
private: |
||||
BusOut(const BusOut&); |
||||
BusOut & operator = (const BusOut&); |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
@ -0,0 +1,243 @@ |
||||
/* 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_CAN_H |
||||
#define MBED_CAN_H |
||||
|
||||
#include "platform.h" |
||||
|
||||
#if DEVICE_CAN |
||||
|
||||
#include "can_api.h" |
||||
#include "can_helper.h" |
||||
#include "FunctionPointer.h" |
||||
|
||||
namespace mbed { |
||||
|
||||
/** CANMessage class
|
||||
*/ |
||||
class CANMessage : public CAN_Message { |
||||
|
||||
public: |
||||
/** Creates empty CAN message.
|
||||
*/ |
||||
CANMessage() : CAN_Message() { |
||||
len = 8; |
||||
type = CANData; |
||||
format = CANStandard; |
||||
id = 0; |
||||
memset(data, 0, 8); |
||||
} |
||||
|
||||
/** Creates CAN message with specific content.
|
||||
*/ |
||||
CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
||||
len = _len & 0xF; |
||||
type = _type; |
||||
format = _format; |
||||
id = _id; |
||||
memcpy(data, _data, _len); |
||||
} |
||||
|
||||
/** Creates CAN remote message.
|
||||
*/ |
||||
CANMessage(int _id, CANFormat _format = CANStandard) { |
||||
len = 0; |
||||
type = CANRemote; |
||||
format = _format; |
||||
id = _id; |
||||
memset(data, 0, 8); |
||||
} |
||||
}; |
||||
|
||||
/** A can bus client, used for communicating with can devices
|
||||
*/ |
||||
class CAN { |
||||
|
||||
public: |
||||
/** Creates an CAN interface connected to specific pins.
|
||||
* |
||||
* @param rd read from transmitter |
||||
* @param td transmit to transmitter |
||||
* |
||||
* Example: |
||||
* @code |
||||
* #include "mbed.h" |
||||
* |
||||
* Ticker ticker; |
||||
* DigitalOut led1(LED1); |
||||
* DigitalOut led2(LED2); |
||||
* CAN can1(p9, p10); |
||||
* CAN can2(p30, p29); |
||||
* |
||||
* char counter = 0; |
||||
* |
||||
* void send() { |
||||
* if(can1.write(CANMessage(1337, &counter, 1))) { |
||||
* printf("Message sent: %d\n", counter); |
||||
* counter++; |
||||
* } |
||||
* led1 = !led1; |
||||
* } |
||||
* |
||||
* int main() { |
||||
* ticker.attach(&send, 1); |
||||
* CANMessage msg; |
||||
* while(1) { |
||||
* if(can2.read(msg)) { |
||||
* printf("Message received: %d\n\n", msg.data[0]); |
||||
* led2 = !led2; |
||||
* } |
||||
* wait(0.2); |
||||
* } |
||||
* } |
||||
* @endcode |
||||
*/ |
||||
CAN(PinName rd, PinName td); |
||||
virtual ~CAN(); |
||||
|
||||
/** Set the frequency of the CAN interface
|
||||
* |
||||
* @param hz The bus frequency in hertz |
||||
* |
||||
* @returns |
||||
* 1 if successful, |
||||
* 0 otherwise |
||||
*/ |
||||
int frequency(int hz); |
||||
|
||||
/** Write a CANMessage to the bus.
|
||||
* |
||||
* @param msg The CANMessage to write. |
||||
* |
||||
* @returns |
||||
* 0 if write failed, |
||||
* 1 if write was successful |
||||
*/ |
||||
int write(CANMessage msg); |
||||
|
||||
/** Read a CANMessage from the bus.
|
||||
* |
||||
* @param msg A CANMessage to read to. |
||||
* @param handle message filter handle (0 for any message) |
||||
* |
||||
* @returns |
||||
* 0 if no message arrived, |
||||
* 1 if message arrived |
||||
*/ |
||||
int read(CANMessage &msg, int handle = 0); |
||||
|
||||
/** Reset CAN interface.
|
||||
* |
||||
* To use after error overflow. |
||||
*/ |
||||
void reset(); |
||||
|
||||
/** Puts or removes the CAN interface into silent monitoring mode
|
||||
* |
||||
* @param silent boolean indicating whether to go into silent mode or not |
||||
*/ |
||||
void monitor(bool silent); |
||||
|
||||
enum Mode { |
||||
Reset = 0, |
||||
Normal, |
||||
Silent, |
||||
LocalTest, |
||||
GlobalTest, |
||||
SilentTest |
||||
}; |
||||
|
||||
/** Change CAN operation to the specified mode
|
||||
* |
||||
* @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) |
||||
* |
||||
* @returns |
||||
* 0 if mode change failed or unsupported, |
||||
* 1 if mode change was successful |
||||
*/ |
||||
int mode(Mode mode); |
||||
|
||||
/** Filter out incomming messages
|
||||
* |
||||
* @param id the id to filter on |
||||
* @param mask the mask applied to the id |
||||
* @param format format to filter on (Default CANAny) |
||||
* @param handle message filter handle (Optional) |
||||
* |
||||
* @returns |
||||
* 0 if filter change failed or unsupported, |
||||
* new filter handle if successful |
||||
*/ |
||||
int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); |
||||
|
||||
/** Returns number of read errors to detect read overflow errors.
|
||||
*/ |
||||
unsigned char rderror(); |
||||
|
||||
/** Returns number of write errors to detect write overflow errors.
|
||||
*/ |
||||
unsigned char tderror(); |
||||
|
||||
enum IrqType { |
||||
RxIrq = 0, |
||||
TxIrq, |
||||
EwIrq, |
||||
DoIrq, |
||||
WuIrq, |
||||
EpIrq, |
||||
AlIrq, |
||||
BeIrq, |
||||
IdIrq |
||||
}; |
||||
|
||||
/** Attach a function to call whenever a CAN frame received interrupt is
|
||||
* generated. |
||||
* |
||||
* @param fptr A pointer to a void function, or 0 to set as none |
||||
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) |
||||
*/ |
||||
void attach(void (*fptr)(void), IrqType type=RxIrq); |
||||
|
||||
/** Attach a member function to call whenever a CAN frame received interrupt
|
||||
* is generated. |
||||
* |
||||
* @param tptr pointer to the object to call the member function on |
||||
* @param mptr pointer to the member function to be called |
||||
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) |
||||
*/ |
||||
template<typename T> |
||||
void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
||||
if((mptr != NULL) && (tptr != NULL)) { |
||||
_irq[type].attach(tptr, mptr); |
||||
can_irq_set(&_can, (CanIrqType)type, 1); |
||||
} |
||||
else { |
||||
can_irq_set(&_can, (CanIrqType)type, 0); |
||||
} |
||||
} |
||||
|
||||
static void _irq_handler(uint32_t id, CanIrqType type); |
||||
|
||||
protected: |
||||
can_t _can; |
||||
FunctionPointer _irq[9]; |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
||||
|
||||
#endif // MBED_CAN_H
|
@ -0,0 +1,202 @@ |
||||
/* General C++ Object Thunking class
|
||||
* |
||||
* - allows direct callbacks to non-static C++ class functions |
||||
* - keeps track for the corresponding class instance |
||||
* - supports an optional context parameter for the called function |
||||
* - ideally suited for class object receiving interrupts (NVIC_SetVector) |
||||
* |
||||
* Copyright (c) 2014-2015 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 __CTHUNK_H__ |
||||
#define __CTHUNK_H__ |
||||
|
||||
#define CTHUNK_ADDRESS 1 |
||||
|
||||
#if defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__thumb2__) |
||||
#define CTHUNK_VARIABLES volatile uint32_t code[1] |
||||
/**
|
||||
* CTHUNK disassembly for Cortex-M3/M4 (thumb2): |
||||
* * ldm.w pc,{r0,r1,r2,pc} |
||||
* |
||||
* This instruction loads the arguments for the static thunking function to r0-r2, and |
||||
* branches to that function by loading its address into PC. |
||||
* |
||||
* This is safe for both regular calling and interrupt calling, since it only touches scratch registers |
||||
* which should be saved by the caller, and are automatically saved as part of the IRQ context switch. |
||||
*/ |
||||
#define CTHUNK_ASSIGMENT m_thunk.code[0] = 0x8007E89F |
||||
|
||||
#elif defined(__CORTEX_M0PLUS) || defined(__CORTEX_M0) |
||||
/*
|
||||
* CTHUNK disassembly for Cortex M0 (thumb): |
||||
* * push {r0,r1,r2,r3,r4,lr} save touched registers and return address |
||||
* * movs r4,#4 set up address to load arguments from (immediately following this code block) (1) |
||||
* * add r4,pc set up address to load arguments from (immediately following this code block) (2) |
||||
* * ldm r4!,{r0,r1,r2,r3} load arguments for static thunk function |
||||
* * blx r3 call static thunk function |
||||
* * pop {r0,r1,r2,r3,r4,pc} restore scratch registers and return from function |
||||
*/ |
||||
#define CTHUNK_VARIABLES volatile uint32_t code[3] |
||||
#define CTHUNK_ASSIGMENT do { \ |
||||
m_thunk.code[0] = 0x2404B51F; \
|
||||
m_thunk.code[1] = 0xCC0F447C; \
|
||||
m_thunk.code[2] = 0xBD1F4798; \
|
||||
} while (0) |
||||
|
||||
#else |
||||
#error "Target is not currently suported." |
||||
#endif |
||||
|
||||
/* IRQ/Exception compatible thunk entry function */ |
||||
typedef void (*CThunkEntry)(void); |
||||
|
||||
template<class T> |
||||
class CThunk |
||||
{ |
||||
public: |
||||
typedef void (T::*CCallbackSimple)(void); |
||||
typedef void (T::*CCallback)(void* context); |
||||
|
||||
inline CThunk(T *instance) |
||||
{ |
||||
init(instance, NULL, NULL); |
||||
} |
||||
|
||||
inline CThunk(T *instance, CCallback callback) |
||||
{ |
||||
init(instance, callback, NULL); |
||||
} |
||||
|
||||
~CThunk() { |
||||
|
||||
} |
||||
|
||||
inline CThunk(T *instance, CCallbackSimple callback) |
||||
{ |
||||
init(instance, (CCallback)callback, NULL); |
||||
} |
||||
|
||||
inline CThunk(T &instance, CCallback callback) |
||||
{ |
||||
init(instance, callback, NULL); |
||||
} |
||||
|
||||
inline CThunk(T &instance, CCallbackSimple callback) |
||||
{ |
||||
init(instance, (CCallback)callback, NULL); |
||||
} |
||||
|
||||
inline CThunk(T &instance, CCallback callback, void* context) |
||||
{ |
||||
init(instance, callback, context); |
||||
} |
||||
|
||||
inline void callback(CCallback callback) |
||||
{ |
||||
m_callback = callback; |
||||
} |
||||
|
||||
inline void callback(CCallbackSimple callback) |
||||
{ |
||||
m_callback = (CCallback)callback; |
||||
} |
||||
|
||||
inline void context(void* context) |
||||
{ |
||||
m_thunk.context = (uint32_t)context; |
||||
} |
||||
|
||||
inline void context(uint32_t context) |
||||
{ |
||||
m_thunk.context = context; |
||||
} |
||||
|
||||
inline uint32_t entry(void) |
||||
{ |
||||
return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS); |
||||
} |
||||
|
||||
/* get thunk entry point for connecting rhunk to an IRQ table */ |
||||
inline operator CThunkEntry(void) |
||||
{ |
||||
return (CThunkEntry)entry(); |
||||
} |
||||
|
||||
/* get thunk entry point for connecting rhunk to an IRQ table */ |
||||
inline operator uint32_t(void) |
||||
{ |
||||
return entry(); |
||||
} |
||||
|
||||
/* simple test function */ |
||||
inline void call(void) |
||||
{ |
||||
(((CThunkEntry)(entry()))()); |
||||
} |
||||
|
||||
private: |
||||
T* m_instance; |
||||
volatile CCallback m_callback; |
||||
|
||||
// TODO: this needs proper fix, to refactor toolchain header file and all its use
|
||||
// PACKED there is not defined properly for IAR
|
||||
#if defined (__ICCARM__) |
||||
typedef __packed struct |
||||
{ |
||||
CTHUNK_VARIABLES; |
||||
volatile uint32_t instance; |
||||
volatile uint32_t context; |
||||
volatile uint32_t callback; |
||||
volatile uint32_t trampoline; |
||||
} CThunkTrampoline; |
||||
#else |
||||
typedef struct |
||||
{ |
||||
CTHUNK_VARIABLES; |
||||
volatile uint32_t instance; |
||||
volatile uint32_t context; |
||||
volatile uint32_t callback; |
||||
volatile uint32_t trampoline; |
||||
} __attribute__((__packed__)) CThunkTrampoline; |
||||
#endif |
||||
|
||||
static void trampoline(T* instance, void* context, CCallback* callback) |
||||
{ |
||||
if(instance && *callback) { |
||||
(static_cast<T*>(instance)->**callback)(context); |
||||
} |
||||
} |
||||
|
||||
volatile CThunkTrampoline m_thunk; |
||||
|
||||
inline void init(T *instance, CCallback callback, void* context) |
||||
{ |
||||
/* remember callback - need to add this level of redirection
|
||||
as pointer size for member functions differs between platforms */ |
||||
m_callback = callback; |
||||
|
||||
/* populate thunking trampoline */ |
||||
CTHUNK_ASSIGMENT; |
||||
m_thunk.context = (uint32_t)context; |
||||
m_thunk.instance = (uint32_t)instance; |
||||
m_thunk.callback = (uint32_t)&m_callback; |
||||
m_thunk.trampoline = (uint32_t)&trampoline; |
||||
|
||||
__ISB(); |
||||
__DSB(); |
||||
} |
||||
}; |
||||
|
||||
#endif/*__CTHUNK_H__*/ |
@ -0,0 +1,181 @@ |
||||
/* 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_CALLCHAIN_H |
||||
#define MBED_CALLCHAIN_H |
||||
|
||||
#include "FunctionPointer.h" |
||||
#include <string.h> |
||||
|
||||
namespace mbed { |
||||
|
||||
/** Group one or more functions in an instance of a CallChain, then call them in
|
||||
* sequence using CallChain::call(). Used mostly by the interrupt chaining code, |
||||
* but can be used for other purposes. |
||||
* |
||||
* Example: |
||||
* @code |
||||
* #include "mbed.h" |
||||
* |
||||
* CallChain chain; |
||||
* |
||||
* void first(void) { |
||||
* printf("'first' function.\n"); |
||||
* } |
||||
* |
||||
* void second(void) { |
||||
* printf("'second' function.\n"); |
||||
* } |
||||
* |
||||
* class Test { |
||||
* public: |
||||
* void f(void) { |
||||
* printf("A::f (class member).\n"); |
||||
* } |
||||
* }; |
||||
* |
||||
* int main() { |
||||
* Test test; |
||||
* |
||||
* chain.add(second); |
||||
* chain.add_front(first); |
||||
* chain.add(&test, &Test::f); |
||||
* chain.call(); |
||||
* } |
||||
* @endcode |
||||
*/ |
||||
|
||||
typedef FunctionPointer* pFunctionPointer_t; |
||||
|
||||
class CallChain { |
||||
public: |
||||
/** Create an empty chain
|
||||
* |
||||
* @param size (optional) Initial size of the chain |
||||
*/ |
||||
CallChain(int size = 4); |
||||
virtual ~CallChain(); |
||||
|
||||
/** Add a function at the end of the chain
|
||||
* |
||||
* @param function A pointer to a void function |
||||
* |
||||
* @returns |
||||
* The function object created for 'function' |
||||
*/ |
||||
pFunctionPointer_t add(void (*function)(void)); |
||||
|
||||
/** Add a function at the end of the chain
|
||||
* |
||||
* @param tptr pointer to the object to call the member function on |
||||
* @param mptr pointer to the member function to be called |
||||
* |
||||
* @returns |
||||
* The function object created for 'tptr' and 'mptr' |
||||
*/ |
||||
template<typename T> |
||||
pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) { |
||||
return common_add(new FunctionPointer(tptr, mptr)); |
||||
} |
||||
|
||||
/** Add a function at the beginning of the chain
|
||||
* |
||||
* @param function A pointer to a void function |
||||
* |
||||
* @returns |
||||
* The function object created for 'function' |
||||
*/ |
||||
pFunctionPointer_t add_front(void (*function)(void)); |
||||
|
||||
/** Add a function at the beginning of the chain
|
||||
* |
||||
* @param tptr pointer to the object to call the member function on |
||||
* @param mptr pointer to the member function to be called |
||||
* |
||||
* @returns |
||||
* The function object created for 'tptr' and 'mptr' |
||||
*/ |
||||
template<typename T> |
||||
pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) { |
||||
return common_add_front(new FunctionPointer(tptr, mptr)); |
||||
} |
||||
|
||||
/** Get the number of functions in the chain
|
||||
*/ |
||||
int size() const; |
||||
|
||||
/** Get a function object from the chain
|
||||
* |
||||
* @param i function object index |
||||
* |
||||
* @returns |
||||
* The function object at position 'i' in the chain |
||||
*/ |
||||
pFunctionPointer_t get(int i) const; |
||||
|
||||
/** Look for a function object in the call chain
|
||||
* |
||||
* @param f the function object to search |
||||
* |
||||
* @returns |
||||
* The index of the function object if found, -1 otherwise. |
||||
*/ |
||||
int find(pFunctionPointer_t f) const; |
||||
|
||||
/** Clear the call chain (remove all functions in the chain).
|
||||
*/ |
||||
void clear(); |
||||
|
||||
/** Remove a function object from the chain
|
||||
* |
||||
* @arg f the function object to remove |
||||
* |
||||
* @returns |
||||
* true if the function object was found and removed, false otherwise. |
||||
*/ |
||||
bool remove(pFunctionPointer_t f); |
||||
|
||||
/** Call all the functions in the chain in sequence
|
||||
*/ |
||||
void call(); |
||||
|
||||
#ifdef MBED_OPERATORS |
||||
void operator ()(void) { |
||||
call(); |
||||
} |
||||
pFunctionPointer_t operator [](int i) const { |
||||
return get(i); |
||||
} |
||||
#endif |
||||
|
||||
private: |
||||
void _check_size(); |
||||
pFunctionPointer_t common_add(pFunctionPointer_t pf); |
||||
pFunctionPointer_t common_add_front(pFunctionPointer_t pf); |
||||
|
||||
pFunctionPointer_t* _chain; |
||||
int _size; |
||||
int _elements; |
||||
|
||||
/* disallow copy constructor and assignment operators */ |
||||
private: |
||||
CallChain(const CallChain&); |
||||
CallChain & operator = (const CallChain&); |
||||
}; |
||||
|
||||
} // namespace mbed
|
||||
|
||||
#endif |
||||
|
@ -0,0 +1,98 @@ |
||||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2015 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_CIRCULARBUFFER_H |
||||
#define MBED_CIRCULARBUFFER_H |
||||
|
||||
namespace mbed { |
||||
|
||||
/** Templated Circular buffer class
|
||||
*/ |
||||
template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
||||
class CircularBuffer { |
||||
public: |
||||
CircularBuffer() : _head(0), _tail(0), _full(false) { |
||||
} |
||||
|
||||
~CircularBuffer() { |
||||
} |
||||
|
||||
/** Push the transaction to the buffer. This overwrites the buffer if it's
|
||||
* full |
||||
* |
||||