/*!
 * \file
 *    thermostat_shield.h
 * \brief
 *    Nucleo thermostat shield port file. This file contain the implementation of driver
 *    calls for the shield.
 *
 * Author: Christos Choutouridis AEM: 8997
 * email : <cchoutou@ece.auth.gr>
 */

#ifndef DRIVERS_THERMOSTAT_SHIELD_H_
#define DRIVERS_THERMOSTAT_SHIELD_H_

#include "NUCLEO_F401RE.h"
#include "driver_types.h"
#include "deque08.h"

/*
 * =============== Digital I/O ===============
 */

/*
 * =============== LCD ===============
 * LCD_BD4  -- D8  -- PA9
 * LCD_BD5  -- D7  -- PA8
 * LCD_BD6  -- D6  -- PB10
 * LCD_BD7  -- D5  -- PB4
 * LCD_RS   -- D15 -- PB8
 * LCD_EN   -- D14 -- PB9
 * LCD_BL   -- D2  -- PA10
 */
void SHIELD_LCD_DB4 (uint8_t en);
void SHIELD_LCD_DB5 (uint8_t en);
void SHIELD_LCD_DB6 (uint8_t en);
void SHIELD_LCD_DB7 (uint8_t en);
void SHIELD_LCD_RS (uint8_t en);
void SHIELD_LCD_EN (uint8_t en);
void SHIELD_LCD_BL (uint8_t en);

/*
 * =============== LED ===============
 * LED0 -- D4 -- PB5
 * LED1 -- D3 -- PB3
 */
void SHIELD_LED0 (uint8_t on);
void SHIELD_LED1 (uint8_t on);

#define LED_RED      SHIELD_LED0
#define LED_GREEN    SHIELD_LED1

/*
 * =============== BRIDGE ===============
 * BR1 -- D10 -- PB6
 * BR2 -- D9  -- PC7
 */
void SHIELD_BR1 (uint8_t on);
void SHIELD_BR2 (uint8_t on);


/*
 *  =============== SR04 ===============
 *  TRIG -- D11 -- PA7
 *  ECHO -- D12 -- PA6
 */
void SHIELD_TRIG (uint8_t on);
uint8_t SHIELD_ECHO (void);

/*
 * ============= 1 Wire USART6 (AF08)===============
 *  1W_Tx   -- PA11 (OD+PU)
 *  1W_Rx   -- PA12 (I)
 */
// non-shared resources
#define  _1WIRE_UART_INSTANCE          (USART6)
#define  _1WIRE_UART_INIT_BR           (9600)
#define  _1WIRE_UART_TIMEOUT           (10)     /*!< 10msec is good enough */
/*!
 * Helper Macros linked to USART used
 */
#define _1WIRE_UART_CLK_ENABLE       __HAL_RCC_USART6_CLK_ENABLE
#define _1WIRE_UART_CLK_DISABLE      __HAL_RCC_USART6_CLK_DISABLE

LLD_Status_en SHIELD_1W_Init (void);

drv_status_en SHIELD_1W_UART_BR (uint32_t br);
uint16_t SHIELD_1W_RW (uint8_t byte);
uint8_t  SHIELD_1W_Rx (void);
uint8_t  SHIELD_1W_Tx (uint8_t byte);

LLD_Status_en SHIELD_Init (void);


/*
 * ============= Serial console UART2 (AF07)===============
 *  COM_Tx -- PA2 (PP)
 *  COM_Rx -- PA3 (I)
 */
#define COM_UART_INSTANCE          USART2
#define COM_UART_IRQ               USART2_IRQn
#define COM_UART_BAUDRATE          (57600)
#define COM_UART_GETCHAR_TIMEOUT   (5)         // 5sec
#define COM_UART_PUTCHAR_TIMEOUT   (10)        // 1 [CPU time]

#define COM_BUFFER_SIZE            (256)

/*!
 * Helper Macros linked to USART used
 */
#define _COM_CLK_ENABLE      __HAL_RCC_USART2_CLK_ENABLE
#define _COM_CLK_DISNABLE    __HAL_RCC_USART2_CLK_DISABLE


LLD_Status_en COM_Init (void);
LLD_Status_en COM_baudrate (uint32_t br);
int COM_putchar (char c);
int COM_getchar (void);
int COM_Transmit (uint8_t *data, int size);
int COM_CheckReceive (void);
void COM_ReceiveFlush (void);
int COM_Receive (uint8_t *data, int size);


#endif /* DRIVERS_THERMOSTAT_SHIELD_H_ */
