X_NUCLEO_IDB05A1
Dependencies: mbed-os-example-ble-Advertising
source/platform/stm32_bluenrg_ble.cpp@138:32e3c4ca7a45, 2015-10-08 (annotated)
- Committer:
- Wolfgang Betz
- Date:
- Thu Oct 08 07:29:43 2015 +0200
- Revision:
- 138:32e3c4ca7a45
- Parent:
- 136:83d24503e76a
- Child:
- 139:3a75965fd389
Use 'AST_FOR_MBED_OS' instead of 'YOTTA_CFG_MBED_OS'
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 |
132:51056160fa4a | 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" |
Wolfgang Betz |
132:51056160fa4a | 55 | #include "gp_timer.h" |
Wolfgang Betz |
132:51056160fa4a | 56 | #include "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 | { |
Wolfgang Betz |
132:51056160fa4a | 125 | // FIXME: this is not implemented yet |
Wolfgang Betz |
132:51056160fa4a | 126 | while (1); |
Wolfgang Betz |
132:51056160fa4a | 127 | } |
Wolfgang Betz |
132:51056160fa4a | 128 | |
Wolfgang Betz |
132:51056160fa4a | 129 | /** |
Wolfgang Betz |
132:51056160fa4a | 130 | * @brief Reads from BlueNRG SPI buffer and store data into local buffer. |
Wolfgang Betz |
132:51056160fa4a | 131 | * @param buffer : Buffer where data from SPI are stored |
Wolfgang Betz |
132:51056160fa4a | 132 | * @param buff_size: Buffer size |
Wolfgang Betz |
132:51056160fa4a | 133 | * @retval int32_t : Number of read bytes |
Wolfgang Betz |
132:51056160fa4a | 134 | */ |
Wolfgang Betz |
132:51056160fa4a | 135 | int32_t BlueNRG_SPI_Read_All(uint8_t *buffer, |
Wolfgang Betz |
132:51056160fa4a | 136 | uint8_t buff_size) |
Wolfgang Betz |
132:51056160fa4a | 137 | { |
Wolfgang Betz |
132:51056160fa4a | 138 | int32_t ret = bluenrgDeviceInstance.spiRead(buffer, buff_size); |
Wolfgang Betz |
132:51056160fa4a | 139 | |
Wolfgang Betz |
132:51056160fa4a | 140 | return ret; |
Wolfgang Betz |
132:51056160fa4a | 141 | } |
Wolfgang Betz |
132:51056160fa4a | 142 | |
Wolfgang Betz |
132:51056160fa4a | 143 | /** |
Wolfgang Betz |
132:51056160fa4a | 144 | * @brief Writes data from local buffer to SPI. |
Wolfgang Betz |
132:51056160fa4a | 145 | * @param data1 : First data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 146 | * @param data2 : Second data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 147 | * @param Nb_bytes1: Size of first data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 148 | * @param Nb_bytes2: Size of second data buffer to be written |
Wolfgang Betz |
132:51056160fa4a | 149 | * @retval Number of read bytes |
Wolfgang Betz |
132:51056160fa4a | 150 | */ |
Wolfgang Betz |
132:51056160fa4a | 151 | int32_t BlueNRG_SPI_Write(uint8_t* data1, |
Wolfgang Betz |
132:51056160fa4a | 152 | uint8_t* data2, uint8_t Nb_bytes1, uint8_t Nb_bytes2) |
Wolfgang Betz |
132:51056160fa4a | 153 | { |
Wolfgang Betz |
132:51056160fa4a | 154 | int32_t ret = bluenrgDeviceInstance.spiWrite(data1, data2, Nb_bytes1, Nb_bytes2); |
Wolfgang Betz |
132:51056160fa4a | 155 | |
Wolfgang Betz |
132:51056160fa4a | 156 | return ret; |
Wolfgang Betz |
132:51056160fa4a | 157 | } |
Wolfgang Betz |
132:51056160fa4a | 158 | |
Wolfgang Betz |
132:51056160fa4a | 159 | /** |
Wolfgang Betz |
132:51056160fa4a | 160 | * @brief Enable SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 161 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 162 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 163 | */ |
Wolfgang Betz |
132:51056160fa4a | 164 | void Enable_SPI_IRQ(void) |
Wolfgang Betz |
132:51056160fa4a | 165 | { |
Wolfgang Betz |
132:51056160fa4a | 166 | bluenrgDeviceInstance.enable_irq(); |
Wolfgang Betz |
132:51056160fa4a | 167 | } |
Wolfgang Betz |
132:51056160fa4a | 168 | |
Wolfgang Betz |
132:51056160fa4a | 169 | /** |
Wolfgang Betz |
132:51056160fa4a | 170 | * @brief Disable SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 171 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 172 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 173 | */ |
Wolfgang Betz |
132:51056160fa4a | 174 | void Disable_SPI_IRQ(void) |
Wolfgang Betz |
132:51056160fa4a | 175 | { |
Wolfgang Betz |
132:51056160fa4a | 176 | bluenrgDeviceInstance.disable_irq(); |
Wolfgang Betz |
132:51056160fa4a | 177 | } |
Wolfgang Betz |
132:51056160fa4a | 178 | |
Wolfgang Betz |
138:32e3c4ca7a45 | 179 | #ifdef AST_FOR_MBED_OS |
Wolfgang Betz |
133:1bb8df697f7f | 180 | /** |
Wolfgang Betz |
133:1bb8df697f7f | 181 | * Call BTLE callback handler. |
Wolfgang Betz |
133:1bb8df697f7f | 182 | * @param None |
Wolfgang Betz |
133:1bb8df697f7f | 183 | * @retval None |
Wolfgang Betz |
133:1bb8df697f7f | 184 | */ |
Wolfgang Betz |
133:1bb8df697f7f | 185 | void Call_BTLE_Handler(void) |
Wolfgang Betz |
133:1bb8df697f7f | 186 | { |
Wolfgang Betz |
134:d2f8d5898d04 | 187 | if(!btle_handler_pending) { |
Wolfgang Betz |
134:d2f8d5898d04 | 188 | btle_handler_pending = 1; |
Wolfgang Betz |
134:d2f8d5898d04 | 189 | minar::Scheduler::postCallback(btle_handler); |
Wolfgang Betz |
134:d2f8d5898d04 | 190 | } |
Wolfgang Betz |
133:1bb8df697f7f | 191 | } |
Wolfgang Betz |
133:1bb8df697f7f | 192 | #endif |
Wolfgang Betz |
133:1bb8df697f7f | 193 | |
Wolfgang Betz |
132:51056160fa4a | 194 | /** |
Wolfgang Betz |
132:51056160fa4a | 195 | * @brief Clear Pending SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 196 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 197 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 198 | */ |
Wolfgang Betz |
132:51056160fa4a | 199 | void Clear_SPI_IRQ(void) |
Wolfgang Betz |
132:51056160fa4a | 200 | { |
Wolfgang Betz |
132:51056160fa4a | 201 | } |
Wolfgang Betz |
132:51056160fa4a | 202 | |
Wolfgang Betz |
132:51056160fa4a | 203 | /** |
Wolfgang Betz |
132:51056160fa4a | 204 | * @brief Clear EXTI (External Interrupt) line for SPI IRQ. |
Wolfgang Betz |
132:51056160fa4a | 205 | * @param None |
Wolfgang Betz |
132:51056160fa4a | 206 | * @retval None |
Wolfgang Betz |
132:51056160fa4a | 207 | */ |
Wolfgang Betz |
132:51056160fa4a | 208 | void Clear_SPI_EXTI_Flag(void) |
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 | |
Wolfgang Betz |
132:51056160fa4a | 214 | |
Wolfgang Betz |
132:51056160fa4a | 215 | #ifdef __cplusplus |
Wolfgang Betz |
132:51056160fa4a | 216 | } |
Wolfgang Betz |
132:51056160fa4a | 217 | #endif |
Wolfgang Betz |
132:51056160fa4a | 218 | // End of C function wrappers |
Wolfgang Betz |
132:51056160fa4a | 219 | //////////////////////////////////////// |
Wolfgang Betz |
132:51056160fa4a | 220 | |
Wolfgang Betz |
132:51056160fa4a | 221 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |