/*
 * hal_SPI_Flash.c
 *
 *  Created on: Nov 8, 2011
 *      Author: RomanM
 */

#include "hal_SPI_Flash.h"
#include "drivers/Periph/SPI/hal_SPI.h"

void hal_SPI_Flash_Init(void) {

	hal_SPI_Flash_RCC_Config();
	hal_SPI_Flash_GPIO_Config();
	hal_SPI_Init(hal_SPI_FLASH_SPIx);
	hal_SPI_FLASH_CS_HIGH();
}

void hal_SPI_Flash_RCC_Config(void) {
	RCC_APB2PeriphClockCmd(hal_SPI_FLASH_CS_CLK, ENABLE);
}

void hal_SPI_Flash_GPIO_Config(void) {
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = hal_SPI_FLASH_CS_PIN;	// CS pin
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	// Out push-pull
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(hal_SPI_FLASH_CS_PORT, &GPIO_InitStructure);
}

uint8_t hal_SPI_Flash_RW(uint8_t data) {
	return hal_SPI_RW(hal_SPI_FLASH_SPIx, data);
}

void hal_SPI_Flash_WriteN(uint8_t *outBuff, uint8_t n) {
	hal_SPI_WriteN(hal_SPI_FLASH_SPIx, outBuff, n);
}

void hal_SPI_Flash_ReadN(uint8_t *inBuff, uint8_t n) {
	hal_SPI_ReadN(hal_SPI_FLASH_SPIx, inBuff, n);
}

void hal_SPI_Flash_SendAddress(uint32_t address) {
	hal_SPI_Flash_RW((uint8_t)((address >> 16) & 0x000000ff));
	hal_SPI_Flash_RW((uint8_t)((address >> 8) & 0x000000ff));
	hal_SPI_Flash_RW((uint8_t)(address & 0x000000ff));
}
