Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_if.c « app « STM32_WPAN « BLE_MeshLightingProvisioner « BLE « Applications « P-NUCLEO-WB55.Nucleo « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 43a0e548dadcbdc4dce1d5ad2cac4609de0704ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
******************************************************************************
* @file    user_if.c
* @author  BLE Mesh Team
* @version V1.09.000
* @date    15-Oct-2018
* @brief   User interface file 
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*   1. Redistributions of source code must retain the above copyright notice,
*      this list of conditions and the following disclaimer.
*   2. Redistributions in binary form must reproduce the above copyright notice,
*      this list of conditions and the following disclaimer in the documentation
*      and/or other materials provided with the distribution.
*   3. Neither the name of STMicroelectronics nor the names of its contributors
*      may be used to endorse or promote products derived from this software
*      without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Initial BlueNRG-Mesh is built over Motorola’s Mesh over Bluetooth Low Energy 
* (MoBLE) technology. The present solution is developed and maintained for both 
* Mesh library and Applications solely by STMicroelectronics.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "cube_hal.h"
#include "ble_clock.h" 
#include "hci.h"
#include "user_if.h" 
#include "hal.h"  
#include "hal_common.h"
#include "mesh_cfg.h"

#include <stdio.h>
#ifdef IAR
#include <yfuns.h>
#else
#define _LLIO_STDIN  0
#define _LLIO_STDOUT 1
#define _LLIO_STDERR 2
#define _LLIO_ERROR ((size_t)-1) /* For __read and __write. */
#endif



typedef struct _IO_ReceiveCallBack
{
  /**
  * callback to notify the application with the received I/O data
  */ 
  IO_RECEIVE_DATA_USER_CALLBACK_TYPE IO_user_application;
} IO_ReceiveCallBack;
/**
* @}
*/

#define READ_DATA_SIZE      1024
#define READ_BUFFER_IS_EMPTY() (Read_ptr_in == Read_ptr_out)

/* IO User Callback used to handle received data from I/O channel*/
static uint8_t  Read_Buffer [READ_DATA_SIZE]; 
static uint32_t Read_ptr_in = 0;
static uint32_t Read_ptr_out = 0;
/*static volatile uint8_t s_SdkEvalVersion = ID_UNKNOWN;*/
static IO_ReceiveCallBack IO_Receive_Data;
static void SdkEval_IO_processInputData(uint8_t * rx_data, uint16_t data_size);


/**
* @brief  SetLed Sets the state of led
* @param  int state State for the led GPIO
* @retval void
*/ 
void SetLed(int state)
{
  if (state)
  {
    BSP_LED_On(LED_BLUE);
  }
  else
  {
    BSP_LED_Off(LED_BLUE);
  }
}

#if 0
/**
* @brief  GetButtonState 
* @param  void
* @retval BUTTON_STATE returns the status of the userbutton GPIO
*/ 
BUTTON_STATE GetButtonState(void)
{
  return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
}

/**
* @brief  InitButton initializes the GPIO for the user button 
* @param  void
* @retval void
*/
void InitButton(void)
{
  GPIO_InitTypeDef init;
  init.Pin = GPIO_PIN_13;
  init.Mode = GPIO_MODE_INPUT | GPIO_MODE_EVT_FALLING;
  init.Pull = GPIO_NOPULL;
  init.Speed = GPIO_SPEED_VERY_LOW;
  init.Alternate = 0;
  __HAL_RCC_GPIOC_CLK_ENABLE();
  HAL_GPIO_Init(GPIOC, &init);
  EXTI->FTSR |= EXTI_RTSR_TR13; /* enable wake-up event on falling edge */
  EXTI->IMR &= ~EXTI_IMR_MR13;  /* disable interrupt */
  EXTI->EMR  |= EXTI_EMR_MR13;  /* enable event */
}
#endif

/**
* @brief  processInputData  
* @param  uint8_t * rx_data pointer to receive data
* @param  uint16_t data_size size of receive data
* @retval void
*/
void processInputData(uint8_t * rx_data, uint16_t data_size)
{
  SdkEval_IO_processInputData(rx_data, data_size);
}

/**
* @brief  Configure the selected I/O communication channel
* @param  None.
* @retval None.
*/
void SdkEval_IO_Config(IO_RECEIVE_DATA_USER_CALLBACK_TYPE io_receive_function)
{
  IO_Receive_Data.IO_user_application = io_receive_function;
#if defined (ENABLE_USB)
  /* Init Device Library */
  USBD_Init(&USBD_Device, &VCP_Desc, 0);
  
  /* Add Supported Class */
  USBD_RegisterClass(&USBD_Device, USBD_CDC_CLASS);
  
  /* Add CDC Interface Class */
  USBD_CDC_RegisterInterface(&USBD_Device, &USBD_CDC_fops);
  
  /* Start Device Process */
  USBD_Start(&USBD_Device);
  
#elif defined (ENABLE_USART)
  /* Init Serial Uart */
  MX_USART2_UART_Init();
#endif 
}

/**
* @brief  InitDevice intitializes the Device  
* @param  void
* @retval void
*/
void InitDevice(void)
{
  Init_Device(); /* Initializes HAL and System Clock */
  
#ifndef NEWLIB_SYSCALLS
  SdkEval_IO_Config(processInputData);
#endif
  /* Delay for debug purpose, in order to see printed data at startup. */
#if defined(ENABLE_USART)
  Clock_Wait(2000);
#endif
  
#if defined(ACCELEROMETER_TYPE) || defined(THERMOMETER_TYPE)
  I2C_Reset();
  Temperature_Init();
  Accel_Init();
#endif
  InitButton();
}


/**
* @brief  ShouldSleepFunc sleep mode fuction   
* @param  void
* @retval void
*/
void ShouldSleepFunc(void)
{
  __disable_irq();
  __WFE();
  __NOP();
  __enable_irq();
}

/**
* @brief  Receive data on selected I/O port
* @param  rx_data pointer to buffer of received
* @param  data_size number of received data 
* @retval None.
*/
void SdkEval_IO_Receive_Data(uint8_t * rx_data, uint16_t data_size)
{
  if(IO_Receive_Data.IO_user_application)
    IO_Receive_Data.IO_user_application(rx_data,data_size);
  
}
/**
* @brief  Read buffer push
* @param  byte 
* @retval None.
*/
void Read_Buffer_Push(uint8_t byte)
{
  Read_Buffer[Read_ptr_in] = byte;
  
  Read_ptr_in++;
  
  /* To avoid buffer overflow */
  if(Read_ptr_in == READ_DATA_SIZE)
  {
    Read_ptr_in = 0;
  }        
}
/**
* @brief  Read buffer pop
* @param  byte 
* @retval None.
*/
int Read_Buffer_Pop(uint8_t *byte)
{
  if(READ_BUFFER_IS_EMPTY())
    return 0;
  
  *byte = Read_Buffer[Read_ptr_out];
  
  Read_ptr_out++;
  
  if(Read_ptr_out == READ_DATA_SIZE)
    Read_ptr_out = 0;  
  
  return 1;
}
/**
* @brief  Process input data
* @param  byte 
* @retval None.
*/
static void SdkEval_IO_processInputData(uint8_t * rx_data, uint16_t data_size)
{
  for(int i = 0; i < data_size; i++){
    Read_Buffer_Push(rx_data[i]);
  }
}
extern UART_HandleTypeDef huart2;
#undef putchar
/**
* @brief  Put character
* @param  byte 
* @retval None.
*/
int putchar(int c)
{
  /* wait for the transmission end */
  while(!(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE)));
  /* send the data */
  HAL_UART_Transmit(&huart2, (uint8_t*)&c, 1, UART_TX_TIMEOUT);
  return c;
}

void __io_putchar(char c)
{
  putchar(c);
}

/* Required for Keil */
#ifndef __IAR_SYSTEMS_ICC__
void _ttywrch(int ch)
{
  putchar(ch);
}

int fputc(int ch, FILE *f)
{
  _ttywrch(ch);
  return(ch);
}
int fgetc(FILE *f)
{
  return -1;
}
#endif

/**
* @brief  Write handle
* @param  buffer 
* @param  size 
* @retval None.
*/
size_t _write(int handle, const unsigned char * buffer, size_t size)
{
  size_t nChars = 0;
  
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR) {
    return _LLIO_ERROR;
  }
  
  if (buffer == 0) {
    /* This means that we should flush internal buffers.
    spin until TX complete (TX is idle)*/
#ifdef ENABLE_USB
    while (!USB_OUT_BUFFER_IS_EMPTY()) {}   
#endif /* ENABLE_USB */
    return 0;
  }
  
  while(size--) {
    __io_putchar(*buffer++);
    ++nChars;
  }
  
  return nChars;
}

/**
* @brief  Read handle
* @param  buffer 
* @param  size 
* @retval None.
*/
size_t __read(int handle, unsigned char * buffer, size_t size)
{
  size_t bytes = 0;
  
  while(bytes < size && Read_Buffer_Pop(buffer+bytes)){
    bytes++;
  }
  return bytes;
}

uint8_t __io_getcharNonBlocking(uint8_t *data)
{
  if (__read(_LLIO_STDIN,data,1))
    return 1;
  else
    return 0;
}/* end serialReadByte() */

/******************* (C) COPYRIGHT 2019 STMicroelectronics *****END OF FILE****/