| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

SPI_Master_Dma_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : SPI_Master_Dma_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 "BlueNRG1_it.h"
208 #include "SDK_EVAL_Config.h"
209
222 /* Private typedef -----------------------------------------------------------*/
223 /* Private define ------------------------------------------------------------*/
224 #define TOGGLE_DL1 '1'
225 #define TOGGLE_DL2 '2'
226 #define TOGGLE_DL3 '3'
227 #define TOGGLE_ALL_LED '4'
228
229 #define SYST_CLOCK 16000000 /*< System clock frequency */
230
231 /* Variables used for DMA operation */
232 extern volatile FlagStatus spi_eot;
233 extern volatile uint8_t spi_buffer_tx[SPI_BUFF_SIZE];
234 extern volatile uint8_t spi_buffer_rx[SPI_BUFF_SIZE];
235 //extern uint8_t buffer_spi_tail;
236 extern volatile uint8_t buffer_spi_used;
237
238 /* Private macro -------------------------------------------------------------*/
239 /* Private variables ---------------------------------------------------------*/
240 /* Private function prototypes -----------------------------------------------*/
241 void processCommand(void);
242 void helpMessage(void);
243 void SPI_Master_Configuration(void);
244 void DMAUart_Sending(uint32_t buffer, uint32_t size) ;
245
246 /* Private functions ---------------------------------------------------------*/
247
253 int main(void)
254 {
255 /* System initialization function */
256 SystemInit();
257
258 /* Identify BlueNRG1 platform */
259 SdkEvalIdentification();
260
261 /* UART initialization */
262 SdkEvalComUartInit(UART_BAUDRATE);
263
264 /* Configure SysTick to generate interrupt with 25ms period */
265 SysTick_Config(SYST_CLOCK/40);
266
267 /* SPI initialization */
268 SPI_Master_Configuration();
269
270 /* Infinite loop for process command */
271 processCommand();
272
273 }
274
280 void helpMessage(void)
281 {
282 printf("** SPI Master test program **\r\n(requires another board with SPI Slave test program connected by SPI\r\n\n");
283 printf("%c: Toggle LED DL1\r\n", TOGGLE_DL1);
284 printf("%c: Toggle LED DL2\r\n", TOGGLE_DL2);
285 printf("%c: Toggle LED DL3\r\n", TOGGLE_DL3);
286 printf("%c: Toggle ALL LED\r\n", TOGGLE_ALL_LED);
287 }
288
294 void processCommand(void)
295 {
296 uint8_t command;
297
298 /* Infinite loop */
299 while(1) {
300
301 /* Loop until the UART Receive Data Register is not empty */
302 while (UART_GetFlagStatus(UART_FLAG_RXFE) == SET);
303
304 /* Store the received byte in RxBuffer */
305 command = UART_ReceiveData();
306
307 /* Parse the command */
308 switch (command) {
309 case TOGGLE_DL1:
310 {
311 /* SPI Master communication */
312 DMAUart_Sending((uint32_t)&command, 1);
313 printf("Toggle LED DL1\r\n");
314 }
315 break;
316 case TOGGLE_DL2:
317 {
318 /* SPI Master communication */
319 DMAUart_Sending((uint32_t)&command, 1);
320 printf("Toggle LED DL2\r\n");
321 }
322 break;
323 case TOGGLE_DL3:
324 {
325 /* SPI Master communication */
326 DMAUart_Sending((uint32_t)&command, 1);
327 printf("Toggle LED DL3\r\n");
328 }
329 break;
330 case TOGGLE_ALL_LED:
331 {
332 uint8_t led[] = {TOGGLE_DL1, TOGGLE_DL2, TOGGLE_DL3};
333 /* SPI Master communication */
334 DMAUart_Sending((uint32_t)led, sizeof(led));
335 printf("All LED Toggled\r\n");
336 }
337 break;
338 default:
339 helpMessage();
340 break;
341 }
342 }
343 }
344
345
351 void SPI_Master_Configuration(void)
352 {
353 SPI_InitType SPI_InitStructure;
354 GPIO_InitType GPIO_InitStructure;
355
356 /* Enable SPI and GPIO clocks */
357 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_GPIO | CLOCK_PERIPH_SPI, ENABLE);
358
359 /* Configure SPI pins */
360 GPIO_StructInit(&GPIO_InitStructure);
361 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MOSI_PIN;
362 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MOSI_MODE;
363 GPIO_InitStructure.GPIO_Pull = ENABLE;
364 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
365 GPIO_Init(&GPIO_InitStructure);
366
367 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MISO_PIN;
368 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MISO_MODE;
369 GPIO_Init(&GPIO_InitStructure);
370
371 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_SCLK_PIN;
372 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_SCLK_MODE;
373 GPIO_Init(&GPIO_InitStructure);
374
375 GPIO_InitStructure.GPIO_Pin = SPI_CS_MS_DEMO_PIN;
376 GPIO_InitStructure.GPIO_Mode = GPIO_Output;
377 GPIO_InitStructure.GPIO_HighPwr = ENABLE;
378 GPIO_Init(&GPIO_InitStructure);
379 GPIO_SetBits(SPI_CS_MS_DEMO_PIN);
380
381 /* Configure SPI in master mode */
382 SPI_StructInit(&SPI_InitStructure);
383 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
384 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b ;
385 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
386 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
387 SPI_InitStructure.SPI_BaudRate = SPI_BAUDRATE;
388 SPI_Init(&SPI_InitStructure);
389
390 /* Clear RX and TX FIFO */
391 SPI_ClearTXFIFO();
392 SPI_ClearRXFIFO();
393
394 /* Set null character */
395 SPI_SetDummyCharacter(0xFF);
396
397 /* Set communication mode */
398 SPI_SetMasterCommunicationMode(SPI_FULL_DUPLEX_MODE);
399
400
401 /* Configure DMA peripheral */
402 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_DMA, ENABLE);
403
404 DMA_InitType DMA_InitStructure;
405 NVIC_InitType NVIC_InitStructure;
406 /* Configure DMA SPI TX channel */
407 DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_DR_BASE_ADDR;
408 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_buffer_tx;
409 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
410 DMA_InitStructure.DMA_BufferSize = (uint32_t)SPI_BUFF_SIZE;
411 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
412 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
413 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
414 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
415 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
416 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
417 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
418 DMA_Init(DMA_CH_SPI_TX, &DMA_InitStructure);
419
420 /* Configure DMA SPI RX channel */
421 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_buffer_rx;
422 DMA_InitStructure.DMA_BufferSize = (uint32_t)SPI_BUFF_SIZE;
423 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
424 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
425 DMA_Init(DMA_CH_SPI_RX, &DMA_InitStructure);
426 DMA_Cmd(DMA_CH_SPI_RX, ENABLE);
427
428 /* Enable SPI_TX/SPI_RX DMA requests */
429 SPI_DMACmd(SPI_DMAReq_Tx | SPI_DMAReq_Rx, ENABLE);
430
431 /* Enable DMA_CH_UART_TX Transfer Complete interrupt */
432 DMA_FlagConfig(DMA_CH_SPI_TX, DMA_FLAG_TC, ENABLE);
433
434 /* Enable the USARTx Interrupt */
435 NVIC_InitStructure.NVIC_IRQChannel = DMA_IRQn;
436 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = HIGH_PRIORITY;
437 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
438 NVIC_Init(&NVIC_InitStructure);
439
440 /* Enable SPI functionality */
441 SPI_Cmd(ENABLE);
442
443 }
444
445
446 void DMAUart_Sending(uint32_t buffer, uint32_t size)
447 {
448 while(spi_eot==RESET);
449 spi_eot = RESET;
450
451 /* Rearm the DMA_CH_SPI_TX */
452 DMA_CH_SPI_TX->CMAR = buffer;
453 DMA_CH_SPI_TX->CNDTR = size;
454
455 GPIO_ResetBits(SPI_CS_MS_DEMO_PIN);
456
457 /* DMA_CH_SPI_TX enable */
458 DMA_CH_SPI_TX->CCR_b.EN = SET;
459
460 }
461
462
463
464 #ifdef USE_FULL_ASSERT
465
472 void assert_failed(uint8_t* file, uint32_t line)
473 {
474 /* User can add his own implementation to report the file name and line number,
475 ex: printff("Wrong parameters value: file %s on line %d\r\n", file, line) */
476
477 /* Infinite loop */
478 while (1)
479 {
480 }
481 }
482
483 #endif
484
497 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
SPI_Master_Configuration
void SPI_Master_Configuration(void)
SPI Master initialization.
Definition: SPI_Master_Dma_main.c:351
SYST_CLOCK
#define SYST_CLOCK
Definition: SPI_Master_Dma_main.c:229
TOGGLE_DL2
#define TOGGLE_DL2
Definition: SPI_Master_Dma_main.c:225
spi_eot
volatile FlagStatus spi_eot
helpMessage
void helpMessage(void)
Help message code.
Definition: SPI_Master_Dma_main.c:280
main
int main(void)
Main program code.
Definition: SPI_Master_Dma_main.c:253
buffer_spi_used
volatile uint8_t buffer_spi_used
spi_buffer_rx
volatile uint8_t spi_buffer_rx[SPI_BUFF_SIZE]
spi_buffer_tx
volatile uint8_t spi_buffer_tx[SPI_BUFF_SIZE]
TOGGLE_DL1
#define TOGGLE_DL1
Definition: SPI_Master_Dma_main.c:224
TOGGLE_DL3
#define TOGGLE_DL3
Definition: SPI_Master_Dma_main.c:226
processCommand
void processCommand(void)
Process command code.
Definition: SPI_Master_Dma_main.c:294
DMAUart_Sending
void DMAUart_Sending(uint32_t buffer, uint32_t size)
Definition: SPI_Master_Dma_main.c:446
UART_BAUDRATE
#define UART_BAUDRATE
Definition: ADC_PDM_main.c:225
TOGGLE_ALL_LED
#define TOGGLE_ALL_LED
Definition: SPI_Master_Dma_main.c:227
SPI_CS_MS_DEMO_PIN
#define SPI_CS_MS_DEMO_PIN
Definition: SPI_Master_Polling_main.c:229