//////////////////////////////////////////////////////////////////
//
// App:			API: TWI I/O Expander TCA6416
// Author:		Milan Korinek
// Version:		1.0
// Notice:		Abstract layer of I/O Expander TCA6416
//////////////////////////////////////////////////////////////////

#ifndef TWI_IO_EXPANDER_H_
#define TWI_IO_EXPANDER_H_

#include "TWI_IO_Expander_TCA6416.h"

extern const uint32_t _ports_pull_res_mask[];
extern const uint32_t _ports_pull_res_enable[];
extern const uint32_t _ports_pull_res_disable[];
extern const uint32_t _ports_pull_res_up_res[];
extern const uint32_t _ports_pull_res_down_res[];
extern const uint32_t _ports_dir_mask[];
extern const uint32_t _ports_dir_output[];
extern const uint32_t _ports_dir_input[];

#define get_ports_pull_res_mask(Port) (_ports_pull_res_mask[( Port )])
#define get_ports_pull_res_enable(Enable, Port) (( Enable ) ? _ports_pull_res_enable[( Port )] : _ports_pull_res_disable[( Port )])
#define get_ports_pull_res(PullUp, Port) (( PullUp ) ? _ports_pull_res_up_res[( Port )] : _ports_pull_res_down_res[( Port )])
#define get_ports_dir_mask(Port) (_ports_dir_mask[( Port )])
#define get_ports_dir(Output, Port) (( Output ) ? _ports_dir_output[( Port )] : _ports_dir_input[( Port )])

status_code_t freertos_io_expander_configure(void);

#define freertos_io_expander_init(interrupt_priority,  max_block_time_ticks) freertos_io_expander_tca6416_init(( interrupt_priority ), ( max_block_time_ticks ))

#define freertos_io_expander_run_led_configure() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_CONFIGURATION_PORT_0 ,IOEXP_RUN_LED_MASK, IOEXP_TCA6416_OUTPUT)
#define freertos_io_expander_run_led_on() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_OUTPUT_PORT_0 ,IOEXP_RUN_LED_MASK, IOEXP_RUN_LED_ACTIVE)
#define freertos_io_expander_run_led_off() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_OUTPUT_PORT_0 ,IOEXP_RUN_LED_MASK, ~IOEXP_RUN_LED_ACTIVE)
#define freertos_io_expander_run_led(light)														\
	(( ( light ) > 0 ) ? freertos_io_expander_run_led_on() : freertos_io_expander_run_led_off())	\
	
#define freertos_io_expander_ports_pwr_configure() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_CONFIGURATION_PORT_0 , IOEXP_PX_PWR_MASK | IOEXP_PX_OE_MASK, IOEXP_TCA6416_OUTPUT)
#define freertos_io_expander_ports_pwr_on() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_OUTPUT_PORT_0 ,IOEXP_PX_PWR_MASK | IOEXP_PX_OE_MASK, IOEXP_PX_PWR_ACTIVE | IOEXP_PX_OE_ACTIVE)
#define freertos_io_expander_ports_pwr_off() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_OUTPUT_PORT_0 ,IOEXP_PX_PWR_MASK | IOEXP_PX_OE_MASK, ~(IOEXP_PX_PWR_ACTIVE | IOEXP_PX_OE_ACTIVE))
#define freertos_io_expander_ports_pwr(power_on)															\
	(( ( power_on ) > 0 ) ? freertos_io_expander_ports_pwr_on() : freertos_io_expander_ports_pwr_off())	\
	
#define freertos_io_expander_ports_dir_configure() freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_CONFIGURATION_PORT_0 , IOEXP_PX_DIR_MASK, IOEXP_TCA6416_OUTPUT)
#define freertos_io_expander_ports_dir(ports_dir_mask, settings_flags) freertos_io_expander_tca6416_set(IOEXP_TCA6416_CMD_OUTPUT_PORT_0 ,( ports_dir_mask ), ( settings_flags ))
				
#define freertos_io_expander_ports_pull_res_configure() freertos_io_expander_ports_pull_res(IOEXP_PX_PULL_RES_MASK, IOEXP_PX_PULL_RES_ENABLE, IOEXP_PX_PULL_UP_RES)
status_code_t freertos_io_expander_ports_pull_res(uint32_t resistor_nets_mask, uint32_t enable_flags, uint32_t settings_flags);


#endif /* TWI_IO_EXPANDER_H_ */