/*******************************************************************************
* Function Name  : BACKUP_IRQHandler
* Description    : This function handles BACKUP global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void BACKUP_IRQHandler(void)
{
  uint32_t tmp;
  vuint32_t i, j;

  if (BKP_RTC_GetFlagStatus(BKP_RTC_FLAG_ALRF) == SET)
  {
    if (STOPModeStatus)
    {
      /* Wake-up from STOP mode is handled by an RTC Alarm */
      /* Disable SLEEPONEXIT mode */
      tmp = SCB->SCR;
      tmp &= ~SCB_SCR_SLEEPONEXIT_Msk;
      SCB->SCR = tmp;

      STOPModeStatus = 0;
    }
    else
      /* RTC Alarm handling */
      if (AlarmSetStatus)
      {
        for (i = 0; i < 5; i++)
        {
          PORT_SetBits(LEDs_PORT, LEDs_PINs);
          for (j = 0; j < 300000; j++)
          {
          }
          PORT_ResetBits(LEDs_PORT, LEDs_PINs);
          for (j = 0; j < 300000; j++)
          {
          }
        }
      }
  }
  if (BKP_RTC_GetFlagStatus(BKP_RTC_FLAG_SECF) == SET)
  {
    BKP_RTC_ITConfig(BKP_RTC_IT_SECF, DISABLE);

    /* If counter is equal to 86339: one day was elapsed */
    tmp = BKP_RTC_GetCounter();
    if ((tmp / 3600 == 23) &&
        (((tmp % 3600) / 60) == 59) &&
        (((tmp % 3600) % 60) == 59))
    {
      /* Wait until last write operation on RTC registers has finished */
      BKP_RTC_WaitForUpdate();
      /* Reset counter value */
      BKP_RTC_SetCounter(0);
      /* Wait until last write operation on RTC registers has finished */
      BKP_RTC_WaitForUpdate();

      /* Increment the date */
      Date_Update();
    }
    BKP_RTC_ITConfig(BKP_RTC_IT_SECF, ENABLE);
  }
}
示例#2
0
文件: main.c 项目: maxk9/etro_new
//==================================================================================
void LoadLED_Reg(uint8_t R1,uint8_t anod)
{
	uint8_t R0;
	
	REG_EN;
	
	for(R0=0;R0<8;++R0)
	{
		if(R1 & 1)
			PORT_SetBits(MDR_PORTB, PORT_Pin_10);
		else
			PORT_ResetBits(MDR_PORTB, PORT_Pin_10);
		
		R1 >>= 1;
		
		PORT_ResetBits(MDR_PORTB, PORT_Pin_8);
		__nop();
		__nop();
		PORT_SetBits(MDR_PORTB, PORT_Pin_8);
	}
	
	R1=1<<anod;
	
	for(R0=0;R0<8;++R0)
	{
		if(R1 & 1)
			PORT_ResetBits(MDR_PORTB, PORT_Pin_10);
		else
			PORT_SetBits(MDR_PORTB, PORT_Pin_10);
		
		R1 >>= 1;
		
		PORT_ResetBits(MDR_PORTB, PORT_Pin_8);
		__nop();
		__nop();
		PORT_SetBits(MDR_PORTB, PORT_Pin_8);
	}
	
	REG_DIS;
}
/**
  * @brief  Turns selected LED On.
  * @param  LED_Num: Specifies the Led to be set on.
  *   This parameter can be one of following parameters:
  *     @arg LED1
  *     @arg LED2
 * @retval None
  */
