| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

SPI_Slave_Dma_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : SPI_Slave_Dma_main.c
4 * Author : VMA DIVISION
5 * Version : 1.0.0
6 * Date : 21-September-2015
7 * Description : Code demostrating the SPI slave 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
190 /* Includes ------------------------------------------------------------------*/
191 #include "BlueNRG1.h"
192 #include "BlueNRG1_conf.h"
193 #include "SDK_EVAL_Config.h"
194
207 /* Private typedef -----------------------------------------------------------*/
208 /* Private define ------------------------------------------------------------*/
209 #define TOGGLE_DL1 '1'
210 #define TOGGLE_DL2 '2'
211 #define TOGGLE_DL3 '3'
212
213 #define SYST_CLOCK 16000000 /*< System clock frequency */
214
215 /* Chip Select pin used for the Slave_Polling demo */
216 #define SPI_CS_SLAVE_DEMO_PIN GPIO_Pin_11
217 #define SPI_CS_SLAVE_DEMO_MODE Serial0_Mode
218
219 /* Variables used for DMA operation */
220 extern volatile uint8_t spi_buffer_tx[SPI_BUFF_SIZE];
221 extern volatile uint8_t spi_buffer_rx[SPI_BUFF_SIZE];
222 extern uint8_t buffer_spi_tail;
223 extern volatile uint8_t buffer_spi_used;
224
225 /* Private macro -------------------------------------------------------------*/
226 /* Private variables ---------------------------------------------------------*/
227 /* Private function prototypes -----------------------------------------------*/
228 void processCommand(void);
229 void SPI_Slave_Configuration(void);
230 void DMAUart_Sending(uint32_t buffer, uint32_t size);
231
232 /* Private functions ---------------------------------------------------------*/
233
239 int main(void)
240 {
241 /* System initialization function */
242 SystemInit();
243
244 /* Identify BlueNRG1 platform */
245 SdkEvalIdentification();
246
247 /* LEDS initialization */
248 SdkEvalLedInit(LED1);
249 SdkEvalLedInit(LED2);
250 SdkEvalLedInit(LED3);
251
252 /* Configure SysTick to generate interrupt with 25ms period */
253 SysTick_Config(SYST_CLOCK/40);
254
255 /* SPI initialization */
256 SPI_Slave_Configuration();
257
258 /* Infinite loop for process command */
259 processCommand();
260
261 }
262
268 void processCommand(void)
269 {
270 uint8_t command;
271
272 /* Infinite loop */
273 while(1) {
274
275 /* Get the LEDS actual status */
276 spi_buffer_tx[0] = ((uint8_t)SdkEvalLedGetState(LED3))<<2 | ((uint8_t)SdkEvalLedGetState(LED2))<<1 | ((uint8_t)SdkEvalLedGetState(LED1));
277
278 /* Prepare buffer to sent through SPI DMA TX channel */
279 DMAUart_Sending((uint32_t)&spi_buffer_tx[0], 1);
280
281 while(buffer_spi_used==0);
282
283 /* Parse the command */
284 command = spi_buffer_rx[buffer_spi_tail];
285
286 if(command == TOGGLE_DL1) {
287 SdkEvalLedToggle(LED1);
288 }
289 else if(command == TOGGLE_DL2) {
290 SdkEvalLedToggle(LED2);
291 }
292 else if(command == TOGGLE_DL3) {
293 SdkEvalLedToggle(LED3);
294 }
295
296 buffer_spi_tail = (buffer_spi_tail+1)%SPI_BUFF_SIZE;
297 buffer_spi_used--;
298
299 }
300 }
301
302
303
309 void SPI_Slave_Configuration(void)
310 {
311 SPI_InitType SPI_InitStructure;
312 GPIO_InitType GPIO_InitStructure;
313
314 /* Enable SPI and GPIO clocks */
315 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_GPIO | CLOCK_PERIPH_SPI, ENABLE);
316
317 /* Configure SPI pins */
318 GPIO_StructInit(&GPIO_InitStructure);
319 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MOSI_PIN;
320 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MOSI_MODE;
321 GPIO_InitStructure.GPIO_Pull = ENABLE;
322 GPIO_InitStructure.GPIO_HighPwr = DISABLE;
323 GPIO_Init(&GPIO_InitStructure);
324
325 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_MISO_PIN;
326 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_MISO_MODE;
327 GPIO_Init(&GPIO_InitStructure);
328
329 GPIO_InitStructure.GPIO_Pin = SDK_EVAL_SPI_PERIPH_SCLK_PIN;
330 GPIO_InitStructure.GPIO_Mode = SDK_EVAL_SPI_PERIPH_SCLK_MODE;
331 GPIO_Init(&GPIO_InitStructure);
332
333 GPIO_InitStructure.GPIO_Pin = SPI_CS_SLAVE_DEMO_PIN;
334 GPIO_InitStructure.GPIO_Mode = SPI_CS_SLAVE_DEMO_MODE;
335 GPIO_Init(&GPIO_InitStructure);
336
337 /* Configure SPI in master mode */
338 SPI_StructInit(&SPI_InitStructure);
339 SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
340 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b ;
341 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
342 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
343 SPI_InitStructure.SPI_BaudRate = SPI_BAUDRATE;
344 SPI_Init(&SPI_InitStructure);
345
346 /* Clear RX and TX FIFO */
347 SPI_ClearTXFIFO();
348 SPI_ClearRXFIFO();
349
350
351 /* Configure DMA peripheral */
352 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_DMA, ENABLE);
353
354 DMA_InitType DMA_InitStructure;
355 /* Configure DMA SPI TX channel */
356 DMA_InitStructure.DMA_PeripheralBaseAddr = SPI_DR_BASE_ADDR;
357 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_buffer_tx;
358 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
359 DMA_InitStructure.DMA_BufferSize = (uint32_t)SPI_BUFF_SIZE;
360 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
361 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
362 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
363 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
364 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
365 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
366 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
367 DMA_Init(DMA_CH_SPI_TX, &DMA_InitStructure);
368
369 /* Configure DMA SPI RX channel */
370 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_buffer_rx;
371 DMA_InitStructure.DMA_BufferSize = (uint32_t)SPI_BUFF_SIZE;
372 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
373 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
374 DMA_Init(DMA_CH_SPI_RX, &DMA_InitStructure);
375 DMA_Cmd(DMA_CH_SPI_RX, ENABLE);
376
377 /* Enable SPI_TX/SPI_RX DMA requests */
378 SPI_DMACmd(SPI_DMAReq_Tx | SPI_DMAReq_Rx, ENABLE);
379
380
381 /* Enable SPI functionality */
382 SPI_Cmd(ENABLE);
383
384 }
385
386 void DMAUart_Sending(uint32_t buffer, uint32_t size)
387 {
388 /* DMA_CH_SPI_TX reset */
389 DMA_CH_SPI_TX->CCR_b.EN = RESET;
390
391 /* Rearm the DMA_CH_SPI_TX */
392 DMA_CH_SPI_TX->CMAR = buffer;
393 DMA_CH_SPI_TX->CNDTR = size;
394
395 /* DMA_CH_SPI_TX enable */
396 DMA_CH_SPI_TX->CCR_b.EN = SET;
397
398 }
399
400 #ifdef USE_FULL_ASSERT
401
408 void assert_failed(uint8_t* file, uint32_t line)
409 {
410 /* User can add his own implementation to report the file name and line number,
411 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
412
413 /* Infinite loop */
414 while (1)
415 {
416 }
417 }
418
419 #endif
420
433 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
processCommand
void processCommand(void)
Process command code.
Definition: SPI_Slave_Dma_main.c:268
buffer_spi_used
volatile uint8_t buffer_spi_used
SPI_Slave_Configuration
void SPI_Slave_Configuration(void)
SPI Slave initialization.
Definition: SPI_Slave_Dma_main.c:309
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_DL3
#define TOGGLE_DL3
Definition: SPI_Slave_Dma_main.c:211
SPI_CS_SLAVE_DEMO_MODE
#define SPI_CS_SLAVE_DEMO_MODE
Definition: SPI_Slave_Dma_main.c:217
main
int main(void)
Main program code.
Definition: SPI_Slave_Dma_main.c:239
TOGGLE_DL2
#define TOGGLE_DL2
Definition: SPI_Slave_Dma_main.c:210
SYST_CLOCK
#define SYST_CLOCK
Definition: SPI_Slave_Dma_main.c:213
DMAUart_Sending
void DMAUart_Sending(uint32_t buffer, uint32_t size)
Definition: SPI_Slave_Dma_main.c:386
buffer_spi_tail
uint8_t buffer_spi_tail
SPI_CS_SLAVE_DEMO_PIN
#define SPI_CS_SLAVE_DEMO_PIN
Definition: SPI_Slave_Dma_main.c:216
TOGGLE_DL1
#define TOGGLE_DL1
Definition: SPI_Slave_Dma_main.c:209