X_NUCLEO_IDB05A1
Dependencies: mbed-os-example-ble-Advertising
source/platform/stm32_bluenrg_ble.cpp@293:5a1f577bf92f, 2016-09-15 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Thu Sep 15 10:52:00 2016 +0100
- Branch:
- 8247cc25d48b37d6a78a3e91c728cbbf0fd9ba29
- Revision:
- 293:5a1f577bf92f
- Parent:
- 264:1e754a01869e
Sync with 8247cc25d48b37d6a78a3e91c728cbbf0fd9ba29
2016-07-28 15:55:49+02:00: Andrea Palmieri
Update include
Signed-off-by: Andrea Palmieri <andrea.palmieri@st.com>
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wolfgang Betz |
132:51056160fa4a | 1 | /** |
Wolfgang Betz |
132:51056160fa4a | 2 | ****************************************************************************** |
Wolfgang Betz |
132:51056160fa4a | 3 | * @file stm32_bluenrg_ble.cpp |
Wolfgang Betz |
132:51056160fa4a | 4 | * @author CL |
Wolfgang Betz |
132:51056160fa4a | 5 | * @version V1.0.0 |
Wolfgang Betz |
132:51056160fa4a | 6 | * @date 15-June-2015 |
Wolfgang Betz |
132:51056160fa4a | 7 | * @brief |
Wolfgang Betz |
132:51056160fa4a | 8 | ****************************************************************************** |
Wolfgang Betz |
132:51056160fa4a | 9 | * @attention |
Wolfgang Betz |
132:51056160fa4a | 10 | * |
Wolfgang Betz |
132:51056160fa4a | 11 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
Wolfgang Betz |
132:51056160fa4a | 12 | * |
Wolfgang Betz |
132:51056160fa4a | 13 | * Redistribution and use in source and binary forms, with or without modification, |
Wolfgang Betz |
132:51056160fa4a | 14 | * are permitted provided that the following conditions are met: |
Wolfgang Betz |
132:51056160fa4a | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
Wolfgang Betz |
132:51056160fa4a | 16 | * this list of conditions and the following disclaimer. |
Wolfgang Betz |
132:51056160fa4a | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Wolfgang Betz |
132:51056160fa4a | 18 | * this list of conditions and the following disclaimer in the documentation |
Wolfgang Betz |
132:51056160fa4a | 19 | * and/or other materials provided with the distribution. |
Wolfgang Betz |
132:51056160fa4a | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Wolfgang Betz |
132:51056160fa4a | 21 | * may be used to endorse or promote products derived from this software |
Wolfgang Betz |
132:51056160fa4a | 22 | * without specific prior written permission. |
Wolfgang Betz |
132:51056160fa4a | 23 | * |
Wolfgang Betz |
132:51056160fa4a | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Wolfgang Betz |
132:51056160fa4a | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Wolfgang Betz |
132:51056160fa4a | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Wolfgang Betz |
132:51056160fa4a | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Wolfgang Betz |
132:51056160fa4a | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Wolfgang Betz |
132:51056160fa4a | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Wolfgang Betz |
132:51056160fa4a | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Wolfgang Betz |
132:51056160fa4a | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Wolfgang Betz |
132:51056160fa4a | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Wolfgang Betz |
132:51056160fa4a | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Wolfgang Betz |
132:51056160fa4a | 34 | * |
Wolfgang Betz |
132:51056160fa4a | 35 | ****************************************************************************** |
Wolfgang Betz |
132:51056160fa4a | 36 | */ |
Wolfgang Betz |
132:51056160fa4a | 37 | |
Wolfgang Betz |
132:51056160fa4a | 38 | /* Includes ------------------------------------------------------------------*/ |
Wolfgang Betz |
132:51056160fa4a | 39 | #include "BlueNRGGap.h" |
Wolfgang Betz |
132:51056160fa4a | 40 | #include "BlueNRGDevice.h" |
Wolfgang Betz |
139:3a75965fd389 | 41 | //#include "Utils.h" |
Wolfgang Betz |
133:1bb8df697f7f | 42 | #include "btle.h" |
Wolfgang Betz |
132:51056160fa4a | 43 | |
Wolfgang Betz |
132:51056160fa4a | 44 | // FIXME: find a better way to get the instance of the BlueNRG device |
Wolfgang Betz |
132:51056160fa4a | 45 | extern BlueNRGDevice bluenrgDeviceInstance; |
Wolfgang Betz |
132:51056160fa4a | 46 | |
Wolfgang Betz |
132:51056160fa4a | 47 | |
Wolfgang Betz |
132:51056160fa4a | 48 | //////////////////////////////////////// |
Wolfgang Betz |
132:51056160fa4a | 49 | // Start of C function wrappers |
Wolfgang Betz |
132:51056160fa4a | 50 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 51 | extern "C" { |
Wolfgang Betz |
132:51056160fa4a | 52 | #endif |
Wolfgang Betz |
132:51056160fa4a | 53 | |
Wolfgang Betz |
132:51056160fa4a | 54 | #include "stm32_bluenrg_ble.h" |
Vincent Coubard |
293:5a1f577bf92f | 55 | #include "ble_gp_timer.h" |
Vincent Coubard |
293:5a1f577bf92f | 56 | #include "ble_debug.h" |
Wolfgang Betz |
132:51056160fa4a | 57 | |
Wolfgang Betz |
132:51056160fa4a | 58 | |
Wolfgang Betz |
132:51056160fa4a | 59 | void BlueNRG_RST(void) |
Wolfgang Betz |
132:51056160fa4a | 60 | { |
Wolfgang Betz |
132:51056160fa4a | 61 | bluenrgDeviceInstance.reset(); |
Wolfgang Betz |
132:51056160fa4a | 62 | } |
Wolfgang Betz |
132:51056160fa4a | 63 | |
Wolfgang Betz |
132:51056160fa4a | 64 | uint8_t BlueNRG_DataPresent(void) |
Wolfgang Betz |
132:51056160fa4a | 65 | { |
Wolfgang Betz |
132:51056160fa4a | 66 | return (bluenrgDeviceInstance.dataPresent()); |
Wolfgang Betz |
132:51056160fa4a | 67 | } |
Wolfgang Betz |
132:51056160fa4a | 68 | |
Wolfgang Betz |
132:51056160fa4a | 69 | |
Wolfgang Betz |
132:51056160fa4a | 70 | /** |
Wolfgang Betz |
132:51056160fa4a | 71 | * @brief This function is a utility to print the log time |
Wolfgang Betz |
132:51056160fa4a | 72 | * in the format HH:MM:SS:MSS (DK GUI time format) |
Wolfgang Betz |
132:51056160fa4a | 73 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 74 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 75 | */ |
Wolfgang Betz |
132:51056160fa4a | 76 | void print_csv_time(void){ |
Wolfgang Betz |
132:51056160fa4a | 77 | #ifdef PRINT_CSV_FORMAT |
Wolfgang Betz |
132:51056160fa4a | 78 | uint32_t ms = 0;//ms_counter; |
Wolfgang Betz |
132:51056160fa4a | 79 | PRINT_CSV("%02d:%02d:%02d.%03d", ms/(60*60*1000)%24, ms/(60*1000)%60, (ms/1000)%60, ms%1000); |
Wolfgang Betz |
132:51056160fa4a | 80 | #endif |
Wolfgang Betz |
132:51056160fa4a | 81 | } |
Wolfgang Betz |
132:51056160fa4a | 82 | |
Wolfgang Betz |
132:51056160fa4a | 83 | /** |
Wolfgang Betz |
132:51056160fa4a | 84 | * @brief Writes data to a serial interface. |
Wolfgang Betz |
132:51056160fa4a | 85 | * @param data1 : 1st buffer |
Wolfgang Betz |
132:51056160fa4a | 86 | * @param data2 : 2nd buffer |
Wolfgang Betz |
132:51056160fa4a | 87 | * @param n_bytes1: number of bytes in 1st buffer |
Wolfgang Betz |
132:51056160fa4a | 88 | * @param n_bytes2: number of bytes in 2nd buffer |
Wolfgang Betz |
132:51056160fa4a | 89 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 90 | */ |
Wolfgang Betz |
132:51056160fa4a | 91 | void Hal_Write_Serial(const void* data1, const void* data2, int32_t n_bytes1, |
Wolfgang Betz |
132:51056160fa4a | 92 | int32_t n_bytes2) |
Wolfgang Betz |
132:51056160fa4a | 93 | { |
Wolfgang Betz |
132:51056160fa4a | 94 | struct timer t; |
Wolfgang Betz |
132:51056160fa4a | 95 | |
Wolfgang Betz |
132:51056160fa4a | 96 | Timer_Set(&t, CLOCK_SECOND/10); |
Wolfgang Betz |
132:51056160fa4a | 97 | |
Wolfgang Betz |
132:51056160fa4a | 98 | #ifdef PRINT_CSV_FORMAT |
Wolfgang Betz |
132:51056160fa4a | 99 | print_csv_time(); |
Wolfgang Betz |
132:51056160fa4a | 100 | for (int i=0; i<n_bytes1; i++) { |
Wolfgang Betz |
132:51056160fa4a | 101 | PRINT_CSV(" %02x", ((uint8_t *)data1)[i]); |
Wolfgang Betz |
132:51056160fa4a | 102 | } |
Wolfgang Betz |
132:51056160fa4a | 103 | for (int i=0; i<n_bytes2; i++) { |
Wolfgang Betz |
132:51056160fa4a | 104 | PRINT_CSV(" %02x", ((uint8_t *)data2)[i]); |
Wolfgang Betz |
132:51056160fa4a | 105 | } |
Wolfgang Betz |
132:51056160fa4a | 106 | PRINT_CSV("\n"); |
Wolfgang Betz |
132:51056160fa4a | 107 | #endif |
Wolfgang Betz |
132:51056160fa4a | 108 | |
Wolfgang Betz |
132:51056160fa4a | 109 | while(1){ |
Wolfgang Betz |
132:51056160fa4a | 110 | if(BlueNRG_SPI_Write((uint8_t *)data1,(uint8_t *)data2, n_bytes1, n_bytes2)==0) break; |
Wolfgang Betz |
132:51056160fa4a | 111 | if(Timer_Expired(&t)){ |
Wolfgang Betz |
132:51056160fa4a | 112 | break; |
Wolfgang Betz |
132:51056160fa4a | 113 | } |
Wolfgang Betz |
132:51056160fa4a | 114 | } |
Wolfgang Betz |
132:51056160fa4a | 115 | } |
Wolfgang Betz |
132:51056160fa4a | 116 | |
Wolfgang Betz |
132:51056160fa4a | 117 | |
Wolfgang Betz |
132:51056160fa4a | 118 | /** |
Wolfgang Betz |
132:51056160fa4a | 119 | * @brief Activate internal bootloader using pin. |
Wolfgang Betz |
132:51056160fa4a | 120 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 121 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 122 | */ |
Wolfgang Betz |
132:51056160fa4a | 123 | void BlueNRG_HW_Bootloader(void) |
Wolfgang Betz |
132:51056160fa4a | 124 | { |
Antonio Vilei |
245:6830f71c197c | 125 | // Reset BlueNRG SPI interface |
Antonio Vilei |
245:6830f71c197c | 126 | BlueNRG_RST(); |
Antonio Vilei |
245:6830f71c197c | 127 | |
Antonio Vilei |
245:6830f71c197c | 128 | // Send an ACI command to reboot BlueNRG in bootloader mode |
Antonio Vilei |
245:6830f71c197c | 129 | // The safest way to get in bootloader mode is keeping high |
Antonio Vilei |
245:6830f71c197c | 130 | // the interrupt pin during reset, but this would require many |
Antonio Vilei |
245:6830f71c197c | 131 | // changes to the current mbed driver |
Antonio Vilei |
245:6830f71c197c | 132 | aci_updater_start(); |
Wolfgang Betz |
132:51056160fa4a | 133 | } |
Wolfgang Betz |
132:51056160fa4a | 134 | |
Wolfgang Betz |
132:51056160fa4a | 135 | /** |
Wolfgang Betz |
132:51056160fa4a | 136 | * @brief Reads from BlueNRG SPI buffer and store data into local buffer. |
Wolfgang Betz |
132:51056160fa4a | 137 | * @param buffer : Buffer where data from SPI are stored |
Wolfgang Betz |
132:51056160fa4a | 138 | * @param buff_size: Buffer size |
Wolfgang Betz |
132:51056160fa4a | 139 | * @retval int32_t : Number of read bytes |
Wolfgang Betz |
132:51056160fa4a | 140 | */ |
Wolfgang Betz |
132:51056160fa4a | 141 | int32_t BlueNRG_SPI_Read_All(uint8_t *buffer, |
Wolfgang Betz |
132:51056160fa4a | 142 | uint8_t buff_size) |
Wolfgang Betz |
132:51056160fa4a | 143 | { |
Wolfgang Betz |
132:51056160fa4a | 144 | int32_t ret = bluenrgDeviceInstance.spiRead(buffer, buff_size); |
Wolfgang Betz |
132:51056160fa4a | 145 | |
Wolfgang Betz |
132:51056160fa4a | 146 | return ret; |
Wolfgang Betz |
132:51056160fa4a | 147 | } |
Wolfgang Betz |
132:51056160fa4a | 148 | |
Wolfgang Betz |
132:51056160fa4a | 149 | /** |
Wolfgang Betz |
132:51056160fa4a | 150 | * @brief Writes data from local buffer to SPI. |
Wolfgang Betz |
132:51056160fa4a | 151 | * @param data1 : First data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 152 | * @param data2 : Second data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 153 | * @param Nb_bytes1: Size of first data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 154 | * @param Nb_bytes2: Size of second data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 155 | * @retval Number of read bytes |
Wolfgang Betz |
132:51056160fa4a | 156 | */ |
Wolfgang Betz |
132:51056160fa4a | 157 | int32_t BlueNRG_SPI_Write(uint8_t* data1, |
Wolfgang Betz |
132:51056160fa4a | 158 | uint8_t* data2, uint8_t Nb_bytes1, uint8_t Nb_bytes2) |
Wolfgang Betz |
132:51056160fa4a | 159 | { |
Wolfgang Betz |
132:51056160fa4a | 160 | int32_t ret = bluenrgDeviceInstance.spiWrite(data1, data2, Nb_bytes1, Nb_bytes2); |
Wolfgang Betz |
132:51056160fa4a | 161 | |
Wolfgang Betz |
132:51056160fa4a | 162 | return ret; |
Wolfgang Betz |
132:51056160fa4a | 163 | } |
Wolfgang Betz |
132:51056160fa4a | 164 | |
Wolfgang Betz |
132:51056160fa4a | 165 | /** |
Wolfgang Betz |
132:51056160fa4a | 166 | * @brief Enable SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 167 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 168 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 169 | */ |
Wolfgang Betz |
132:51056160fa4a | 170 | void Enable_SPI_IRQ(void) |
Wolfgang Betz |
132:51056160fa4a | 171 | { |
Wolfgang Betz |
132:51056160fa4a | 172 | bluenrgDeviceInstance.enable_irq(); |
Wolfgang Betz |
132:51056160fa4a | 173 | } |
Wolfgang Betz |
132:51056160fa4a | 174 | |
Vincent Coubard |
264:1e754a01869e | 175 | void signalEventsToProcess(void) { |
Vincent Coubard |
264:1e754a01869e | 176 | if(btle_handler_pending == 0) { |
Vincent Coubard |
264:1e754a01869e | 177 | btle_handler_pending = 1; |
Vincent Coubard |
264:1e754a01869e | 178 | bluenrgDeviceInstance.signalEventsToProcess(BLE::DEFAULT_INSTANCE); |
Vincent Coubard |
264:1e754a01869e | 179 | } |
Wolfgang Betz |
133:1bb8df697f7f | 180 | } |
Wolfgang Betz |
133:1bb8df697f7f | 181 | |
Wolfgang Betz |
132:51056160fa4a | 182 | /** |
Wolfgang Betz |
139:3a75965fd389 | 183 | * @brief Disable SPI IRQ. |
Wolfgang Betz |
139:3a75965fd389 | 184 | * @param None |
Wolfgang Betz |
139:3a75965fd389 | 185 | * @retval None |
Wolfgang Betz |
139:3a75965fd389 | 186 | */ |
Wolfgang Betz |
139:3a75965fd389 | 187 | void Disable_SPI_IRQ(void) |
Wolfgang Betz |
139:3a75965fd389 | 188 | { |
Wolfgang Betz |
139:3a75965fd389 | 189 | bluenrgDeviceInstance.disable_irq(); |
Wolfgang Betz |
139:3a75965fd389 | 190 | } |
Wolfgang Betz |
139:3a75965fd389 | 191 | |
Wolfgang Betz |
139:3a75965fd389 | 192 | /** |
Wolfgang Betz |
132:51056160fa4a | 193 | * @brief Clear Pending SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 194 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 195 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 196 | */ |
Wolfgang Betz |
132:51056160fa4a | 197 | void Clear_SPI_IRQ(void) |
Wolfgang Betz |
132:51056160fa4a | 198 | { |
Wolfgang Betz |
132:51056160fa4a | 199 | } |
Wolfgang Betz |
132:51056160fa4a | 200 | |
Wolfgang Betz |
132:51056160fa4a | 201 | /** |
Wolfgang Betz |
132:51056160fa4a | 202 | * @brief Clear EXTI (External Interrupt) line for SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 203 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 204 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 205 | */ |
Wolfgang Betz |
132:51056160fa4a | 206 | void Clear_SPI_EXTI_Flag(void) |
Wolfgang Betz |
132:51056160fa4a | 207 | { |
Wolfgang Betz |
132:51056160fa4a | 208 | } |
Wolfgang Betz |
132:51056160fa4a | 209 | |
Wolfgang Betz |
132:51056160fa4a | 210 | |
Wolfgang Betz |
132:51056160fa4a | 211 | |
Wolfgang Betz |
132:51056160fa4a | 212 | |
Wolfgang Betz |
132:51056160fa4a | 213 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 214 | } |
Wolfgang Betz |
132:51056160fa4a | 215 | #endif |
Wolfgang Betz |
132:51056160fa4a | 216 | // End of C function wrappers |
Wolfgang Betz |
132:51056160fa4a | 217 | //////////////////////////////////////// |
Wolfgang Betz |
132:51056160fa4a | 218 | |
Vincent Coubard |
264:1e754a01869e | 219 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |