| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

SPI_Master_Polling_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : SPI_Master_Polling_main.c
4 * Author : VMA DIVISION
5 * Version : 1.0.0
6 * Date : 21-September-2015
7 * Description : Code demostrating the SPI master functionality
8 ********************************************************************************
9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
11 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
12 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
13 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
14 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
15 *******************************************************************************/
16
203 /* Includes ------------------------------------------------------------------*/
204 #include "BlueNRG1.h"
205 #include <stdio.h>
206 #include "BlueNRG1_conf.h"
207 #include "SDK_EVAL_Config.h"
208
221 /* Private typedef -----------------------------------------------------------*/
222 /* Private define ------------------------------------------------------------*/
223 #define TOGGLE_DL1 '1'
224 #define TOGGLE_DL2 '2'
225 #define TOGGLE_DL3 '3'
226 #define TOGGLE_ALL_LED '4'
227
228 /* Chip Select pin used for the Master_Polling demo */
229 #define SPI_CS_MS_DEMO_PIN GPIO_Pin_6
230
231
232 /* Private macro -------------------------------------------------------------*/
233 /* Private variables ---------------------------------------------------------*/
234 /* Private function prototypes -----------------------------------------------*/
235 void processCommand(void);
236 void helpMessage(void);
237 void SPI_Master_Configuration(void);
238 void SPI_SendReceiveData(uint8_t *command, uint8_t len);
239
240 /* Private functions ---------------------------------------------------------*/
241
247 int main(void)
248 {
249 /* System initialization function */
250 SystemInit();
251
252 /* Identify BlueNRG1 platform */
253 SdkEvalIdentification();
254
255 /* UART initialization */
256 SdkEvalComUartInit(UART_BAUDRATE);
257
258 /* SPI initialization */
259 SPI_Master_Configuration();
260
261 /* Infinite loop for process command */
262 processCommand();
263
264 }
265
271 void helpMessage(void)
272 {
273 printf("** SPI Master test program **\r\n(requires another board with SPI Slave test program connected by SPI\r\n\n");
274 printf("%c: Toggle LED DL1\r\n", TOGGLE_DL1);
275 printf("%c: Toggle LED DL2\r\n", TOGGLE_DL2);
276 printf("%c: Toggle LED DL3\r\n", TOGGLE_DL3);
277 printf("%c: Toggle ALL LED\r\n", TOGGLE_ALL_LED);
278 }
279
285 void processCommand(void)
286 {
287 uint8_t command;
288
289 /* Infinite loop */
290 while(1) {
291
292 /* Loop until the UART Receive Data Register is not empty */
293 while (UART_GetFlagStatus(UART_FLAG_RXFE) == SET);
294
295 /* Store the received byte in RxBuffer */
296 command = UART_ReceiveData();
297
298 /* Parse the command */
299 switch (command) {
300 case TOGGLE_DL1:
301 {
302 /* SPI Master communication */
303 SPI_SendReceiveData(&command, 1);
304 printf("Toggle LED DL1\r\n");
305 }
306 break;
307 case TOGGLE_DL2:
308 {
309 /* SPI Master communication */
310 SPI_SendReceiveData(&command, 1);
311 printf("Toggle LED DL2\r\n");
312 }
313 break;
314 case TOGGLE_DL3:
315 {
316 /* SPI Master communication */
317 SPI_SendReceiveData(&command, 1);
318 printf("Toggle LED DL3\r\n");
319 }
320 break;
321 case TOGGLE_ALL_LED:
322 {
323 uint8_t led[] = {TOGGLE_DL1, TOGGLE_DL2, TOGGLE_DL3};
324 /* SPI Master communication */
325 SPI_SendReceiveData(led, sizeof(led));
326 printf("All LED Toggled\r\n");
327 }
328 break;
329 default:
330 helpMessage();
331 break;
332 }
333 }
334 }
335
336
342 void SPI_Master_Configuration(void)
343 {
344 SPI_InitType SPI_InitStructure;
345 GPIO_InitType GPIO_InitStructure;
346
347 /* Enable SPI and GPIO clocks */
348 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_GPIO | CLOCK_PERIPH_SPI, ENABLE);
349
350 /* Configure SPI pins */
351 GPIO_StructInit(&GPIO_InitStructure);
352 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MOSI_PIN;
353 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MOSI_MODE;
354 GPIO_InitStructure.GPIO_Pull = ENABLE;
355 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
356 GPIO_Init(&GPIO_InitStructure);
357
358 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MISO_PIN;
359 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MISO_MODE;
360 GPIO_Init(&GPIO_InitStructure);
361
362 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_SCLK_PIN;
363 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_SCLK_MODE;
364 GPIO_Init(&GPIO_InitStructure);
365
366 GPIO_InitStructure.GPIO_Pin = SPI_CS_MS_DEMO_PIN;
367 GPIO_InitStructure.GPIO_Mode = GPIO_Output;
368 GPIO_InitStructure.GPIO_HighPwr = ENABLE;
369 GPIO_Init(&GPIO_InitStructure);
370 GPIO_SetBits(SPI_CS_MS_DEMO_PIN);
371
372 /* Configure SPI in master mode */
373 SPI_StructInit(&SPI_InitStructure);
374 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
375 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b ;
376 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
377 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
378 SPI_InitStructure.SPI_BaudRate = SPI_BAUDRATE;
379 SPI_Init(&SPI_InitStructure);
380
381 /* Clear RX and TX FIFO */
382 SPI_ClearTXFIFO();
383 SPI_ClearRXFIFO();
384
385 /* Set null character */
386 SPI_SetDummyCharacter(0xFF);
387
388 /* Set communication mode */
389 SPI_SetMasterCommunicationMode(SPI_FULL_DUPLEX_MODE);
390
391 /* Enable SPI functionality */
392 SPI_Cmd(ENABLE);
393
394 }
395
402 void SPI_SendReceiveData(uint8_t *command, uint8_t len)
403 {
404 GPIO_ResetBits(SPI_CS_MS_DEMO_PIN);
405
406 for (uint8_t i=0; i< len; i++) {
407 /* Write data to send to TX FIFO */
408 while(RESET == SPI_GetFlagStatus(SPI_FLAG_TFE));
409 SPI_SendData(command[i]);
410 while(RESET == SPI_GetFlagStatus(SPI_FLAG_RNE));
411 SPI_ReceiveData();
412 while (SET == SPI_GetFlagStatus(SPI_FLAG_BSY));
413 }
414 GPIO_SetBits(SPI_CS_MS_DEMO_PIN);
415 }
416
417
418 #ifdef USE_FULL_ASSERT
419
426 void assert_failed(uint8_t* file, uint32_t line)
427 {
428 /* User can add his own implementation to report the file name and line number,
429 ex: printff("Wrong parameters value: file %s on line %d\r\n", file, line) */
430
431 /* Infinite loop */
432 while (1)
433 {
434 }
435 }
436
437 #endif
438
451 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
TOGGLE_DL1
#define TOGGLE_DL1
Definition: SPI_Master_Polling_main.c:223
processCommand
void processCommand(void)
Process command code.
Definition: SPI_Master_Polling_main.c:285
helpMessage
void helpMessage(void)
Help message code.
Definition: SPI_Master_Polling_main.c:271
TOGGLE_ALL_LED
#define TOGGLE_ALL_LED
Definition: SPI_Master_Polling_main.c:226
SPI_Master_Configuration
void SPI_Master_Configuration(void)
SPI Master initialization.
Definition: SPI_Master_Polling_main.c:342
TOGGLE_DL3
#define TOGGLE_DL3
Definition: SPI_Master_Polling_main.c:225
main
int main(void)
Main program code.
Definition: SPI_Master_Polling_main.c:247
TOGGLE_DL2
#define TOGGLE_DL2
Definition: SPI_Master_Polling_main.c:224
SPI_SendReceiveData
void SPI_SendReceiveData(uint8_t *command, uint8_t len)
SPI Master communication.
Definition: SPI_Master_Polling_main.c:402
UART_BAUDRATE
#define UART_BAUDRATE
Definition: ADC_PDM_main.c:225
SPI_CS_MS_DEMO_PIN
#define SPI_CS_MS_DEMO_PIN
Definition: SPI_Master_Polling_main.c:229