CARME-M4 BSP  V1.5
spi.c

Explains how to communicate over the SPI bus.

/*
* This example shows how to read the temperature from the Temp. Sensor module
* over the SPI.
*/
#include <stm32f4xx.h>
#include <carme.h>
#include <carme_io2.h>
#define DS1722_CONFIG_ADDR 0x80 /* Configuration Register */
/*
* Defines for DS1722 configuration
* Choose a control byte for the DS1722.
* D7 = always 1
* D6 = always 1
* D5 = always 1
* D4 = one shot (0: continuous, 1: one shot)
* D3 = Resolution
* D2 = Resolution
* D1 = Resolution
* D0 = shut down (0: continuous, 1: shut down)
*/
#define DS1722_DEFAULT 0xE0 /* Bit 5, 6, 7 always 1 */
#define DS1722_1SHOT 0x10 /* Bit 4 one shot */
#define DS1722_RES_8BIT 0x00 /* Bit 1,2,3 Resolution 8Bit */
#define DS1722_RES_9BIT 0x02 /* Bit 1,2,3 Resolution 9Bit */
#define DS1722_RES_10BIT 0x04 /* Bit 1,2,3 Resolution 10Bit */
#define DS1722_RES_11BIT 0x06 /* Bit 1,2,3 Resolution 11Bit */
#define DS1722_RES_12BIT 0x08 /* Bit 1,2,3 Resolution 12Bit */
#define DS1722_SHUTDOWN (1<<0) /* Bit 0 shut down */
#define DS1722_TEMP_LSB 0x01 /* Data register Temp LSB */
#define DS1722_TEMP_MSB 0x02 /* Data register Temp MSB */
int main(void) {
uint16_t spiData[2];
uint8_t celsius;
/* Configure the temperature chip */
CARME_IO2_SPI_CS_Out(Bit_RESET); /* set CS low */
CARME_IO2_SPI_Send(DS1722_CONFIG_ADDR << 8 | DS1722_DEFAULT | DS1722_RES_9BIT);
CARME_IO2_SPI_CS_Out(Bit_SET); /* set CS high */
for (;;) {
/* Get the temperature */
CARME_IO2_SPI_Send(DS1722_TEMP_LSB);
CARME_IO2_SPI_Receive(&spiData[0]);
CARME_IO2_SPI_Send(DS1722_TEMP_MSB);
CARME_IO2_SPI_Receive(&spiData[1]);
celsius = ((spiData[1] << 8 | spiData[0]) >> 7) / 2;
}
return 0;
}