/** @file 
* @brief Interface for hal_aci_tl. 
*/ 
  
/** @defgroup hal_aci_tl hal_aci_tl 
@{ 
@ingroup hal 
  
@brief Module for the ACI Transport Layer interface 
@details This module is responsible for sending and receiving messages over the ACI interface of the nRF8001 chip. 
The hal_aci_tl_send_cmd() can be called directly to send ACI commands. 
  
  
The RDYN line is hooked to an interrupt on the MCU when the level is low. 
The SPI master clocks in the interrupt context. 
The ACI Command is taken from the head of the command queue is sent over the SPI 
and the received ACI event is placed in the tail of the event queue. 
*/ 
#ifndef HAL_ACI_TL_H__
#define HAL_ACI_TL_H__

#include "aci.h"
#include "stm32f4xx.h"

/**
* @brief	map for pinout, platform specific in this case, stm32f4xx
*/
#define HAL_ACI_SPI_CH			SPI4
#define HAL_ACI_SPI_BASE		GPIOE
#define HAL_ACI_SPI_SCK			2							//GPIO_Pin_2 = (uint16_t)(1<<2), GPIO_PinSource2 = (uint8_t)0x02
#define HAL_ACI_SPI_MISO		5							//GPIO_Pin_5 = (uint16_t)(1<<5), GPIO_PinSource5 = (uint8_t)0x05
#define HAL_ACI_SPI_MOSI		6							//GPIO_Pin_6
#define HAL_ACI_SPI_REQN		4							//GPIO_Pin_4 = CS#

#define HAL_ACI_GPIO_BASE				GPIOB									//single port for GPIO for BLE
#define HAL_ACI_GPIO_RDYN_IRQn	EXTI4_IRQn						//IRQ number for RDYN (GPIO_Pin_4)
#define hal_aci_tl_rdyn_isr			EXTI4_IRQHandler			//RDYN ISR handler function
#define HAL_ACI_GPIO_RDYN		4													//GPIO_Pin_4	(RDYN pin is a GPIO pin), hardware change req.
#define HAL_ACI_GPIO_RESET	7													//GPIO_Pin_7


#ifndef HAL_ACI_MAX_LENGTH 
#define HAL_ACI_MAX_LENGTH 31 
#endif 

/** Data type for ACI commands and events */ 
typedef struct { 
	uint8_t status_byte; 
	uint8_t buffer[HAL_ACI_MAX_LENGTH+1]; 
} hal_aci_data_t; 

/**
* @brief	Datatype for ACI pins and interface (polling/interrupt)
* 				This is not used in ST platform. Just keep for compatibility
*/
typedef struct aci_pins_t
{
	uint8_t board_name;             //Optional : Use BOARD_DEFAULT if you do not know. See boards.h
	uint8_t	reqn_pin;								//Required
	uint8_t	rdyn_pin;								//Required
	uint8_t	mosi_pin;								//Required
	uint8_t	miso_pin;								//Required
	uint8_t	sck_pin;								//Required
	
	uint8_t spi_clock_divider;      //Required : Clock divider on the SPI clock : nRF8001 supports a maximum clock of 3MHz
	
	uint8_t	reset_pin;							//Recommended but optional - Set it to UNUSED when not connected
	uint8_t active_pin;							//Optional - Set it to UNUSED when not connected
	uint8_t optional_chip_sel_pin;  //Optional - Used only when the reqn line is required to be separate from the SPI chip select. Eg. Arduino DUE
	
	bool	interface_is_interrupt;		//Required - true = Uses interrupt on RDYN pin. false - Uses polling on RDYN pin
	
	uint8_t	interrupt_number;				//Required when using interrupts, otherwise ignored
	
}aci_pins_t;

/**
* @brief		ACI Transport Layer initialization.
* @details
*  This function initializes the transport layer, including configuring the SPI, creating 
*  message queues for Commands and Events and setting up interrupt if required. 
*  @param None
*/
void hal_aci_tl_init(void);


#endif