void LEDOn(uint32_t LED_Num)
{
  PORT_SetBits(MDR_PORTD, LED_Num);
}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main (void)
{
uint32_t Address;
uint32_t BankSelector;
uint32_t Data;
int i;
uint8_t EraseErrMM=0;
uint8_t WriteErrMM=0;
uint8_t EraseErrIM=0;
uint8_t WriteErrIM=0;

  /* Enables the clock on PORTD */
  RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTD, ENABLE);

  /* Enables the  clock on EEPROM */
  RST_CLK_PCLKcmd(RST_CLK_PCLK_EEPROM, ENABLE);

  /* Configure PORTD pins 10..14 for output to switch LEDs on/off */
  PORT_InitStructure.PORT_Pin   = (PORT_Pin_10 | PORT_Pin_11 | PORT_Pin_12 | PORT_Pin_13 | PORT_Pin_14);
  PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
  PORT_InitStructure.PORT_FUNC  = PORT_FUNC_PORT;
  PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
  PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
  PORT_Init(MDR_PORTD, &PORT_InitStructure);

  /* Erase main memory page 0 */
  Address = 0x08000000;
  BankSelector = 0;
  EEPROM_ErasePage (Address,BankSelector);

  /* Check main memory page 0 */
  Data = 0xFFFFFFFF;
  for (i=0;i<1024;i++)
  {
    if (EEPROM_ReadWord (Address+i*4,BankSelector) != Data) EraseErrMM = 1;
  }

  /* Indicate status of erasing main memory page 0*/
  if (EraseErrMM == 0)
  {
    PORT_SetBits(MDR_PORTD, PORT_Pin_10);
  }

  /* Fill main memory page 0 */
  Address = 0x08000000;
  BankSelector = 0;
  for (i=0;i<1024;i++)
  {
    Data = Pseudo_Rand (Address+i*4);
    EEPROM_ProgramWord (Address+i*4,BankSelector,Data);
  }

  /* Check main memory page 0 */
  Address = 0x08000000; BankSelector = 0;
  for (i=0;i<1024;i++)
  {
    Data = Pseudo_Rand (Address+i*4);
    if (EEPROM_ReadWord (Address+i*4,BankSelector) != Data)
    {
      WriteErrMM = 1;
    }
  }

  /* Indicate status of writing main memory page 0*/
  if (WriteErrMM == 0)
  {
    PORT_SetBits(MDR_PORTD, PORT_Pin_11);
  }

  /* Full Erase information memory */
  Address = 0x08000000;
  BankSelector = 1;
  EEPROM_EraseAllPages (BankSelector);

  /* Check information memory */
  Data = 0xFFFFFFFF;
  for (i=0;i<1024;i++)
  {
    if (EEPROM_ReadWord (Address+i*4,BankSelector) != Data)
    {
      EraseErrIM = 1;
    }
  }
  /* Indicate status of erasing information memory */
  if (EraseErrIM == 0)
  {
    PORT_SetBits(MDR_PORTD, PORT_Pin_12);
  }

  /* fill information memory */
  Address = 0x08000000;
  BankSelector = 1;
  for (i=0;i<1024;i++)
  {
    Data = Pseudo_Rand (Address+i*4);
    EEPROM_ProgramWord (Address+i*4,BankSelector,Data);
  }

  /* Check information memory */
  Address = 0x08000000;
  BankSelector = 1;
  for (i=0;i<1024;i++)
  {
    Data = Pseudo_Rand (Address+i*4);
    if(EEPROM_ReadWord (Address+i*4,BankSelector) != Data)
    {
      WriteErrIM = 1;
    }
  }

  /* Indicate status of writing information memory */
  if (WriteErrIM == 0)
  {
    PORT_SetBits(MDR_PORTD, PORT_Pin_13);
  }

  while (1)
  {
  }
}
示例#5
0
文件: main.c 项目: maxk9/etro_new
void prvSetupHardware( void )
{
	PORT_InitTypeDef PORT_InitStructure;
	UART_InitTypeDef UART_InitStructure;
	//SSP_InitTypeDef SPI_InitStructure;
	TIMER_CntInitTypeDef sTIM_CntInit;
//	TIMER_ChnInitTypeDef sTIM_ChnInit;
	

//CLK
/* Enable HSE clock oscillator */
	RST_CLK_HSEconfig(RST_CLK_HSE_ON);
	
	while(RST_CLK_HSEstatus() == ERROR);

	RST_CLK_CPUclkSelection(RST_CLK_CPUclkCPU_C3);
	MDR_RST_CLK -> CPU_CLOCK |= 2;//CPU_C1 set HSE
	RST_CLK_HSIcmd(DISABLE);
			
/* Enable the RTCHSE clock on all ports */
	RST_CLK_PCLKcmd(ALL_PORTS_CLK, ENABLE);
	PORT_StructInit(&PORT_InitStructure);//reset struct	
/************************ LCD Initialization *************************/

  /* Configure PORTA pins for data transfer to/from LCD */
	PORT_InitStructure.PORT_Pin = LCD_DATA_BUS_8;
	PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT;
	PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
	PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
	PORT_InitStructure.PORT_OE = PORT_OE_IN;

	PORT_Init(MDR_PORTA, &PORT_InitStructure);

	/* Configure PORTE pin4 and pin5 for LCD crystals control */
	PORT_InitStructure.PORT_Pin = KS0108_CS1 | KS0108_CS2 | KS0108_RS;
	PORT_InitStructure.PORT_OE = PORT_OE_OUT;
	PORT_Init(MDR_PORTE, &PORT_InitStructure);

	PORT_Init(MDR_PORTE, &PORT_InitStructure);

	PORT_InitStructure.PORT_Pin = KS0108_EN | KS0108_RW | KS0108_RES;

	PORT_Init(MDR_PORTC, &PORT_InitStructure);
	
	PORT_SetBits(MDR_PORTA, LCD_DATA_BUS_8);
	PORT_SetBits(MDR_PORTE, KS0108_CS1 | KS0108_CS2 | KS0108_RS);
	PORT_SetBits(MDR_PORTC, KS0108_EN | KS0108_RW | KS0108_RES);

//Timer1
	
	// TIMER1
	RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER1,ENABLE);
	/* Reset all TIMER1 settings */
	TIMER_DeInit(MDR_TIMER1);

	TIMER_BRGInit(MDR_TIMER1,TIMER_HCLKdiv1);

	/* TIM1 configuration ------------------------------------------------*/
	/* Initializes the TIMERx Counter ------------------------------------*/
	sTIM_CntInit.TIMER_IniCounter				= 0;
	sTIM_CntInit.TIMER_Prescaler                = 150;//
	sTIM_CntInit.TIMER_Period                   = 82;
	sTIM_CntInit.TIMER_CounterMode              = TIMER_CntMode_ClkFixedDir;
	sTIM_CntInit.TIMER_CounterDirection         = TIMER_CntDir_Up;
	sTIM_CntInit.TIMER_EventSource              = TIMER_EvSrc_None;
	sTIM_CntInit.TIMER_FilterSampling           = TIMER_FDTS_TIMER_CLK_div_1;
	sTIM_CntInit.TIMER_ARR_UpdateMode           = TIMER_ARR_Update_Immediately;
	sTIM_CntInit.TIMER_ETR_FilterConf           = TIMER_Filter_1FF_at_TIMER_CLK;
	sTIM_CntInit.TIMER_ETR_Prescaler            = TIMER_ETR_Prescaler_None;
	sTIM_CntInit.TIMER_ETR_Polarity             = TIMER_ETRPolarity_NonInverted;
	sTIM_CntInit.TIMER_BRK_Polarity             = TIMER_BRKPolarity_NonInverted;
	TIMER_CntInit (MDR_TIMER1,&sTIM_CntInit);

	NVIC_EnableIRQ(Timer1_IRQn);
	TIMER_ITConfig(MDR_TIMER1,TIMER_STATUS_CNT_ARR, ENABLE);
	
	/* TMR1 enable */
	TIMER_Cmd (MDR_TIMER1,ENABLE);

	
	// TIMER2
	RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER2,ENABLE);
	/* Reset all TIMER1 settings */
	TIMER_DeInit(MDR_TIMER2);

	TIMER_BRGInit(MDR_TIMER2,TIMER_HCLKdiv1);

	/* TIM2 configuration ------------------------------------------------*/
	/* Initializes the TIMERx Counter ------------------------------------*/
	sTIM_CntInit.TIMER_IniCounter				= 0;
	sTIM_CntInit.TIMER_Prescaler                = 0xf;//
	sTIM_CntInit.TIMER_Period                   = 0xffff;
	sTIM_CntInit.TIMER_CounterMode              = TIMER_CntMode_ClkFixedDir;
	sTIM_CntInit.TIMER_CounterDirection         = TIMER_CntDir_Up;
	sTIM_CntInit.TIMER_EventSource              = TIMER_EvSrc_None;
	sTIM_CntInit.TIMER_FilterSampling           = TIMER_FDTS_TIMER_CLK_div_1;
	sTIM_CntInit.TIMER_ARR_UpdateMode           = TIMER_ARR_Update_Immediately;
	sTIM_CntInit.TIMER_ETR_FilterConf           = TIMER_Filter_1FF_at_TIMER_CLK;
	sTIM_CntInit.TIMER_ETR_Prescaler            = TIMER_ETR_Prescaler_None;
	sTIM_CntInit.TIMER_ETR_Polarity             = TIMER_ETRPolarity_NonInverted;
	sTIM_CntInit.TIMER_BRK_Polarity             = TIMER_BRKPolarity_NonInverted;
	TIMER_CntInit (MDR_TIMER2,&sTIM_CntInit);

	NVIC_EnableIRQ(Timer2_IRQn);
	TIMER_ITConfig(MDR_TIMER2,TIMER_STATUS_CNT_ARR, ENABLE);
	
	/* TMR2 enable */
	TIMER_Cmd (MDR_TIMER2,ENABLE);
	
	
	// TIMER3
	RST_CLK_PCLKcmd(RST_CLK_PCLK_TIMER3,ENABLE);
	/* Reset all TIMER3 settings */
	TIMER_DeInit(MDR_TIMER3);

	TIMER_BRGInit(MDR_TIMER3,TIMER_HCLKdiv1);

  /* TIM3 configuration ------------------------------------------------*/
  /* Initializes the TIMERx Counter ------------------------------------*/
	sTIM_CntInit.TIMER_IniCounter				= 0;
	sTIM_CntInit.TIMER_Prescaler                = 0;
	sTIM_CntInit.TIMER_Period                   = 0xFFFF;
	sTIM_CntInit.TIMER_CounterMode              = TIMER_CntMode_EvtFixedDir;
	sTIM_CntInit.TIMER_CounterDirection         = TIMER_CntDir_Up;
	sTIM_CntInit.TIMER_EventSource              = TIMER_EvSrc_CH1;
	sTIM_CntInit.TIMER_FilterSampling           = TIMER_FDTS_TIMER_CLK_div_1;
	sTIM_CntInit.TIMER_ARR_UpdateMode           = TIMER_ARR_Update_Immediately;
	sTIM_CntInit.TIMER_ETR_FilterConf           = TIMER_Filter_1FF_at_TIMER_CLK;
	sTIM_CntInit.TIMER_ETR_Prescaler            = TIMER_ETR_Prescaler_None;
	sTIM_CntInit.TIMER_ETR_Polarity             = TIMER_ETRPolarity_NonInverted;
	sTIM_CntInit.TIMER_BRK_Polarity             = TIMER_BRKPolarity_NonInverted;
	TIMER_CntInit (MDR_TIMER3,&sTIM_CntInit);

	// 	NVIC_EnableIRQ(Timer3_IRQn);
	// 	TIMER_ITConfig(MDR_TIMER3,TIMER_STATUS_CNT_ARR, ENABLE);
	/* Enable TIMER3 */
	TIMER_Cmd(MDR_TIMER3,ENABLE);	
	
	
	
