|
| 1 | +/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. |
| 2 | + * |
| 3 | + * The information contained herein is property of Nordic Semiconductor ASA. |
| 4 | + * Terms and conditions of usage are described in detail in NORDIC |
| 5 | + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
| 6 | + * |
| 7 | + * Licensees are granted free, non-transferable use of the information. NO |
| 8 | + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
| 9 | + * the file. |
| 10 | + * |
| 11 | + * $LastChangedRevision$ |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Test project to put the nRF8001 in Test mode and allow DTM commands over the Arduino Serial interface. |
| 16 | + * Note: The serial interface of some arduino boards is reset on startup, so some work is needed on the nRFgo studio |
| 17 | + * before it can be used with this project. |
| 18 | + */ |
| 19 | + |
| 20 | +/** @defgroup ble_set_in_test_mode ble_set_in_test_mode |
| 21 | +@{ |
| 22 | +@ingroup projects |
| 23 | +@brief Put the nRF8001 in test mode. |
| 24 | +
|
| 25 | +@details |
| 26 | +This project is to put the nRF8001 in test mode and enable the nRF8001 to accept DTM commands over ACI using the Arduino serial interface. |
| 27 | +Note: Serial Event is NOT compatible with Leonardo, Micro, Esplora |
| 28 | +@todo: Test this to make sure it works with both the pyhon script and nRFgo studio: You can send the DTM commands from the nRFgo studio or from a Nordic Semiconductor supplied python script on a Windows PC. |
| 29 | + */ |
| 30 | +#include <SPI.h> |
| 31 | +#include <avr/pgmspace.h> |
| 32 | +#include <hal_aci_tl.h> |
| 33 | +#include <lib_aci.h> |
| 34 | + |
| 35 | +// aci_struct that will contain |
| 36 | +// total initial credits |
| 37 | +// current credit |
| 38 | +// current state of the aci (setup/test/standby/active/sleep) |
| 39 | +// open remote pipe pending |
| 40 | +// close remote pipe pending |
| 41 | +// Current pipe available bitmap |
| 42 | +// Current pipe closed bitmap |
| 43 | +// Current connection interval, slave latency and link supervision timeout |
| 44 | +// Current State of the the GATT client (Service Discovery) |
| 45 | +// Status of the bond (R) Peer address |
| 46 | +static struct aci_state_t aci_state; |
| 47 | + |
| 48 | +static hal_aci_evt_t aci_data; |
| 49 | +static hal_aci_data_t aci_cmd; |
| 50 | + |
| 51 | +static char dtm_buffer[3]; //DTM input from PC, needs one ekstra buffer as toCharArray null terminates the string. |
| 52 | +uint8_t dtm_report[2]; // DTM output |
| 53 | +String inputString = ""; // a String to hold incoming data @todo: consider changing this to a string - char array |
| 54 | +boolean stringComplete = false; // whether the string is complete |
| 55 | +boolean dtmMode = false; //is the device in dtm mode |
| 56 | + |
| 57 | +void setup(void) |
| 58 | +{ |
| 59 | + Serial.begin(19200); |
| 60 | + //Wait until the serial port is available (useful only for the leonardo) |
| 61 | + while(!Serial) |
| 62 | + {} |
| 63 | + Serial.println(F("Arduino setup")); |
| 64 | + //Tell the ACI library, the MCU to nRF8001 pin connections |
| 65 | + aci_state.aci_pins.board_name = BOARD_DEFAULT; //See board.h for details REDBEARLAB_SHIELD_V1_1 or BOARD_DEFAULT |
| 66 | + aci_state.aci_pins.reqn_pin = SS; //SS for Nordic board, 9 for REDBEARLABS |
| 67 | + aci_state.aci_pins.rdyn_pin = 3; //3 for Nordic board, 8 for REDBEARLABS |
| 68 | + aci_state.aci_pins.mosi_pin = MOSI; |
| 69 | + aci_state.aci_pins.miso_pin = MISO; |
| 70 | + aci_state.aci_pins.sck_pin = SCK; |
| 71 | + |
| 72 | + aci_state.aci_pins.spi_clock_divider = SPI_CLOCK_DIV8; |
| 73 | + |
| 74 | + aci_state.aci_pins.reset_pin = 4; //4 for Nordic board, UNUSED for REDBEARLABS |
| 75 | + aci_state.aci_pins.active_pin = UNUSED; |
| 76 | + aci_state.aci_pins.optional_chip_sel_pin = UNUSED; |
| 77 | + |
| 78 | + aci_state.aci_pins.interface_is_interrupt = false; |
| 79 | + aci_state.aci_pins.interrupt_number = 1; |
| 80 | + |
| 81 | + lib_aci_init(&aci_state); |
| 82 | + |
| 83 | + Serial.println(F("nRF8001 Reset done")); |
| 84 | +} |
| 85 | + |
| 86 | +void aci_loop(void) |
| 87 | +{ |
| 88 | + // We enter the if statement only when there is a ACI event available to be processed |
| 89 | + if (lib_aci_event_get(&aci_state, &aci_data)) |
| 90 | + { |
| 91 | + aci_evt_t * aci_evt; |
| 92 | + |
| 93 | + aci_evt = &aci_data.evt; |
| 94 | + switch(aci_evt->evt_opcode) |
| 95 | + { |
| 96 | + /** |
| 97 | + As soon as you reset the nRF8001 you will get an ACI Device Started Event |
| 98 | + */ |
| 99 | + case ACI_EVT_DEVICE_STARTED: |
| 100 | + { |
| 101 | + aci_state.data_credit_available = aci_evt->params.device_started.credit_available; |
| 102 | + switch(aci_evt->params.device_started.device_mode) |
| 103 | + { |
| 104 | + case ACI_DEVICE_SETUP: |
| 105 | + Serial.println(F("Evt Device Started: Setup")); |
| 106 | + //Put the nRF8001 in Test mode. |
| 107 | + //See ACI Test Command in Section 24 (System Commands) of the nRF8001 datasheet. |
| 108 | + //Use ACI_TEST_MODE_DTM_ACI to send DTM commands over ACI |
| 109 | + //lib_aci_test(ACI_TEST_MODE_DTM_UART); |
| 110 | + lib_aci_test(ACI_TEST_MODE_DTM_ACI); |
| 111 | + break; |
| 112 | + |
| 113 | + case ACI_DEVICE_TEST: |
| 114 | + { |
| 115 | + uint8_t i = 0; |
| 116 | + Serial.println(F("Evt Device Started: Test")); |
| 117 | + Serial.println(F("Device in DTM over ACI mode")); |
| 118 | + Serial.println(F("Use DTM over UART with the Arduino serial interface to send DTM commands to the nRF8001")); |
| 119 | + dtmMode = true; |
| 120 | + } |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + break; //ACI Device Started Event |
| 125 | + case ACI_EVT_CMD_RSP: |
| 126 | + //If an ACI command response event comes with an error -> stop |
| 127 | + if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status) |
| 128 | + { |
| 129 | + //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of |
| 130 | + //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE |
| 131 | + //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command |
| 132 | + Serial.print(F("ACI Command 0x")); |
| 133 | + Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); |
| 134 | + Serial.println(F("Evt Cmd respone: Error. Arduino is in an while(1); loop")); |
| 135 | + while (1); |
| 136 | + } |
| 137 | + else if (ACI_CMD_DTM_CMD == aci_evt->params.cmd_rsp.cmd_opcode) |
| 138 | + { |
| 139 | + dtm_report[0] = aci_evt->params.cmd_rsp.params.dtm_cmd.evt_msb; |
| 140 | + dtm_report[1] = aci_evt->params.cmd_rsp.params.dtm_cmd.evt_lsb; |
| 141 | + Serial.write(dtm_report,2); |
| 142 | + } |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + // No event in the ACI Event queue |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void dtm_command_loop(void) |
| 153 | +{ |
| 154 | + if(dtmMode) |
| 155 | + { |
| 156 | + // print the string when a newline arrives: |
| 157 | + if (stringComplete) { |
| 158 | + if (inputString.length() > 2) |
| 159 | + { |
| 160 | + Serial.println(F("DTM command to long")); //Not compatible with DTM tester? |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + inputString.toCharArray(dtm_buffer,3); |
| 165 | + //Forwarding DTM command to the nRF8001 |
| 166 | + lib_aci_dtm_command(dtm_buffer[0], dtm_buffer[1]); |
| 167 | + } |
| 168 | + |
| 169 | + // clear the string: |
| 170 | + inputString = ""; |
| 171 | + stringComplete = false; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +void loop() |
| 177 | +{ |
| 178 | + //Process any ACI commands or events |
| 179 | + aci_loop(); |
| 180 | + |
| 181 | + //Process any DTM command, DTM Events is processed in the aci_loop |
| 182 | + dtm_command_loop(); |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +/* |
| 187 | + SerialEvent occurs whenever a new data comes in the |
| 188 | + hardware serial RX. This routine is run between each |
| 189 | + time loop() runs, so using delay inside loop can delay |
| 190 | + response. Multiple bytes of data may be available. |
| 191 | + Serial Event is NOT compatible with Leonardo, Micro, Esplora |
| 192 | + */ |
| 193 | +void serialEvent() { |
| 194 | + while (Serial.available()) { |
| 195 | + // get the new byte: |
| 196 | + char inChar = (char)Serial.read(); |
| 197 | + // add it to the inputString: |
| 198 | + inputString += inChar; |
| 199 | + // if the incoming character is a newline, set a flag |
| 200 | + // so the main loop can do something about it: |
| 201 | + if (inputString.length()==2) { |
| 202 | + stringComplete = true; |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments