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