/************************ UART1 Initialization *************************/
	PORT_StructInit(&PORT_InitStructure);//reset struct

	PORT_InitStructure.PORT_Pin = PORT_Pin_12|PORT_Pin_13;//RX TX
	PORT_InitStructure.PORT_FUNC = PORT_FUNC_OVERRID;
	PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
	PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
	PORT_Init(MDR_PORTE, &PORT_InitStructure);

	PORT_InitStructure.PORT_Pin = PORT_Pin_14;//EN RS485
	PORT_InitStructure.PORT_OE =PORT_OE_OUT;
	PORT_InitStructure.PORT_PD = PORT_PD_OPEN;
	PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT;
	PORT_Init(MDR_PORTE, &PORT_InitStructure);

	RS485_TX_OFF;
//UART1
	
	RST_CLK_PCLKcmd(RST_CLK_PCLK_UART1,ENABLE);
	/* Set the HCLK division factor = 1 for UART1,UART2*/
	UART_BRGInit(MDR_UART1, UART_HCLKdiv1);
	UART_DeInit(MDR_UART1);
	
	/* Disable interrupt on UART1 */
    NVIC_DisableIRQ(UART1_IRQn);
	
	/* Initialize UART_InitStructure */
	UART_InitStructure.UART_BaudRate			= 38400;
	UART_InitStructure.UART_WordLength			= UART_WordLength8b;
	UART_InitStructure.UART_StopBits			= UART_StopBits1;//PKDU2 niobyi
	UART_InitStructure.UART_Parity				= UART_Parity_No;
	UART_InitStructure.UART_FIFOMode			= UART_FIFO_OFF;
	UART_InitStructure.UART_HardwareFlowControl	= UART_HardwareFlowControl_RXE | UART_HardwareFlowControl_TXE;

	/* Configure UART1 parameters*/
	UART_Init(MDR_UART1,&UART_InitStructure);

	NVIC_EnableIRQ(UART1_IRQn);
	UART_ITConfig(MDR_UART1,UART_IT_RX, ENABLE);
	/* Enables UART1 peripheral */
	UART_Cmd(MDR_UART1,ENABLE);
	
	
