Commit 95c89438 authored by Xavier Piroux's avatar Xavier Piroux

SD via SPI : now the basic is done (init of the card, need to test read and write functions)

parent cd73edf2
This diff is collapsed.
......@@ -53,7 +53,6 @@
#define SD_VOLUME_SIZE (2 * 1024 * 1024) /* definition for size of sddrive0 : we assume working with a 2GB micro SD card */
/**
* @brief micro SD initialization routine.
* @return > 0 means a success
......@@ -62,15 +61,35 @@ uint32_t sd_init(void);
/**
* @brief Controls power for the micro SD.
* @param uint8_t enable turns on the power if greater than 0.
* @param enable turns on the power if greater than 0.
*/
void sd_power(uint8_t enable);
/**
* @brief Set the number of bytes in a sector of the micro SD
* @param uint8_t bytes_per_sector number of bytes per sector
* @return the reply from the card (0 = success)
* @param bytes_per_sector number of bytes per sector
* @return > 0 means a success
*/
uint8_t sd_send_block_length(uint32_t bytes_per_sector);
/**
* @brief read Operating Condition register
* @param buffer space for 4 bytes
* @return > 0 means a success
*/
uint8_t sd_read_ocr(uint8_t* buffer);
/**
* @brief Write a block of byte to a given address of the SD card
* @param block_addr address of the block to write
* @param txBuffer block of data to write
* @return > 0 means a success
*/
int sd_write_block(uint32_t block_addr, const uint8_t *txBuffer);
/**
* @brief Read a block of byte at a given address of the SD card
* @param block_addr address of the block to read
* @param rxBuffer buffer where to store data to read
* @return > 0 means a success
*/
int sd_read_block(uint32_t block_addr, uint8_t *rxBuffer);
/**
* @brief enumeration for the possible type of replies coming from the micro SD
......
......@@ -126,9 +126,30 @@ C_SRC += \
../../common/emlib/src/em_usart.c \
../../common/emlib/src/em_assert.c \
../../common/emlib/src/em_gpio.c \
../../common/drivers/sd.c \
src/simple_spi_sd.c
#sources for using LCD :
INCLUDEPATHS += \
-I../../common/
C_SRC += \
../../common/drivers/i2cdrv.c \
../../common/drivers/light_sensor.c \
../../common/drivers/lcd.c \
../../common/drivers/sd.c \
../../common/udelay.c \
../../common/gfx/graphics.c \
../../common/gfx/font_helv11.c \
../../common/gfx/font_helv17.c \
../../common/gfx/font_helv17b.c \
../../common/gfx/font_xm4x5.c \
../../common/gfx/font_xm4x6.c \
../../common/emlib/src/em_burtc.c \
../../common/emlib/src/em_emu.c \
../../common/emlib/src/em_i2c.c \
../../common/emlib/src/em_int.c \
../../common/emlib/src/em_rtc.c \
s_SRC +=
S_SRC += \
......
......@@ -32,11 +32,15 @@
* arising from your use of this Software.
*
*****************************************************************************/
#include <stdio.h>
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "sd.h"
#include "lcd.h"
#include "i2cdrv.h"
#include <gfx/graphics.h>
/* Counts 1ms timeTicks */
volatile uint32_t msTicks;
......@@ -71,6 +75,7 @@ uint8_t sd_spi_transfer_byte_not_static(uint8_t txData);
#define sd_spi_init sd_spi_init_not_static
#define sd_spi_transfer_byte sd_spi_transfer_byte_not_static
uint32_t guiTracerSD = 0;//TODO: remove
/**************************************************************************//**
* @brief Main function
......@@ -79,37 +84,116 @@ int main(void)
{
int i;
uint8_t res;
/* Setup SysTick Timer for 1 msec interrupts */
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
//int x, y, i;
char str[20];
double temp = 0;
double pressure = 0;
double lux;
int i = 0;
uint8_t res_sd, res_sd_init;
uint8_t buffer_ocr[4];
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
//init SPI for microSD
sd_spi_init_not_static();
sd_power(1);
// Backlight LEDs
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
for (i =11; i < 13; ++i) {
GPIO_PinModeSet(gpioPortE, i, gpioModePushPull, 0);
}
lcd_init();
I2CDRV_Init(&i2cInit);
/* Infinite blink loop */
while (1) {
//this code is ok but not what we want (it only test spi protocol, we want now to test the SD)
//res = sd_spi_transfer_byte(0b00001111);
//res = sd_spi_transfer_byte(0b01010101);
//res = sd_spi_transfer_byte(0b00110011);
//send dummy stuff
res = sd_spi_transfer_byte(0xFF);
res++;
Delay(1);
for (i = 11; i < 13; ++i) {
GPIO_PinOutToggle(gpioPortE, i);
res = sd_spi_transfer_byte(0b00110011);
Delay(1);
//init SPI for microSD
res_sd_init = sd_init();
//sd_spi_init();
//sd_power(1);
while(1) {
i++;
lux = 102.03050 + i;
sprintf(str, "light: %3.3f lux", lux);
text(&font_helv11, 5, 10, str);
temp = 1122.3344 + i;
pressure = 12.34 + i;
sprintf(str, "temp: %f C", temp);
text(&font_helv11, 5, 30, str);
sprintf(str, "pressure: %f mbar", pressure);
text(&font_helv11, 5, 40, str);
sprintf(str, "sd_init() = %d", res_sd_init);
text(&font_helv11, 5, 60, str);
sprintf(str, "guiTracerSR = %u", (unsigned int) guiTracerSD);
text(&font_helv11, 5, 70, str);
res_sd = sd_read_ocr(buffer_ocr);
sprintf(str, "sd_read_ocr() = %u = %02X %02X %0X %02X", (unsigned int) res_sd,
buffer_ocr[0], buffer_ocr[1], buffer_ocr[2], buffer_ocr[3]);
text(&font_helv11, 5, 80, str);
GPIO_PinOutToggle(gpioPortE, 11);
GPIO_PinOutToggle(gpioPortE, 12);
lcd_update();
//Delay(1000);
box(0, 0, 128, 128, 0);
//lcd_clear();
Delay(1000);
if (res_sd_init) {
uint8_t data[SD_SECTOR_SIZE]= {0};
static uint32_t addr = 0;
sprintf(str, "sd_init() = %d", res_sd_init);
text(&font_helv11, 5, 10, str);
sprintf(str, "guiTracerSR = %u", (unsigned int) guiTracerSD);
text(&font_helv11, 5, 20, str);
#if 0
res_sd = sd_read_block(addr, data);
sprintf(str, "sd_read_block(%u) = %d", (unsigned int) addr, res_sd);
text(&font_helv11, 5, 30, str);
if (res_sd) {
int baseaddr = 0;
sprintf(str, "%2x %2x %2x %2x %2x %2x %2x %2x",
data[baseaddr + 0], data[baseaddr + 1], data[baseaddr + 2], data[baseaddr + 3],
data[baseaddr + 4], data[baseaddr + 5], data[baseaddr + 6], data[baseaddr + 7]);
text(&font_helv11, 5, 40, str);
baseaddr = 8;
sprintf(str, "%2x %2x %2x %2x %2x %2x %2x %2x",
data[baseaddr + 0], data[baseaddr + 1], data[baseaddr + 2], data[baseaddr + 3],
data[baseaddr + 4], data[baseaddr + 5], data[baseaddr + 6], data[baseaddr + 7]);
text(&font_helv11, 5, 50, str);
baseaddr = 16;
sprintf(str, "%2x %2x %2x %2x %2x %2x %2x %2x",
data[baseaddr + 0], data[baseaddr + 1], data[baseaddr + 2], data[baseaddr + 3],
data[baseaddr + 4], data[baseaddr + 5], data[baseaddr + 6], data[baseaddr + 7]);
text(&font_helv11, 5, 60, str);
baseaddr = 24;
sprintf(str, "%2x %2x %2x %2x %2x %2x %2x %2x",
data[baseaddr + 0], data[baseaddr + 1], data[baseaddr + 2], data[baseaddr + 3],
data[baseaddr + 4], data[baseaddr + 5], data[baseaddr + 6], data[baseaddr + 7]);
text(&font_helv11, 5, 70, str);
}
#else
res_sd = sd_write_block(addr, data);
sprintf(str, "sd_write_block(%u) = %d", (unsigned int) addr, res_sd);
text(&font_helv11, 5, 30, str);
#endif
addr += 512;
lcd_update();
box(0, 0, 128, 128, 0);
Delay(2000);
}
}
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment