mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Jul 17 09:15:10 2015 +0100
Revision:
592:a274ee790e56
Parent:
579:53297373a894
Synchronized with git revision e7144f83a8d75df80c4877936b6ffe552b0be9e6

Full URL: https://github.com/mbedmicro/mbed/commit/e7144f83a8d75df80c4877936b6ffe552b0be9e6/

More API implementation for SAMR21

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 579:53297373a894 1 #include "extint.h"
mbed_official 579:53297373a894 2 #include "extint_callback.h"
mbed_official 579:53297373a894 3
mbed_official 579:53297373a894 4 /**
mbed_official 579:53297373a894 5 * \internal
mbed_official 579:53297373a894 6 * Internal driver device instance struct, declared in the main module driver.
mbed_official 579:53297373a894 7 */
mbed_official 579:53297373a894 8 extern struct _extint_module _extint_dev;
mbed_official 579:53297373a894 9
mbed_official 579:53297373a894 10 /**
mbed_official 579:53297373a894 11 * \internal
mbed_official 579:53297373a894 12 * This is the number of the channel whose callback is currently running
mbed_official 579:53297373a894 13 */
mbed_official 579:53297373a894 14 uint8_t _current_channel;
mbed_official 579:53297373a894 15
mbed_official 579:53297373a894 16 /**
mbed_official 579:53297373a894 17 * \brief Registers an asynchronous callback function with the driver.
mbed_official 579:53297373a894 18 *
mbed_official 579:53297373a894 19 * Registers an asynchronous callback with the EXTINT driver, fired when a
mbed_official 579:53297373a894 20 * channel detects the configured channel detection criteria
mbed_official 579:53297373a894 21 * (e.g. edge or level). Callbacks are fired once for each detected channel.
mbed_official 579:53297373a894 22 *
mbed_official 579:53297373a894 23 * \note NMI channel callbacks cannot be registered via this function; the
mbed_official 579:53297373a894 24 * device's NMI interrupt should be hooked directly in the user
mbed_official 579:53297373a894 25 * application and the NMI flags manually cleared via
mbed_official 579:53297373a894 26 * \ref extint_nmi_clear_detected().
mbed_official 579:53297373a894 27 *
mbed_official 579:53297373a894 28 * \param[in] callback Pointer to the callback function to register
mbed_official 579:53297373a894 29 * \param[in] channel Logical channel to register callback for
mbed_official 579:53297373a894 30 * \param[in] type Type of callback function to register
mbed_official 579:53297373a894 31 *
mbed_official 579:53297373a894 32 * \return Status of the registration operation.
mbed_official 579:53297373a894 33 * \retval STATUS_OK The callback was registered successfully
mbed_official 579:53297373a894 34 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
mbed_official 579:53297373a894 35 * \retval STATUS_ERR_ALREADY_INITIALIZED Callback function has been
mbed_official 579:53297373a894 36 * registered, need unregister first
mbed_official 579:53297373a894 37 */
mbed_official 579:53297373a894 38 enum status_code extint_register_callback(
mbed_official 579:53297373a894 39 const extint_callback_t callback,
mbed_official 579:53297373a894 40 const uint8_t channel,
mbed_official 579:53297373a894 41 const enum extint_callback_type type)
mbed_official 579:53297373a894 42 {
mbed_official 579:53297373a894 43 /* Sanity check arguments */
mbed_official 579:53297373a894 44 Assert(callback);
mbed_official 579:53297373a894 45
mbed_official 579:53297373a894 46 if (type != EXTINT_CALLBACK_TYPE_DETECT) {
mbed_official 579:53297373a894 47 Assert(false);
mbed_official 579:53297373a894 48 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 49 }
mbed_official 579:53297373a894 50
mbed_official 579:53297373a894 51 if (_extint_dev.callbacks[channel] == NULL) {
mbed_official 579:53297373a894 52 _extint_dev.callbacks[channel] = callback;
mbed_official 579:53297373a894 53 return STATUS_OK;
mbed_official 579:53297373a894 54 } else if (_extint_dev.callbacks[channel] == callback) {
mbed_official 579:53297373a894 55 return STATUS_OK;
mbed_official 579:53297373a894 56 }
mbed_official 579:53297373a894 57
mbed_official 579:53297373a894 58 return STATUS_ERR_ALREADY_INITIALIZED;
mbed_official 579:53297373a894 59 }
mbed_official 579:53297373a894 60
mbed_official 579:53297373a894 61 /**
mbed_official 579:53297373a894 62 * \brief Unregisters an asynchronous callback function with the driver.
mbed_official 579:53297373a894 63 *
mbed_official 579:53297373a894 64 * Unregisters an asynchronous callback with the EXTINT driver, removing it
mbed_official 579:53297373a894 65 * from the internal callback registration table.
mbed_official 579:53297373a894 66 *
mbed_official 579:53297373a894 67 * \param[in] callback Pointer to the callback function to unregister
mbed_official 579:53297373a894 68 * \param[in] channel Logical channel to unregister callback for
mbed_official 579:53297373a894 69 * \param[in] type Type of callback function to unregister
mbed_official 579:53297373a894 70 *
mbed_official 579:53297373a894 71 * \return Status of the de-registration operation.
mbed_official 579:53297373a894 72 * \retval STATUS_OK The callback was Unregistered successfully
mbed_official 579:53297373a894 73 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
mbed_official 579:53297373a894 74 * \retval STATUS_ERR_BAD_ADDRESS No matching entry was found in the
mbed_official 579:53297373a894 75 * registration table
mbed_official 579:53297373a894 76 */
mbed_official 579:53297373a894 77 enum status_code extint_unregister_callback(
mbed_official 579:53297373a894 78 const extint_callback_t callback,
mbed_official 579:53297373a894 79 const uint8_t channel,
mbed_official 579:53297373a894 80 const enum extint_callback_type type)
mbed_official 579:53297373a894 81 {
mbed_official 579:53297373a894 82 /* Sanity check arguments */
mbed_official 579:53297373a894 83 Assert(callback);
mbed_official 579:53297373a894 84
mbed_official 579:53297373a894 85 if (type != EXTINT_CALLBACK_TYPE_DETECT) {
mbed_official 579:53297373a894 86 Assert(false);
mbed_official 579:53297373a894 87 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 88 }
mbed_official 579:53297373a894 89
mbed_official 579:53297373a894 90 if (_extint_dev.callbacks[channel] == callback) {
mbed_official 579:53297373a894 91 _extint_dev.callbacks[channel] = NULL;
mbed_official 579:53297373a894 92 return STATUS_OK;
mbed_official 579:53297373a894 93 }
mbed_official 579:53297373a894 94
mbed_official 579:53297373a894 95 return STATUS_ERR_BAD_ADDRESS;
mbed_official 579:53297373a894 96 }
mbed_official 579:53297373a894 97
mbed_official 579:53297373a894 98 /**
mbed_official 579:53297373a894 99 * \brief Enables asynchronous callback generation for a given channel and type.
mbed_official 579:53297373a894 100 *
mbed_official 579:53297373a894 101 * Enables asynchronous callbacks for a given logical external interrupt channel
mbed_official 579:53297373a894 102 * and type. This must be called before an external interrupt channel will
mbed_official 579:53297373a894 103 * generate callback events.
mbed_official 579:53297373a894 104 *
mbed_official 579:53297373a894 105 * \param[in] channel Logical channel to enable callback generation for
mbed_official 579:53297373a894 106 * \param[in] type Type of callback function callbacks to enable
mbed_official 579:53297373a894 107 *
mbed_official 579:53297373a894 108 * \return Status of the callback enable operation.
mbed_official 579:53297373a894 109 * \retval STATUS_OK The callback was enabled successfully
mbed_official 579:53297373a894 110 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
mbed_official 579:53297373a894 111 */
mbed_official 579:53297373a894 112 enum status_code extint_chan_enable_callback(
mbed_official 579:53297373a894 113 const uint8_t channel,
mbed_official 579:53297373a894 114 const enum extint_callback_type type)
mbed_official 579:53297373a894 115 {
mbed_official 579:53297373a894 116 if (type == EXTINT_CALLBACK_TYPE_DETECT) {
mbed_official 579:53297373a894 117 Eic *const eic = _extint_get_eic_from_channel(channel);
mbed_official 579:53297373a894 118
mbed_official 579:53297373a894 119 eic->INTENSET.reg = (1UL << channel);
mbed_official 579:53297373a894 120 } else {
mbed_official 579:53297373a894 121 Assert(false);
mbed_official 579:53297373a894 122 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 123 }
mbed_official 579:53297373a894 124
mbed_official 579:53297373a894 125 return STATUS_OK;
mbed_official 579:53297373a894 126 }
mbed_official 579:53297373a894 127
mbed_official 579:53297373a894 128 /**
mbed_official 579:53297373a894 129 * \brief Disables asynchronous callback generation for a given channel and type.
mbed_official 579:53297373a894 130 *
mbed_official 579:53297373a894 131 * Disables asynchronous callbacks for a given logical external interrupt
mbed_official 579:53297373a894 132 * channel and type.
mbed_official 579:53297373a894 133 *
mbed_official 579:53297373a894 134 * \param[in] channel Logical channel to disable callback generation for
mbed_official 579:53297373a894 135 * \param[in] type Type of callback function callbacks to disable
mbed_official 579:53297373a894 136 *
mbed_official 579:53297373a894 137 * \return Status of the callback disable operation.
mbed_official 579:53297373a894 138 * \retval STATUS_OK The callback was disabled successfully
mbed_official 579:53297373a894 139 * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
mbed_official 579:53297373a894 140 */
mbed_official 579:53297373a894 141 enum status_code extint_chan_disable_callback(
mbed_official 579:53297373a894 142 const uint8_t channel,
mbed_official 579:53297373a894 143 const enum extint_callback_type type)
mbed_official 579:53297373a894 144 {
mbed_official 579:53297373a894 145 if (type == EXTINT_CALLBACK_TYPE_DETECT) {
mbed_official 579:53297373a894 146 Eic *const eic = _extint_get_eic_from_channel(channel);
mbed_official 579:53297373a894 147
mbed_official 579:53297373a894 148 eic->INTENCLR.reg = (1UL << channel);
mbed_official 579:53297373a894 149 } else {
mbed_official 579:53297373a894 150 Assert(false);
mbed_official 579:53297373a894 151 return STATUS_ERR_INVALID_ARG;
mbed_official 579:53297373a894 152 }
mbed_official 579:53297373a894 153
mbed_official 579:53297373a894 154 return STATUS_OK;
mbed_official 579:53297373a894 155 }
mbed_official 579:53297373a894 156
mbed_official 579:53297373a894 157 /**
mbed_official 579:53297373a894 158 * \brief Find what channel caused the callback.
mbed_official 579:53297373a894 159 *
mbed_official 579:53297373a894 160 * Can be used in an EXTINT callback function to find what channel caused
mbed_official 579:53297373a894 161 * the callback in case same callback is used by multiple channels.
mbed_official 579:53297373a894 162 *
mbed_official 579:53297373a894 163 * \return Channel number.
mbed_official 579:53297373a894 164 */
mbed_official 579:53297373a894 165 uint8_t extint_get_current_channel(void)
mbed_official 579:53297373a894 166 {
mbed_official 579:53297373a894 167 return _current_channel;
mbed_official 579:53297373a894 168 }
mbed_official 579:53297373a894 169
mbed_official 579:53297373a894 170 /** Handler for the EXTINT hardware module interrupt. */
mbed_official 579:53297373a894 171 void EIC_Handler(void)
mbed_official 579:53297373a894 172 {
mbed_official 579:53297373a894 173 /* Find any triggered channels, run associated callback handlers */
mbed_official 579:53297373a894 174 for (_current_channel = 0; _current_channel < EIC_NUMBER_OF_INTERRUPTS ; _current_channel++) {
mbed_official 579:53297373a894 175 if (extint_chan_is_detected(_current_channel)) {
mbed_official 579:53297373a894 176 /* Clear flag */
mbed_official 579:53297373a894 177 extint_chan_clear_detected(_current_channel);
mbed_official 579:53297373a894 178 /* Find any associated callback entries in the callback table */
mbed_official 579:53297373a894 179 if (_extint_dev.callbacks[_current_channel] != NULL) {
mbed_official 579:53297373a894 180 /* Run the registered callback */
mbed_official 579:53297373a894 181 _extint_dev.callbacks[_current_channel]();
mbed_official 579:53297373a894 182 }
mbed_official 579:53297373a894 183 }
mbed_official 579:53297373a894 184 }
mbed_official 579:53297373a894 185 }