//PORT
/************************ Joystick Initialization *************************/

  /* Configure PORTC pins 10..14 for input to handle joystick events */
	PORT_StructInit(&PORT_InitStructure);//reset struct
	
	PORT_InitStructure.PORT_Pin   = ( PORT_Pin_10 | PORT_Pin_11 | PORT_Pin_12 |
									PORT_Pin_13 | PORT_Pin_14 );
	PORT_InitStructure.PORT_OE    = PORT_OE_IN;
	PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
	PORT_InitStructure.PORT_FUNC  = PORT_FUNC_PORT;
	PORT_InitStructure.PORT_SPEED = PORT_OUTPUT_OFF;

	PORT_Init( MDR_PORTC, &PORT_InitStructure );

/************************ LEDs Initialization *************************/
	PORT_StructInit( &PORT_InitStructure );//reset struct
  /* Configure PORTD pins 10..14 for output to switch LEDs on/off */
	PORT_InitStructure.PORT_Pin   = LEDs_PINs;
	PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
	PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
	PORT_InitStructure.PORT_FUNC  = PORT_FUNC_PORT;
	PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;

	PORT_Init( MDR_PORTD, &PORT_InitStructure );

  /* All LEDs switch off */
	PORT_ResetBits(MDR_PORTD, LEDs_PINs);
}
示例#6
0
//==============================================================//
// Select LCD (LCD0, LCD1)
// Hardware-specific function
//==============================================================//
void LcdSelect(uint8_t lcd)
{
	(lcd==LCD0) ? PORT_SetBits(MDR_PORTE, 1<<LCD_SEL) : PORT_ResetBits(MDR_PORTE, 1<<LCD_SEL);
	LcdDelayUs(20);
}
/*******************************************************************************
* Function Name  : Demo_Init
* Description    : Initializes the demonstration application
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Demo_Init(void)
{
  ClockConfigure();

/************************ LCD Initialization *************************/

  /* Configure LCD_DATA_PORT for data transfer to/from LCD */
  PortInitStructure.PORT_Pin = LCD_DATA_BAS_8_0;
  PortInitStructure.PORT_FUNC = PORT_FUNC_PORT;
  PortInitStructure.PORT_OE = PORT_OE_IN;
  PortInitStructure.PORT_SPEED = PORT_SPEED_SLOW;
  PortInitStructure.PORT_MODE = PORT_MODE_DIGITAL;

  PORT_Init(LCD_DATA_PORT_0, &PortInitStructure);
  PortInitStructure.PORT_Pin = LCD_DATA_BAS_8_1;
  PORT_Init(LCD_DATA_PORT_1, &PortInitStructure);

  /* Configure LCD_RD_WR_PORT for read/write control */
  /* Switch LCD into data output mode */
  PORT_SetBits(LCD_RD_WR_PORT, LCD_RD_WR_PIN);

  PortInitStructure.PORT_Pin = LCD_RD_WR_PIN;
  PortInitStructure.PORT_OE = PORT_OE_OUT;

  PORT_Init(LCD_RD_WR_PORT, &PortInitStructure);

  /* Configure LCD_CLOCK_PORT for CLOCK signal control*/
  /* Set LCD CLOCK signal to its initial value (0) */
  PORT_ResetBits(LCD_CLOCK_PORT, LCD_CLOCK_PIN);

  PortInitStructure.PORT_Pin = LCD_CLOCK_PIN;
  PortInitStructure.PORT_SPEED = PORT_SPEED_FAST;

  PORT_Init(LCD_CLOCK_PORT, &PortInitStructure);

  /* Configure LCD_CRYSTAL_PORT for LCD crystal control */
  /* De-select both LCD crystals*/
  PORT_ResetBits(LCD_CRYSTAL_PORT, LCD_CRYSTAL_PINs);

  PortInitStructure.PORT_Pin = LCD_CRYSTAL_PINs;
  PortInitStructure.PORT_SPEED = PORT_SPEED_SLOW;

  PORT_Init(LCD_CRYSTAL_PORT, &PortInitStructure);

  /* Configure LCD_CMD_DATA_PORT for data/command mode switching */
  PortInitStructure.PORT_Pin = LCD_CMD_DATA_PIN;

  PORT_Init(LCD_CMD_DATA_PORT, &PortInitStructure);

  /* Configure LCD_RESET_PORT for RESET signal control */
  /* Zeroing LCD RES signal (initial state) */
  PORT_ResetBits(LCD_RESET_PORT, LCD_RESET_PIN);

  PortInitStructure.PORT_Pin = LCD_RESET_PIN;
  PortInitStructure.PORT_SPEED = PORT_SPEED_FAST;

  PORT_Init(LCD_RESET_PORT, &PortInitStructure);

  LCD_INIT();

