ver:init

Committer:
iv123
Date:
Sun Jun 18 16:10:28 2017 +0000
Revision:
0:88b85febcb45
Initial commit

Who changed what in which revision?

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