ライントレースとうおーるぼっとチャレンジのコードです。 SW1で操作モードとライントレースモードを切り替えます。 操作モードの時にRCBControllerのSTARTボタンを押すとチャレンジが開始します。 チャレンジの時はiPhoneを傾けてハンドリングする操作しか効かなくなっています。

Dependencies:   mbed

Committer:
sibu2
Date:
Tue Nov 18 04:28:02 2014 +0000
Revision:
1:a86c84ead24a
Parent:
0:76dfa9657d9d
adjust wheel rotate direction, wheel encoder signals

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:76dfa9657d9d 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
jksoft 0:76dfa9657d9d 2 *
jksoft 0:76dfa9657d9d 3 * The information contained herein is property of Nordic Semiconductor ASA.
jksoft 0:76dfa9657d9d 4 * Terms and conditions of usage are described in detail in NORDIC
jksoft 0:76dfa9657d9d 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
jksoft 0:76dfa9657d9d 6 *
jksoft 0:76dfa9657d9d 7 * Licensees are granted free, non-transferable use of the information. NO
jksoft 0:76dfa9657d9d 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
jksoft 0:76dfa9657d9d 9 * the file.
jksoft 0:76dfa9657d9d 10 *
jksoft 0:76dfa9657d9d 11 */
jksoft 0:76dfa9657d9d 12
jksoft 0:76dfa9657d9d 13 #include "softdevice_handler.h"
jksoft 0:76dfa9657d9d 14 #include <stdlib.h>
jksoft 0:76dfa9657d9d 15 #include "nordic_common.h"
jksoft 0:76dfa9657d9d 16 #include "app_error.h"
jksoft 0:76dfa9657d9d 17 #include "app_util.h"
jksoft 0:76dfa9657d9d 18 #include "nrf_assert.h"
jksoft 0:76dfa9657d9d 19 #include "nrf_soc.h"
jksoft 0:76dfa9657d9d 20
jksoft 0:76dfa9657d9d 21 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 22 #include "ant_interface.h"
jksoft 0:76dfa9657d9d 23 #elif defined(ANT_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 24 #include "ant_interface.h"
jksoft 0:76dfa9657d9d 25 #elif defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 26 #include "ble.h"
jksoft 0:76dfa9657d9d 27 #endif
jksoft 0:76dfa9657d9d 28
jksoft 0:76dfa9657d9d 29
jksoft 0:76dfa9657d9d 30 static softdevice_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating SoftDevice events to the scheduler. */
jksoft 0:76dfa9657d9d 31
jksoft 0:76dfa9657d9d 32 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 33 // The following two definition is needed only if ANT or BLE events are needed to be pulled from the stack.
jksoft 0:76dfa9657d9d 34 static uint8_t * m_evt_buffer; /**< Buffer for receiving events from the SoftDevice. */
jksoft 0:76dfa9657d9d 35 #endif
jksoft 0:76dfa9657d9d 36
jksoft 0:76dfa9657d9d 37 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 38 static uint16_t m_ble_evt_buffer_size; /**< Size of BLE event buffer. */
jksoft 0:76dfa9657d9d 39 #endif
jksoft 0:76dfa9657d9d 40
jksoft 0:76dfa9657d9d 41 static volatile bool m_softdevice_enabled = false; /**< Variable to indicate whether the SoftDevice is enabled. */
jksoft 0:76dfa9657d9d 42
jksoft 0:76dfa9657d9d 43 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 44 static ble_evt_handler_t m_ble_evt_handler; /**< Application event handler for handling BLE events. */
jksoft 0:76dfa9657d9d 45 #endif
jksoft 0:76dfa9657d9d 46
jksoft 0:76dfa9657d9d 47 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 48 static ant_evt_handler_t m_ant_evt_handler; /**< Application event handler for handling ANT events. */
jksoft 0:76dfa9657d9d 49 #endif
jksoft 0:76dfa9657d9d 50
jksoft 0:76dfa9657d9d 51 static sys_evt_handler_t m_sys_evt_handler; /**< Application event handler for handling System (SOC) events. */
jksoft 0:76dfa9657d9d 52
jksoft 0:76dfa9657d9d 53
jksoft 0:76dfa9657d9d 54 /**@brief Callback function for asserts in the SoftDevice.
jksoft 0:76dfa9657d9d 55 *
jksoft 0:76dfa9657d9d 56 * @details A pointer to this function will be passed to the SoftDevice. This function will be
jksoft 0:76dfa9657d9d 57 * called if an ASSERT statement in the SoftDevice fails.
jksoft 0:76dfa9657d9d 58 *
jksoft 0:76dfa9657d9d 59 * @param[in] pc The value of the program counter when the ASSERT call failed.
jksoft 0:76dfa9657d9d 60 * @param[in] line_num Line number of the failing ASSERT call.
jksoft 0:76dfa9657d9d 61 * @param[in] file_name File name of the failing ASSERT call.
jksoft 0:76dfa9657d9d 62 */
jksoft 0:76dfa9657d9d 63 void softdevice_assertion_handler(uint32_t pc, uint16_t line_num, const uint8_t * file_name)
jksoft 0:76dfa9657d9d 64 {
jksoft 0:76dfa9657d9d 65 UNUSED_PARAMETER(pc);
jksoft 0:76dfa9657d9d 66 assert_nrf_callback(line_num, file_name);
jksoft 0:76dfa9657d9d 67 }
jksoft 0:76dfa9657d9d 68
jksoft 0:76dfa9657d9d 69
jksoft 0:76dfa9657d9d 70 void intern_softdevice_events_execute(void)
jksoft 0:76dfa9657d9d 71 {
jksoft 0:76dfa9657d9d 72 if (!m_softdevice_enabled)
jksoft 0:76dfa9657d9d 73 {
jksoft 0:76dfa9657d9d 74 // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
jksoft 0:76dfa9657d9d 75 // application without using this module's API (i.e softdevice_handler_init)
jksoft 0:76dfa9657d9d 76
jksoft 0:76dfa9657d9d 77 return;
jksoft 0:76dfa9657d9d 78 }
jksoft 0:76dfa9657d9d 79
jksoft 0:76dfa9657d9d 80 bool no_more_soc_evts = (m_sys_evt_handler == NULL);
jksoft 0:76dfa9657d9d 81 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 82 bool no_more_ble_evts = (m_ble_evt_handler == NULL);
jksoft 0:76dfa9657d9d 83 #endif
jksoft 0:76dfa9657d9d 84 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 85 bool no_more_ant_evts = (m_ant_evt_handler == NULL);
jksoft 0:76dfa9657d9d 86 #endif
jksoft 0:76dfa9657d9d 87
jksoft 0:76dfa9657d9d 88 for (;;)
jksoft 0:76dfa9657d9d 89 {
jksoft 0:76dfa9657d9d 90 uint32_t err_code;
jksoft 0:76dfa9657d9d 91
jksoft 0:76dfa9657d9d 92 if (!no_more_soc_evts)
jksoft 0:76dfa9657d9d 93 {
jksoft 0:76dfa9657d9d 94 uint32_t evt_id;
jksoft 0:76dfa9657d9d 95
jksoft 0:76dfa9657d9d 96 // Pull event from SOC.
jksoft 0:76dfa9657d9d 97 err_code = sd_evt_get(&evt_id);
jksoft 0:76dfa9657d9d 98
jksoft 0:76dfa9657d9d 99 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:76dfa9657d9d 100 {
jksoft 0:76dfa9657d9d 101 no_more_soc_evts = true;
jksoft 0:76dfa9657d9d 102 }
jksoft 0:76dfa9657d9d 103 else if (err_code != NRF_SUCCESS)
jksoft 0:76dfa9657d9d 104 {
jksoft 0:76dfa9657d9d 105 APP_ERROR_HANDLER(err_code);
jksoft 0:76dfa9657d9d 106 }
jksoft 0:76dfa9657d9d 107 else
jksoft 0:76dfa9657d9d 108 {
jksoft 0:76dfa9657d9d 109 // Call application's SOC event handler.
jksoft 0:76dfa9657d9d 110 m_sys_evt_handler(evt_id);
jksoft 0:76dfa9657d9d 111 }
jksoft 0:76dfa9657d9d 112 }
jksoft 0:76dfa9657d9d 113
jksoft 0:76dfa9657d9d 114 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 115 // Fetch BLE Events.
jksoft 0:76dfa9657d9d 116 if (!no_more_ble_evts)
jksoft 0:76dfa9657d9d 117 {
jksoft 0:76dfa9657d9d 118 // Pull event from stack
jksoft 0:76dfa9657d9d 119 uint16_t evt_len = m_ble_evt_buffer_size;
jksoft 0:76dfa9657d9d 120
jksoft 0:76dfa9657d9d 121 err_code = sd_ble_evt_get(m_evt_buffer, &evt_len);
jksoft 0:76dfa9657d9d 122 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:76dfa9657d9d 123 {
jksoft 0:76dfa9657d9d 124 no_more_ble_evts = true;
jksoft 0:76dfa9657d9d 125 }
jksoft 0:76dfa9657d9d 126 else if (err_code != NRF_SUCCESS)
jksoft 0:76dfa9657d9d 127 {
jksoft 0:76dfa9657d9d 128 APP_ERROR_HANDLER(err_code);
jksoft 0:76dfa9657d9d 129 }
jksoft 0:76dfa9657d9d 130 else
jksoft 0:76dfa9657d9d 131 {
jksoft 0:76dfa9657d9d 132 // Call application's BLE stack event handler.
jksoft 0:76dfa9657d9d 133 m_ble_evt_handler((ble_evt_t *)m_evt_buffer);
jksoft 0:76dfa9657d9d 134 }
jksoft 0:76dfa9657d9d 135 }
jksoft 0:76dfa9657d9d 136 #endif
jksoft 0:76dfa9657d9d 137
jksoft 0:76dfa9657d9d 138 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 139 // Fetch ANT Events.
jksoft 0:76dfa9657d9d 140 if (!no_more_ant_evts)
jksoft 0:76dfa9657d9d 141 {
jksoft 0:76dfa9657d9d 142 // Pull event from stack
jksoft 0:76dfa9657d9d 143 err_code = sd_ant_event_get(&((ant_evt_t *)m_evt_buffer)->channel,
jksoft 0:76dfa9657d9d 144 &((ant_evt_t *)m_evt_buffer)->event,
jksoft 0:76dfa9657d9d 145 ((ant_evt_t *)m_evt_buffer)->evt_buffer);
jksoft 0:76dfa9657d9d 146 if (err_code == NRF_ERROR_NOT_FOUND)
jksoft 0:76dfa9657d9d 147 {
jksoft 0:76dfa9657d9d 148 no_more_ant_evts = true;
jksoft 0:76dfa9657d9d 149 }
jksoft 0:76dfa9657d9d 150 else if (err_code != NRF_SUCCESS)
jksoft 0:76dfa9657d9d 151 {
jksoft 0:76dfa9657d9d 152 APP_ERROR_HANDLER(err_code);
jksoft 0:76dfa9657d9d 153 }
jksoft 0:76dfa9657d9d 154 else
jksoft 0:76dfa9657d9d 155 {
jksoft 0:76dfa9657d9d 156 // Call application's ANT stack event handler.
jksoft 0:76dfa9657d9d 157 m_ant_evt_handler((ant_evt_t *)m_evt_buffer);
jksoft 0:76dfa9657d9d 158 }
jksoft 0:76dfa9657d9d 159 }
jksoft 0:76dfa9657d9d 160 #endif
jksoft 0:76dfa9657d9d 161
jksoft 0:76dfa9657d9d 162 if (no_more_soc_evts)
jksoft 0:76dfa9657d9d 163 {
jksoft 0:76dfa9657d9d 164 // There are no remaining System (SOC) events to be fetched from the SoftDevice.
jksoft 0:76dfa9657d9d 165 #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 166 // Check if there are any remaining BLE and ANT events.
jksoft 0:76dfa9657d9d 167 if (no_more_ble_evts && no_more_ant_evts)
jksoft 0:76dfa9657d9d 168 {
jksoft 0:76dfa9657d9d 169 break;
jksoft 0:76dfa9657d9d 170 }
jksoft 0:76dfa9657d9d 171 #elif defined(BLE_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 172 // Check if there are any remaining BLE events.
jksoft 0:76dfa9657d9d 173 if (no_more_ble_evts)
jksoft 0:76dfa9657d9d 174 {
jksoft 0:76dfa9657d9d 175 break;
jksoft 0:76dfa9657d9d 176 }
jksoft 0:76dfa9657d9d 177 #elif defined(ANT_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 178 // Check if there are any remaining ANT events.
jksoft 0:76dfa9657d9d 179 if (no_more_ant_evts)
jksoft 0:76dfa9657d9d 180 {
jksoft 0:76dfa9657d9d 181 break;
jksoft 0:76dfa9657d9d 182 }
jksoft 0:76dfa9657d9d 183 #else
jksoft 0:76dfa9657d9d 184 // No need to check for BLE or ANT events since there is no support for BLE and ANT
jksoft 0:76dfa9657d9d 185 // required.
jksoft 0:76dfa9657d9d 186 break;
jksoft 0:76dfa9657d9d 187 #endif
jksoft 0:76dfa9657d9d 188 }
jksoft 0:76dfa9657d9d 189 }
jksoft 0:76dfa9657d9d 190 }
jksoft 0:76dfa9657d9d 191
jksoft 0:76dfa9657d9d 192
jksoft 0:76dfa9657d9d 193 uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t clock_source,
jksoft 0:76dfa9657d9d 194 void * p_evt_buffer,
jksoft 0:76dfa9657d9d 195 uint16_t evt_buffer_size,
jksoft 0:76dfa9657d9d 196 softdevice_evt_schedule_func_t evt_schedule_func)
jksoft 0:76dfa9657d9d 197 {
jksoft 0:76dfa9657d9d 198 uint32_t err_code;
jksoft 0:76dfa9657d9d 199
jksoft 0:76dfa9657d9d 200 // Save configuration.
jksoft 0:76dfa9657d9d 201 #if defined (BLE_STACK_SUPPORT_REQD) || defined (ANT_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 202 // Check that buffer is not NULL.
jksoft 0:76dfa9657d9d 203 if (p_evt_buffer == NULL)
jksoft 0:76dfa9657d9d 204 {
jksoft 0:76dfa9657d9d 205 return NRF_ERROR_INVALID_PARAM;
jksoft 0:76dfa9657d9d 206 }
jksoft 0:76dfa9657d9d 207
jksoft 0:76dfa9657d9d 208 // Check that buffer is correctly aligned.
jksoft 0:76dfa9657d9d 209 if (!is_word_aligned(p_evt_buffer))
jksoft 0:76dfa9657d9d 210 {
jksoft 0:76dfa9657d9d 211 return NRF_ERROR_INVALID_PARAM;
jksoft 0:76dfa9657d9d 212 }
jksoft 0:76dfa9657d9d 213
jksoft 0:76dfa9657d9d 214 m_evt_buffer = (uint8_t *)p_evt_buffer;
jksoft 0:76dfa9657d9d 215 #else
jksoft 0:76dfa9657d9d 216 // The variable p_evt_buffer is not needed if neither BLE Stack nor ANT stack support is
jksoft 0:76dfa9657d9d 217 // required.
jksoft 0:76dfa9657d9d 218 UNUSED_PARAMETER(p_evt_buffer);
jksoft 0:76dfa9657d9d 219 #endif
jksoft 0:76dfa9657d9d 220
jksoft 0:76dfa9657d9d 221 #if defined (BLE_STACK_SUPPORT_REQD)
jksoft 0:76dfa9657d9d 222 m_ble_evt_buffer_size = evt_buffer_size;
jksoft 0:76dfa9657d9d 223 #else
jksoft 0:76dfa9657d9d 224 // The variable evt_buffer_size is not needed if BLE Stack support is NOT required.
jksoft 0:76dfa9657d9d 225 UNUSED_PARAMETER(evt_buffer_size);
jksoft 0:76dfa9657d9d 226 #endif
jksoft 0:76dfa9657d9d 227
jksoft 0:76dfa9657d9d 228 m_evt_schedule_func = evt_schedule_func;
jksoft 0:76dfa9657d9d 229
jksoft 0:76dfa9657d9d 230 // Initialize SoftDevice.
jksoft 0:76dfa9657d9d 231
jksoft 0:76dfa9657d9d 232 err_code = sd_softdevice_enable(clock_source, softdevice_assertion_handler);
jksoft 0:76dfa9657d9d 233 if (err_code != NRF_SUCCESS)
jksoft 0:76dfa9657d9d 234 {
jksoft 0:76dfa9657d9d 235 return err_code;
jksoft 0:76dfa9657d9d 236 }
jksoft 0:76dfa9657d9d 237
jksoft 0:76dfa9657d9d 238 m_softdevice_enabled = true;
jksoft 0:76dfa9657d9d 239
jksoft 0:76dfa9657d9d 240 // Enable BLE event interrupt (interrupt priority has already been set by the stack).
jksoft 0:76dfa9657d9d 241 return sd_nvic_EnableIRQ(SWI2_IRQn);
jksoft 0:76dfa9657d9d 242 }
jksoft 0:76dfa9657d9d 243
jksoft 0:76dfa9657d9d 244
jksoft 0:76dfa9657d9d 245 uint32_t softdevice_handler_sd_disable(void)
jksoft 0:76dfa9657d9d 246 {
jksoft 0:76dfa9657d9d 247 uint32_t err_code = sd_softdevice_disable();
jksoft 0:76dfa9657d9d 248
jksoft 0:76dfa9657d9d 249 m_softdevice_enabled = !(err_code == NRF_SUCCESS);
jksoft 0:76dfa9657d9d 250
jksoft 0:76dfa9657d9d 251 return err_code;
jksoft 0:76dfa9657d9d 252 }
jksoft 0:76dfa9657d9d 253
jksoft 0:76dfa9657d9d 254
jksoft 0:76dfa9657d9d 255 #ifdef BLE_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 256 uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler)
jksoft 0:76dfa9657d9d 257 {
jksoft 0:76dfa9657d9d 258 if (ble_evt_handler == NULL)
jksoft 0:76dfa9657d9d 259 {
jksoft 0:76dfa9657d9d 260 return NRF_ERROR_NULL;
jksoft 0:76dfa9657d9d 261 }
jksoft 0:76dfa9657d9d 262
jksoft 0:76dfa9657d9d 263 m_ble_evt_handler = ble_evt_handler;
jksoft 0:76dfa9657d9d 264
jksoft 0:76dfa9657d9d 265 return NRF_SUCCESS;
jksoft 0:76dfa9657d9d 266 }
jksoft 0:76dfa9657d9d 267 #endif
jksoft 0:76dfa9657d9d 268
jksoft 0:76dfa9657d9d 269
jksoft 0:76dfa9657d9d 270 #ifdef ANT_STACK_SUPPORT_REQD
jksoft 0:76dfa9657d9d 271 uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler)
jksoft 0:76dfa9657d9d 272 {
jksoft 0:76dfa9657d9d 273 if (ant_evt_handler == NULL)
jksoft 0:76dfa9657d9d 274 {
jksoft 0:76dfa9657d9d 275 return NRF_ERROR_NULL;
jksoft 0:76dfa9657d9d 276 }
jksoft 0:76dfa9657d9d 277
jksoft 0:76dfa9657d9d 278 m_ant_evt_handler = ant_evt_handler;
jksoft 0:76dfa9657d9d 279
jksoft 0:76dfa9657d9d 280 return NRF_SUCCESS;
jksoft 0:76dfa9657d9d 281 }
jksoft 0:76dfa9657d9d 282 #endif
jksoft 0:76dfa9657d9d 283
jksoft 0:76dfa9657d9d 284
jksoft 0:76dfa9657d9d 285 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
jksoft 0:76dfa9657d9d 286 {
jksoft 0:76dfa9657d9d 287 if (sys_evt_handler == NULL)
jksoft 0:76dfa9657d9d 288 {
jksoft 0:76dfa9657d9d 289 return NRF_ERROR_NULL;
jksoft 0:76dfa9657d9d 290 }
jksoft 0:76dfa9657d9d 291
jksoft 0:76dfa9657d9d 292 m_sys_evt_handler = sys_evt_handler;
jksoft 0:76dfa9657d9d 293
jksoft 0:76dfa9657d9d 294 return NRF_SUCCESS;
jksoft 0:76dfa9657d9d 295 }
jksoft 0:76dfa9657d9d 296
jksoft 0:76dfa9657d9d 297
jksoft 0:76dfa9657d9d 298 /**@brief Function for handling the Application's BLE Stack events interrupt.
jksoft 0:76dfa9657d9d 299 *
jksoft 0:76dfa9657d9d 300 * @details This function is called whenever an event is ready to be pulled.
jksoft 0:76dfa9657d9d 301 */
jksoft 0:76dfa9657d9d 302 extern "C" void SWI2_IRQHandler(void)
jksoft 0:76dfa9657d9d 303 {
jksoft 0:76dfa9657d9d 304 if (m_evt_schedule_func != NULL)
jksoft 0:76dfa9657d9d 305 {
jksoft 0:76dfa9657d9d 306 uint32_t err_code = m_evt_schedule_func();
jksoft 0:76dfa9657d9d 307 APP_ERROR_CHECK(err_code);
jksoft 0:76dfa9657d9d 308 }
jksoft 0:76dfa9657d9d 309 else
jksoft 0:76dfa9657d9d 310 {
jksoft 0:76dfa9657d9d 311 intern_softdevice_events_execute();
jksoft 0:76dfa9657d9d 312 }
jksoft 0:76dfa9657d9d 313 }