/************************ Joystick Initialization *************************/

  /* Configure SEL_PORT for input to handle joystick event SEL */
  PortInitStructure.PORT_Pin   = SEL_PIN;
  PortInitStructure.PORT_OE    = PORT_OE_IN;
  PortInitStructure.PORT_FUNC  = PORT_FUNC_PORT;
  PortInitStructure.PORT_SPEED = PORT_SPEED_SLOW;

  PORT_Init(SEL_PORT, &PortInitStructure);

  /* Configure UP_PORT for input to handle joystick event UP */
  PortInitStructure.PORT_Pin   = UP_PIN;

  PORT_Init(UP_PORT, &PortInitStructure);

  /* Configure DOWN_PORT for input to handle joystick event DOWN */
  PortInitStructure.PORT_Pin   = DOWN_PIN;

  PORT_Init(DOWN_PORT, &PortInitStructure);

  /* Configure LEFT_PORT for input to handle joystick event LEFT */
  PortInitStructure.PORT_Pin   = LEFT_PIN;

  PORT_Init(LEFT_PORT, &PortInitStructure);

  /* Configure RIGHT_PORT for input to handle joystick event RIGHT */
  PortInitStructure.PORT_Pin   = RIGHT_PIN;

  PORT_Init(RIGHT_PORT, &PortInitStructure);

