芯科BG22学习笔记:10-如何实现RTT log打印
实验目的:log日志记录对开发嵌入式产品非常重要。尤其对于基于连接的无线产品。因为使用断点可能会导致连接中断,而通过浏览log日志可以很容易地解决问题。添加RTT组件并修改相关的.c和.h文件实现RTT log打印,通过Jlink工具J-Link RTT Viewer V6.86d实现log的显示。
实验环境:Simplicity Studio V5 / GSDK4.4.3
实验器材:Thunderboard BRD4184A
实验开始:
1. 新建工程,蓝牙工程输入soc empty, 普通MCU工程输入empty c
2. 双击SLCP文件,在Soft Components安装第三方的两个RTT组件
- Third Party → SEGGER → RTT → SEGGER RTT
- Third Party → SEGGER→ RTT→ SEGGER RTT printf
3. 添加log.h文件
1 #ifndef LOG_H 2 #define LOG_H 3 4 #include "SEGGER_RTT.h" 5 6 #if (LOCAL_LOG_OFF == 1) 7 #define GK_LOGD(_tag_, _prefix_, ...) 8 #define GK_LOGI(_tag_, _prefix_, ...) 9 #define GK_LOGW(_tag_, _prefix_, ...) 10 #define GK_LOGE(_tag_, _prefix_, ...) 11 #define GK_LOGV(_tag_, _prefix_, ...) 12 #define GK_UINT8_ARRAY_DUMP(array_base, array_size) 13 #define GK_ADDRESSING() 14 #define GK_CHECK(tag__, x) 15 #define LOG_ASSERT(x) 16 #define LOG(...) 17 #define LOGN() 18 #define UINT8_ARRAY_DUMP(array_base, array_size) 19 #define LOG_DIRECT_ERR(_prefix_, ...) 20 #define LOGE(_prefix_, ...) 21 #define LOGW(_prefix_, ...) 22 #define LOGI(_prefix_, ...) 23 #define LOGD(_prefix_, ...) 24 #define LOGV(_prefix_, ...) 25 #define ERROR_ADDRESSING() 26 #define INIT_LOG() 27 #define EVT_LOG_C(_evt_name_, _attached_, ...) 28 #define EVT_LOG_I(_evt_name_, _attached_, ...) 29 #define EVT_LOG_V(_evt_name_, _attached_, ...) 30 #define SE_CALL(x) \ 31 do { \ 32 x; \ 33 } while (0) 34 #define GENERAL_ERR_CHECK(x) 35 #else 36 37 /* General part - High level definitions */ 38 #ifndef LOG_LEVEL 39 #define LOG_LEVEL LVL_VERBOSE 40 #endif 41 42 #define NO_LOG 0 43 #define LVL_ERROR 1 44 #define LVL_WARNING 2 45 #define LVL_INFO 3 46 #define LVL_DEBUG 4 47 #define LVL_VERBOSE 5 48 49 #ifndef GENERAL_SUCCESS 50 #define GENERAL_SUCCESS 0 51 #endif 52 53 // The expected format of the logging message is 54 // [LOGGING_LEVEL]<MODULE_NAME>: xxxx... 55 #define ASSERT_FLAG "[ASSERT] " 56 #define ERROR_FLAG "[E] " 57 #define WARNING_FLAG "[W] " 58 #define INFO_FLAG "[I] " 59 #define DEBUG_FLAG "[D] " 60 #define VERBOSE_FLAG "[V] " 61 62 #ifndef SUB_MODULE_NAME 63 #define SUB_MODULE_NAME "" 64 #endif 65 66 #define END_OF_LOG_HEADER ":> " 67 68 #define LOG_ASSERT_PREFIX \ 69 RTT_CTRL_TEXT_BRIGHT_RED ASSERT_FLAG "[Assert-assert] " \ 70 SUB_MODULE_NAME END_OF_LOG_HEADER 71 #define LOG_ERROR_PREFIX \ 72 RTT_CTRL_TEXT_BRIGHT_RED ERROR_FLAG \ 73 SUB_MODULE_NAME END_OF_LOG_HEADER 74 #define LOG_WARNING_PREFIX \ 75 RTT_CTRL_TEXT_BRIGHT_YELLOW WARNING_FLAG \ 76 SUB_MODULE_NAME END_OF_LOG_HEADER 77 #define LOG_INFO_PREFIX \ 78 RTT_CTRL_TEXT_BRIGHT_CYAN INFO_FLAG \ 79 SUB_MODULE_NAME END_OF_LOG_HEADER 80 #define LOG_DEBUG_PREFIX \ 81 RTT_CTRL_TEXT_BRIGHT_GREEN DEBUG_FLAG \ 82 SUB_MODULE_NAME END_OF_LOG_HEADER 83 #define LOG_VERBOSE_PREFIX \ 84 RTT_CTRL_RESET VERBOSE_FLAG \ 85 SUB_MODULE_NAME END_OF_LOG_HEADER 86 87 #define LOG(...) SEGGER_RTT_printf(0, __VA_ARGS__) 88 89 #define LOG_ASSERT_MSG() \ 90 do { \ 91 LOG(LOG_ASSERT_PREFIX "ASSERT ERROR at %s:%d\n", __FILE__, __LINE__); \ 92 } while (0) 93 94 #define LOG_ASSERT(x) \ 95 do { \ 96 if (!x) { LOG_ASSERT_MSG(); } \ 97 } while (0) 98 99 #define UINT8_ARRAY_DUMP(array_base, array_size) \ 100 do { \ 101 for (int i_log_exlusive = 0; i_log_exlusive < (array_size); \ 102 i_log_exlusive++) { \ 103 LOG((i_log_exlusive + 1) % 16 ? "%02X " : "%02X\n", \ 104 ((char *)(array_base))[i_log_exlusive]); \ 105 } \ 106 LOG("\n"); \ 107 } while (0) 108 109 #define LOGN() \ 110 do { \ 111 LOG("\r\n"); \ 112 } while (0) 113 114 /* Direct way to output Error message */ 115 #define LOG_DIRECT_ERR(_prefix_, ...) \ 116 do { \ 117 LOG(RTT_CTRL_TEXT_BRIGHT_RED _prefix_, ## __VA_ARGS__); \ 118 } while (0) 119 120 /** 121 * Different level log system 122 */ 123 124 /* Error */ 125 #define LOGE(_prefix_, ...) \ 126 do { \ 127 if (LOG_LEVEL >= LVL_ERROR) { \ 128 LOG(LOG_ERROR_PREFIX _prefix_, ## __VA_ARGS__); \ 129 } \ 130 } while (0) 131 132 /* Warning */ 133 #define LOGW(_prefix_, ...) \ 134 do { \ 135 if (LOG_LEVEL >= LVL_WARNING) { \ 136 LOG(LOG_WARNING_PREFIX _prefix_, ## __VA_ARGS__); \ 137 } \ 138 } while (0) 139 140 /* Information */ 141 #define LOGI(_prefix_, ...) \ 142 do { \ 143 if (LOG_LEVEL >= LVL_INFO) { \ 144 LOG(LOG_INFO_PREFIX _prefix_, ## __VA_ARGS__); \ 145 } \ 146 } while (0) 147 148 /* DEBUG */ 149 #define LOGD(_prefix_, ...) \ 150 do { \ 151 if (LOG_LEVEL >= LVL_DEBUG) { \ 152 LOG(LOG_DEBUG_PREFIX _prefix_, ## __VA_ARGS__); \ 153 } \ 154 } while (0) 155 156 /* Vobase */ 157 #define LOGV(_prefix_, ...) \ 158 do { \ 159 if (LOG_LEVEL >= LVL_VERBOSE) { \ 160 LOG(LOG_VERBOSE_PREFIX _prefix_, ## __VA_ARGS__); \ 161 } \ 162 } while (0) 163 164 /* Address error - file and line */ 165 #define ERROR_ADDRESSING() \ 166 do { \ 167 LOGE(" |--> File - %s, Line - %d\r\n", __FILE__, __LINE__); \ 168 } while (0) 169 170 #define INIT_LOG() \ 171 do { \ 172 SEGGER_RTT_Init(); \ 173 LOGI("--------- Compiled - %s - %s ---------\r\n", \ 174 (uint32_t)__DATE__, \ 175 (uint32_t)__TIME__); \ 176 } while (0) 177 178 /** 179 * Event Log Part 180 */ 181 #define EVENT_LOG_LEVEL VERBOSE 182 183 #define NO_EVENT_LOG 0 184 #define CRITICAL 1 185 #define IMPORTANT 2 186 #define VERBOSE 3 187 188 #define CRITICAL_COLOR RTT_CTRL_TEXT_BRIGHT_RED 189 #define IMPORTANT_COLOR RTT_CTRL_TEXT_BRIGHT_YELLOW 190 #define VERBOSE_COLOR RTT_CTRL_RESET 191 192 #define FUNCTION "" 193 #define EVT_CATEGORY "" 194 195 /*FUNCTION Macro doesn't be used yet */ 196 #define EVT_CRITICAL_PREFIX CRITICAL_COLOR FUNCTION EVT_CATEGORY 197 #define EVT_IMPORTANT_PREFIX IMPORTANT_COLOR FUNCTION EVT_CATEGORY 198 #define EVT_VERBOSE_PREFIX VERBOSE_COLOR FUNCTION EVT_CATEGORY 199 200 #define COEX 1 201 #define DFU 1 202 #define ENDPOINT 1 203 #define PSTORE 1 204 #define GATT 1 205 #define GATT_SERVER 1 206 #define HARDWARE 1 207 #define LE_CONNECTION 1 208 #define LE_GAP 1 209 #define LE_SM 1 210 #define SYSTEM 1 211 #define TEST 1 212 #define USER 1 213 214 /** 215 * Critical events 216 */ 217 #define EVT_LOG_C(_evt_name_, _attached_, ...) \ 218 do { \ 219 if (EVENT_LOG_LEVEL >= CRITICAL) { \ 220 LOG(EVT_CRITICAL_PREFIX _evt_name_ _attached_, ## __VA_ARGS__); \ 221 } \ 222 } while (0) 223 224 /** 225 * Important events 226 */ 227 #define EVT_LOG_I(_evt_name_, _attached_, ...) \ 228 do { \ 229 if (EVENT_LOG_LEVEL >= IMPORTANT) { \ 230 LOG(EVT_IMPORTANT_PREFIX _evt_name_ _attached_, ## __VA_ARGS__); \ 231 } \ 232 } while (0) 233 234 /** 235 * Verbose events 236 */ 237 #define EVT_LOG_V(_evt_name_, _attached_, ...) \ 238 do { \ 239 if (EVENT_LOG_LEVEL >= VERBOSE) { \ 240 LOG(EVT_VERBOSE_PREFIX _evt_name_ _attached_, ## __VA_ARGS__); \ 241 } \ 242 } while (0) 243 244 /** 245 * Test purpose 246 */ 247 #define TRY_OUT_ALL_COLORS() \ 248 do { \ 249 for (uint8_t i_log_exlusive = 1; i_log_exlusive < 8; \ 250 i_log_exlusive++) { \ 251 SEGGER_RTT_printf(0, \ 252 "[2;3%dm" "Normal color. Test For Log out...\r\n", \ 253 i_log_exlusive); \ 254 SEGGER_RTT_printf(0, \ 255 "[1;3%dm" "Bright color. Test For Log out...\r\n", \ 256 i_log_exlusive); \ 257 } \ 258 } while (0) 259 260 /** 261 * Function declarations 262 */ 263 sl_status_t error_checking(sl_status_t sc, uint8_t directly); 264 void log_events(sl_bt_msg_t *evt); 265 266 #define SE_CALL(x) \ 267 do { \ 268 if (error_checking((x)->result, 0)) { \ 269 ERROR_ADDRESSING(); \ 270 } \ 271 } while (0) 272 273 #define SURROUNDING(x) "<" x "> - " 274 275 #define GENERAL_ERR_CHECK(x) \ 276 do { \ 277 if (x != GENERAL_SUCCESS) { \ 278 LOGE("Error Return. Error code = 0x%04X\r\n", x); \ 279 ERROR_ADDRESSING(); \ 280 } \ 281 } while (0) 282 283 #endif 284 #endif
4. 修改app.c文件
1 #include "em_common.h" 2 #include "sl_bluetooth.h" 3 #include "gatt_db.h" 4 5 #include "app_assert.h" 6 7 #include "app.h" 8 #include "log.h" 9 10 /* System */ 11 #define SYSTEM_BOOT_EVT SURROUNDING("Boot") 12 #define EXSYSTEM_TERNAL_SIGNAL_EVT SURROUNDING("External signal") 13 14 /* Bluetooth LE Connection */ 15 #define LE_CONNECTION_OPEN_EVT SURROUNDING("Opened") 16 #define LE_CONNECTION_CLOSED_EVT SURROUNDING("Closed") 17 #define LE_CONNECTION_UPDATE_EVT SURROUNDING("Parameters Updated") 18 #define LE_CONNECTION_RSSI SURROUNDING("RSSI") 19 20 /* Gatt Server */ 21 #define GATT_SERVER_CHARACTERISTIC_STATUS SURROUNDING("Characteristic Status") 22 #define GATT_SERVER_ATT_VALUE SURROUNDING("Attribute Value") 23 #define GATT_SERVER_READ_REQUEST SURROUNDING("Read Request") 24 25 /* DTM */ 26 #define TEST_DTM_COMPLETED SURROUNDING("DTM Completed") 27 28 #define COMMANDS_NOT_ADDED SURROUNDING("Command not added") 29 30 #define NO_INFO "" 31 #define FOLLOWINGS " |-->>" 32 33 static void log_out(uint8_t direct, const char *msg, sl_status_t sc); 34 35 // The advertising set handle allocated from Bluetooth stack. 36 static uint8_t advertising_set_handle = 0xff; 37 38 /**************************************************************************//** 39 * Application Init. 40 *****************************************************************************/ 41 SL_WEAK void app_init(void) 42 { 43 ///////////////////////////////////////////////////////////////////////////// 44 // Put your additional application init code here! // 45 // This is called once during start-up. // 46 ///////////////////////////////////////////////////////////////////////////// 47 } 48 49 /**************************************************************************//** 50 * Application Process Action. 51 *****************************************************************************/ 52 SL_WEAK void app_process_action(void) 53 { 54 ///////////////////////////////////////////////////////////////////////////// 55 // Put your additional application code here! // 56 // This is called infinitely. // 57 // Do not call blocking functions from here! // 58 ///////////////////////////////////////////////////////////////////////////// 59 } 60 61 /**************************************************************************//** 62 * Bluetooth stack event handler. 63 * This overrides the dummy weak implementation. 64 * 65 * @param[in] evt Event coming from the Bluetooth stack. 66 *****************************************************************************/ 67 void sl_bt_on_event(sl_bt_msg_t *evt) 68 { 69 sl_status_t sc; 70 71 log_events(evt); 72 73 switch (SL_BT_MSG_ID(evt->header)) { 74 // ------------------------------- 75 // This event indicates the device has started and the radio is ready. 76 // Do not call any stack command before receiving this boot event! 77 case sl_bt_evt_system_boot_id: 78 79 LOGE("This is an ERROR message\r\n"); 80 LOGW("This is a WARNING message\r\n"); 81 LOGD("This is a DEBUG message\r\n"); 82 LOGV("This is a VERBOSE message\r\n"); 83 LOGI("This is an INFORMATION message\r\n\r\n"); 84 85 // Create an advertising set. 86 sc = sl_bt_advertiser_create_set(&advertising_set_handle); 87 app_assert_status(sc); 88 89 // Generate data for advertising 90 sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle, 91 sl_bt_advertiser_general_discoverable); 92 app_assert_status(sc); 93 94 // Set advertising interval to 100ms. 95 sc = sl_bt_advertiser_set_timing( 96 advertising_set_handle, 97 160, // min. adv. interval (milliseconds * 1.6) 98 160, // max. adv. interval (milliseconds * 1.6) 99 0, // adv. duration 100 0); // max. num. adv. events 101 app_assert_status(sc); 102 Start advertising and enable connections. 103 sc = sl_bt_legacy_advertiser_start(advertising_set_handle, 104 sl_bt_advertiser_connectable_scannable); 105 app_assert_status(sc); 106 LOGD("Starting advertising\r\n"); 107 break; 108 109 // ------------------------------- 110 // This event indicates that a new connection was opened. 111 case sl_bt_evt_connection_opened_id: 112 LOGD("Connection opened\r\n"); 113 break; 114 115 // ------------------------------- 116 // This event indicates that a connection was closed. 117 case sl_bt_evt_connection_closed_id: 118 LOGD("Connection closed, reason: 0x%2.2x\r\n", 119 evt->data.evt_connection_closed.reason); 120 // Generate data for advertising 121 sc = sl_bt_legacy_advertiser_generate_data(advertising_set_handle, 122 sl_bt_advertiser_general_discoverable); 123 app_assert_status(sc); 124 125 // Restart advertising after client has disconnected. 126 sc = sl_bt_legacy_advertiser_start(advertising_set_handle, 127 sl_bt_advertiser_connectable_scannable); 128 app_assert_status(sc); 129 break; 130 131 /////////////////////////////////////////////////////////////////////////// 132 // Add additional event handlers here as your application requires! // 133 /////////////////////////////////////////////////////////////////////////// 134 135 // ------------------------------- 136 // Default event handler. 137 default: 138 break; 139 } 140 } 141 142 void log_events(sl_bt_msg_t *evt) 143 { 144 /* Handle events */ 145 switch (SL_BT_MSG_ID(evt->header)) { 146 #if (SYSTEM == 1) 147 #undef EVT_CATEGORY 148 #define EVT_CATEGORY "[SYSTEM]: " 149 case sl_bt_evt_system_boot_id: 150 EVT_LOG_C(SYSTEM_BOOT_EVT, NO_INFO); 151 LOGN(); 152 EVT_LOG_V(FOLLOWINGS, 153 "Major = 0x%04x, Minor = 0x%04x, Patch = 0x%04x, Build = %d, Bootloader = 0x%08lx, Hw = 0x%04x, Hash = 0x%08lx", 154 evt->data.evt_system_boot.major, 155 evt->data.evt_system_boot.minor, 156 evt->data.evt_system_boot.patch, 157 evt->data.evt_system_boot.build, 158 evt->data.evt_system_boot.bootloader, 159 evt->data.evt_system_boot.hw, 160 evt->data.evt_system_boot.hash); 161 LOGN(); 162 break; 163 164 case sl_bt_evt_system_external_signal_id: 165 EVT_LOG_I(EXSYSTEM_TERNAL_SIGNAL_EVT, "External signals = 0x%08lX", 166 evt->data.evt_system_external_signal.extsignals); 167 LOGN(); 168 break; 169 #endif 170 171 #if (LE_CONNECTION == 1) 172 #undef EVT_CATEGORY 173 #define EVT_CATEGORY "[LE_CONNECTION]: " 174 case sl_bt_evt_connection_opened_id: 175 EVT_LOG_C(LE_CONNECTION_OPEN_EVT, "Connection Handle = 0x%02x", 176 evt->data.evt_connection_opened.connection); 177 LOGN(); 178 EVT_LOG_I(FOLLOWINGS, "Peer address = "); 179 UINT8_ARRAY_DUMP(evt->data.evt_connection_opened.address.addr, 6); 180 LOGN(); 181 EVT_LOG_V(FOLLOWINGS, 182 "Role = %s, Bonding handle = %d, advertiser = %d", 183 evt->data.evt_connection_opened.master == 1 ? "Master" : "Slave", 184 evt->data.evt_connection_opened.bonding, 185 evt->data.evt_connection_opened.advertiser); 186 LOGN(); 187 break; 188 case sl_bt_evt_connection_closed_id: 189 EVT_LOG_C(LE_CONNECTION_CLOSED_EVT, "Handle = 0x%02x, Reason = ", 190 evt->data.evt_connection_closed.connection); 191 error_checking(evt->data.evt_connection_closed.reason, 1); 192 LOGN(); 193 break; 194 case sl_bt_evt_connection_parameters_id: 195 // Why float can't be printed while "print float" has been checked???? 196 EVT_LOG_I(LE_CONNECTION_UPDATE_EVT, 197 "Connection Handle = 0x%02x, Interval = %d*1.25ms, Latency = %d, Timeout = %dms", 198 evt->data.evt_connection_parameters.connection, 199 evt->data.evt_connection_parameters.interval, 200 evt->data.evt_connection_parameters.latency, 201 evt->data.evt_connection_parameters.timeout * 10); 202 203 /* TODO: There are still 2 parameters may need to be handled */ 204 LOGN(); 205 break; 206 case sl_bt_evt_connection_rssi_id: 207 // Status parameter??? 208 EVT_LOG_I(LE_CONNECTION_RSSI, "Handle = 0x%02x, Rssi = %ddBm", 209 evt->data.evt_connection_rssi.connection, 210 evt->data.evt_connection_rssi.rssi); 211 LOGN(); 212 break; 213 case sl_bt_evt_connection_phy_status_id: 214 break; 215 #endif 216 217 #if (GATT_SERVER == 1) 218 #undef EVT_CATEGORY 219 #define EVT_CATEGORY "[GATT_SERVER]: " 220 case sl_bt_evt_gatt_server_characteristic_status_id: 221 EVT_LOG_I(GATT_SERVER_CHARACTERISTIC_STATUS, 222 "Connection Handle = 0x%02x, Characteristic = 0x%04x, Type = %s, Value = 0x%04x", 223 evt->data.evt_gatt_server_characteristic_status.connection, 224 evt->data.evt_gatt_server_characteristic_status.characteristic, 225 evt->data.evt_gatt_server_characteristic_status.status_flags == 1 ? "Gatt server client config" : "Confirmation", 226 evt->data.evt_gatt_server_characteristic_status.client_config_flags); 227 LOGN(); 228 break; 229 case sl_bt_evt_gatt_server_attribute_value_id: 230 EVT_LOG_I(GATT_SERVER_ATT_VALUE, 231 "Connection Handle = 0x%02x, Attribute = 0x%04x, Op_Code = 0x%02x, Offset = 0x%04x, Value = ", 232 evt->data.evt_gatt_server_attribute_value.connection, 233 evt->data.evt_gatt_server_attribute_value.attribute, 234 evt->data.evt_gatt_server_attribute_value.att_opcode, 235 evt->data.evt_gatt_server_attribute_value.offset); 236 UINT8_ARRAY_DUMP(evt->data.evt_gatt_server_attribute_value.value.data, 237 evt->data.evt_gatt_server_attribute_value.value.len); 238 LOGN(); 239 break; 240 case sl_bt_evt_gatt_server_execute_write_completed_id: 241 break; 242 case sl_bt_evt_gatt_server_user_read_request_id: 243 EVT_LOG_I(GATT_SERVER_READ_REQUEST, 244 "Connection Handle = 0x%02x, Characteristic = 0x%04x, Op_Code = 0x%02x, Offset = 0x%04x", 245 evt->data.evt_gatt_server_user_read_request.connection, 246 evt->data.evt_gatt_server_user_read_request.characteristic, 247 evt->data.evt_gatt_server_user_read_request.att_opcode, 248 evt->data.evt_gatt_server_user_read_request.offset); 249 LOGN(); 250 break; 251 case sl_bt_evt_gatt_server_user_write_request_id: 252 EVT_LOG_I(GATT_SERVER_ATT_VALUE, 253 "Connection Handle = 0x%02x, Characteristic = 0x%04x, Op_Code = 0x%02x, Offset = 0x%04x, Value = ", 254 evt->data.evt_gatt_server_user_write_request.connection, 255 evt->data.evt_gatt_server_user_write_request.characteristic, 256 evt->data.evt_gatt_server_user_write_request.att_opcode, 257 evt->data.evt_gatt_server_user_write_request.offset); 258 UINT8_ARRAY_DUMP(evt->data.evt_gatt_server_user_write_request.value.data, 259 evt->data.evt_gatt_server_user_write_request.value.len); 260 LOGN(); 261 break; 262 #endif 263 264 #if (HARDWARE == 1) 265 #undef EVT_CATEGORY 266 #define EVT_CATEGORY "[HARDWARE]: " 267 case sl_bt_evt_system_soft_timer_id: 268 // TODO 269 break; 270 #endif 271 272 #if (TEST == 1) 273 #undef EVT_CATEGORY 274 #define EVT_CATEGORY "[TEST]: " 275 case sl_bt_evt_test_dtm_completed_id: 276 EVT_LOG_I(TEST_DTM_COMPLETED, "Result = "); 277 error_checking(evt->data.evt_test_dtm_completed.result, 1); 278 LOGN(); 279 EVT_LOG_I(FOLLOWINGS, 280 "Number of Packets = %d", 281 evt->data.evt_test_dtm_completed.number_of_packets); 282 LOGN(); 283 break; 284 #endif 285 286 default: 287 EVT_LOG_V(COMMANDS_NOT_ADDED, 288 "Header = 0x%08lx", 289 SL_BT_MSG_ID(evt->header)); 290 LOGN(); 291 break; 292 } 293 } 294 295 static void log_out(uint8_t direct, const char *msg, sl_status_t sc) 296 { 297 if (direct) { 298 LOG_DIRECT_ERR("0x%04lX - %s", sc, msg); 299 } else { 300 LOGE("0x%04lX - %s", sc, msg); 301 LOGN(); 302 } 303 } 304 305 sl_status_t error_checking(sl_status_t sc, uint8_t directly) 306 { 307 if (sc == SL_STATUS_OK) { 308 if (directly) { 309 LOG("Success"); 310 } 311 return sc; 312 } 313 314 switch (sc) { 315 case SL_STATUS_BT_PS_STORE_FULL: 316 log_out(directly, "Flash reserved for PS store is full", sc); 317 break; 318 case SL_STATUS_BT_PS_KEY_NOT_FOUND: 319 log_out(directly, "PS key not found", sc); 320 break; 321 case SL_STATUS_INVALID_HANDLE: 322 log_out(directly, "Invalid handle", sc); 323 break; 324 case SL_STATUS_BT_CTRL_CONNECTION_TIMEOUT: 325 log_out(directly, "Link supervision timeout has expired", sc); 326 break; 327 case SL_STATUS_BT_CTRL_INVALID_COMMAND_PARAMETERS: 328 log_out(directly, "Command contained invalid parameter", sc); 329 break; 330 case SL_STATUS_BT_L2CAP_WRONG_STATE: 331 log_out(directly, "Device is in wrong state to receive command", sc); 332 break; 333 case SL_STATUS_BT_CTRL_MEMORY_CAPACITY_EXCEEDED: 334 log_out(directly, "Controller is out of memory", sc); 335 break; 336 case SL_STATUS_BT_CTRL_REMOTE_USER_TERMINATED: 337 log_out(directly, 338 "User on the remote device terminated the connection", 339 sc); 340 break; 341 case 342 SL_STATUS_BT_CTRL_REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES 343 : 344 log_out(directly, 345 "The remote device terminated the connection because of low resources", 346 sc); 347 break; 348 case SL_STATUS_BT_CTRL_REMOTE_POWERING_OFF: 349 log_out(directly, 350 "Remote Device Terminated Connection due to Power Off", 351 sc); 352 break; 353 case SL_STATUS_BT_CTRL_CONNECTION_TERMINATED_BY_LOCAL_HOST: 354 log_out(directly, "Local device terminated the connection", sc); 355 break; 356 case SL_STATUS_BT_CTRL_PAIRING_NOT_ALLOWED: 357 log_out(directly, "The device does not allow pairing", sc); 358 break; 359 case SL_STATUS_BT_CTRL_LL_RESPONSE_TIMEOUT: 360 log_out(directly, 361 "Connection terminated due to link-layer procedure timeout", 362 sc); 363 break; 364 case SL_STATUS_BT_ATT_INVALID_HANDLE: 365 log_out(directly, 366 "The attribute handle given was not valid on this server", 367 sc); 368 break; 369 case SL_STATUS_BT_ATT_READ_NOT_PERMITTED: 370 log_out(directly, "The attribute cannot be read", sc); 371 break; 372 case SL_STATUS_BT_ATT_WRITE_NOT_PERMITTED: 373 log_out(directly, "The attribute cannot be written", sc); 374 break; 375 case SL_STATUS_BT_ATT_INSUFFICIENT_AUTHENTICATION: 376 log_out(directly, 377 "The attribute requires authentication before it can be read or written", 378 sc); 379 break; 380 case SL_STATUS_BT_ATT_INVALID_OFFSET: 381 log_out(directly, 382 "Offset specified was past the end of the attribute", 383 sc); 384 break; 385 case SL_STATUS_BT_ATT_ATT_NOT_FOUND: 386 log_out(directly, 387 "No attribute found within the given attribute handle range", 388 sc); 389 break; 390 default: 391 log_out(directly, "Error needs to be added", sc); 392 LOG("Error code = 0x%04X", (int)sc); 393 break; 394 } 395 return sc; 396 }
5. 编译程序并下载到开发板中
6. 打开电脑上的J-Link RTT Viewer V6.86d,选择BG22的型号; 此处选择512的型号(选择V6.86版本:过早版本没有BG22芯片的选项,过新版本有些Jlink设备有限制)
7. RTT log显示如下
完