API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
spi.c

Example showing how to use the SPI. The example shows how to send and receive data through the SPI.

/*
* @file spi.c
* @author TST
* @version V0.0
* @date 21/06/2012
* @brief Serial Peripheral Interface (SPI) example. This example shows how to use the SPI attached to a specific
* device. For this example, an external flash memory sst25vf040b is used.
*/
/* This file includes all the necessary headers (System, STM32, FreeRTOS, TS_Drivers, TS_Boards, TS_Devices
* and TS_Utils) so the program can work properly */
#include "tsmart.h"
/* Enums to enable/disable external flash */
typedef enum{
TSMART_EXT_FLASH_ENABLE,
TSMART_EXT_FLASH_DISABLE
}tsmart_ext_flash_state_t;
/*
* @brief TSMART_ChipEnable function. Function to enable or disable the external flash.
*
*/
int8_t TSMART_ChipEnable(tsmart_ext_flash_state_t TSMART_EXT_FLASH_new_state){
if (TSMART_EXT_FLASH_new_state == TSMART_EXT_FLASH_ENABLE){
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET);
}else{
GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET);
}
return TSMART_PASS;
}
/*
* Task function. Main process to use the SPI. This function shows how to get the external flash memory sst25vf040b
* identification through the SPI. SPI send and receive functions are used for this propose.
*/
void vSPI_EXAMPLE(void *pvParameters){
/* Variables */
uint8_t device_id = 0;
uint8_t command;
uint16_t id = 0;
tsmart_spi_config_t tsmart_spi_config;
/* SPI configuration. User must load the SPI configuration: SPI mode and its clock speed */
tsmart_spi_config.mode = TS_SPI_MODE_0;
tsmart_spi_config.clk_mode = TS_SPI_CLK_MODE_SLOW;
/* Chip enable is needed to activate the external flash */
TSMART_ChipEnable(TSMART_EXT_FLASH_ENABLE);
/* The protocol below is necessary to get the external flash ID, sending the indicated commands through the SPI */
/* Send through SPI */
/* send command 0x90 to get external flash ID */
command = 0x90;
TSMART_SPI_Send(&tsmart_spi1, &tsmart_spi_config, 1, &command, 1000/portTICK_RATE_MS);
/* send command 0x00 */
TSMART_SPI_Send(&tsmart_spi1, &tsmart_spi_config, 1, (uint8_t *) 0x00, 1000/portTICK_RATE_MS);
/* send command 0x00 */
TSMART_SPI_Send(&tsmart_spi1, &tsmart_spi_config, 1, (uint8_t *) 0x00, 1000/portTICK_RATE_MS);
/* send address 0x00 */
TSMART_SPI_Send(&tsmart_spi1, &tsmart_spi_config, 1, (uint8_t *) 0x00, 1000/portTICK_RATE_MS);
/* Receive through SPI */
/* Get devices identifier (ID) */
TSMART_SPI_Receive(&tsmart_spi1, &tsmart_spi_config, 1, &device_id, 1000/portTICK_RATE_MS);
/* Disable external flash memory */
TSMART_ChipEnable(TSMART_EXT_FLASH_DISABLE);
/* Device identifier stored in "id" */
id = device_id;
while(1);
}
/*
* @brief init_resources() function
*
* This function initializes all the necessary resources for the external flash memory sst25vf040b.
*/
void TSMART_InitResources(){
/* Initialize external flash GPIOs */
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* All pins work at 2MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
/* Chip Select PIN defined as output push pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*
* @brief init() function
*
* This is the first thing that the user must do for using TSmarT.
* It initializes the specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* Function to initialize all the necessary resources for this example.
* An external flash memory included in the board is used to show the SPI operation.
* These resources will be different if another device is used, in this case, user must initialize the necessary
* resources for the new device */
TSMART_InitResources();
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* Initialize SPI. Interface SPI1 is selected */
/* Create SPI task */
if(xTaskCreate(vSPI_EXAMPLE, "SPI_EXAMPLE", 256, NULL, 7, NULL) != pdPASS){
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}