/************************ LEDs Initialization *************************/

  /* Configure LEDs_PORT for output to switch LEDs on/off */
  PortInitStructure.PORT_Pin   = LEDs_PINs;
  PortInitStructure.PORT_OE    = PORT_OE_OUT;
  PortInitStructure.PORT_FUNC  = PORT_FUNC_PORT;

  PORT_Init(LEDs_PORT, &PortInitStructure);

  /* All LEDs switch off */
  PORT_ResetBits(LEDs_PORT, LEDs_PINs);
}
//-----------------------------------------------------------------//
// Setup IO Ports
//-----------------------------------------------------------------//
void HW_PortInit(void)
{
    PORT_InitTypeDef PORT_InitStructure;


    PORT_DeInit(MDR_PORTA);
    PORT_DeInit(MDR_PORTB);
    PORT_DeInit(MDR_PORTC);
    PORT_DeInit(MDR_PORTD);
    PORT_DeInit(MDR_PORTE);
    PORT_DeInit(MDR_PORTF);

    // default output value is 0
    // Set some outputs the default design values of 1
    //PORT_SetBits(MDR_PORTA, (1<<BUZ1) | (1<<BUZ2) );
    PORT_SetBits(MDR_PORTB, (1<<LGREEN) | (1<<LRED) );

    /*
       Reset PORT initialization structure parameters values
    PORT_InitStruct->PORT_Pin        = PORT_Pin_All;
    PORT_InitStruct->PORT_OE         = PORT_OE_IN;
    PORT_InitStruct->PORT_PULL_UP    = PORT_PULL_UP_OFF;
    PORT_InitStruct->PORT_PULL_DOWN  = PORT_PULL_DOWN_OFF;
    PORT_InitStruct->PORT_PD_SHM     = PORT_PD_SHM_OFF;
    PORT_InitStruct->PORT_PD         = PORT_PD_DRIVER;
    PORT_InitStruct->PORT_GFEN       = PORT_GFEN_OFF;
    PORT_InitStruct->PORT_FUNC       = PORT_FUNC_PORT;
    PORT_InitStruct->PORT_SPEED      = PORT_OUTPUT_OFF;
    PORT_InitStruct->PORT_MODE       = PORT_MODE_ANALOG;
    */

    //================= PORTA =================//
    PORT_StructInit(&PORT_InitStructure);

    // Typical digital inputs:
    PORT_InitStructure.PORT_Pin   = (1<<OVERLD) | (1<<ENC_BTN);
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);

    // Digital input with pull-up
    PORT_InitStructure.PORT_Pin   = (1<<EEN);
    PORT_InitStructure.PORT_PULL_UP  = PORT_PULL_UP_ON;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);

    // Typical digital outputs:
    PORT_InitStructure.PORT_Pin   = (1<<CLIM_SEL);
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);

    // Timer outputs to buzzer (TMR1.CH2, TMR1.CH2N)
    PORT_InitStructure.PORT_Pin   = (1<<BUZ1) | (1<<BUZ2);
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_ALTER;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);

    // USART1 pins
    PORT_StructInit(&PORT_InitStructure);
    // TX pin
    PORT_InitStructure.PORT_Pin   = 1<<TXD1;
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_OVERRID;
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);
    // RX pin
    PORT_InitStructure.PORT_Pin   = 1<<RXD1;
    PORT_InitStructure.PORT_OE    = PORT_OE_IN;
    PORT_Init(MDR_PORTA, &PORT_InitStructure);

    // debug
    /*	PORT_StructInit(&PORT_InitStructure);
    	PORT_InitStructure.PORT_Pin   = (1<<TXD1) | (1<<RXD1);
    	PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    	PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
    	PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    	PORT_Init(MDR_PORTA, &PORT_InitStructure); */

    //================= PORTB =================//
    PORT_StructInit(&PORT_InitStructure);

    // Typical digital inputs: buttons and encoder
    PORT_InitStructure.PORT_Pin   = (1<<SB_ESC) | (1<<SB_LEFT) | (1<<SB_RIGHT) | (1<<SB_OK) | (1<<SB_MODE) | (1<<ENCA) | (1<<ENCB) | (1<<PG) ;
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_Init(MDR_PORTB, &PORT_InitStructure);

    // Power good
    // TODO: add interrupt
    PORT_InitStructure.PORT_Pin   = (1<<PG) ;
    PORT_Init(MDR_PORTB, &PORT_InitStructure);

    // Leds and buttons SB_ON, SB_OFF
    PORT_InitStructure.PORT_Pin   = (1<<LGREEN) | (1<<LRED);
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
    PORT_Init(MDR_PORTB, &PORT_InitStructure);


    //================= PORTC =================//
    PORT_StructInit(&PORT_InitStructure);

    // LCD Backlight (TMR3.CH1)
    PORT_InitStructure.PORT_Pin   = (1<<LCD_BL);
    PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_ALTER;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
    PORT_Init(MDR_PORTC, &PORT_InitStructure);

    // I2C
    PORT_InitStructure.PORT_Pin = (1<<SCL) | (1<<SDA);
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
    PORT_Init(MDR_PORTC, &PORT_InitStructure);


    //================= PORTD =================//
    PORT_StructInit(&PORT_InitStructure);

    // Analog functions
    PORT_InitStructure.PORT_Pin   = (1<<VREF_P) | (1<<VREF_N) | (1<<TEMP_IN) | (1<<UADC) | (1<<IADC) ;
    PORT_Init(MDR_PORTD, &PORT_InitStructure);

    // LCD CLK and CS
    PORT_InitStructure.PORT_Pin   = (1<<LCD_CLK) | (1<<LCD_CS) ;
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_ALTER;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_Init(MDR_PORTD, &PORT_InitStructure);

    // MOSI
    PORT_InitStructure.PORT_Pin   = (1<<LCD_MOSI);
    PORT_InitStructure.PORT_PULL_DOWN = PORT_PULL_DOWN_ON;
    PORT_Init(MDR_PORTD, &PORT_InitStructure);



    //================= PORTE =================//
    PORT_StructInit(&PORT_InitStructure);

    // LCD RST and SEL, Load disable output
    PORT_InitStructure.PORT_Pin   = (1<<LCD_RST) | (1<<LCD_SEL) | (1<<LDIS);
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_Init(MDR_PORTE, &PORT_InitStructure);

    // cooler PWM output (TMR3.CH3N)
    PORT_InitStructure.PORT_Pin   = (1<<CPWM);
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_OVERRID;
    PORT_Init(MDR_PORTE, &PORT_InitStructure);

    // voltage and current PWM outputs (TMR2.CH1N, TMR2.CH3N)
    PORT_InitStructure.PORT_Pin   = (1<<UPWM) | (1<<IPWM);
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_ALTER;
    PORT_Init(MDR_PORTE, &PORT_InitStructure);



    //================= PORTF =================//
    PORT_StructInit(&PORT_InitStructure);

    // Feedback channel select, converter enable
    PORT_InitStructure.PORT_Pin   = (1<<EN) | (1<<STAB_SEL) ;
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_SLOW;
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_Init(MDR_PORTF, &PORT_InitStructure);

    // USART2 functions
    PORT_StructInit(&PORT_InitStructure);
    // TX pin
    PORT_InitStructure.PORT_Pin   = 1<<TXD2;
    PORT_InitStructure.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_InitStructure.PORT_SPEED = PORT_SPEED_FAST;
    PORT_InitStructure.PORT_FUNC  = PORT_FUNC_OVERRID;
    PORT_InitStructure.PORT_OE    = PORT_OE_OUT;
    PORT_Init(MDR_PORTF, &PORT_InitStructure);
    // RX pin
    PORT_InitStructure.PORT_Pin   = 1<<RXD2;
    PORT_InitStructure.PORT_OE    = PORT_OE_IN;
    PORT_Init(MDR_PORTF, &PORT_InitStructure);
}