Exemplo n.º 1
0
/*******************************************************************************
* Function Name  : SPI_FLASH_Init
* Description    : SPI FLASH Configuration
* Input          : None
* Output         : None
* Return         : None
* Attention		 : None
*******************************************************************************/
void SPI_FLASH_RTC_Init(void)
{
    SSP_CFG_Type SSP_ConfigStruct;
	/*
	 * Initialize SPI pin connect
	 * P2.19 - SSEL - used as GPIO
	 * P2.22 - SCK
	 * P2.26 - MISO
	 * P2.27 - MOSI
	 */

	PINSEL_ConfigPin(FLASH_CS_PORT_NUM, FLASH_CS_PIN_NUM, 0);	 /* P2.19 - GPIO */
//	PINSEL_ConfigPin(RTC_CS_PORT_NUM, RTC_CS_PIN_NUM, 0);	 /* P2.21 - GPIO */
	
	PINSEL_ConfigPin(RTC_FLASH_MOSI_PORT_NUM, RTC_FLASH_MOSI_PIN_NUM, RTC_FLASH_MOSI_FUN_NUM);	 /* SSP0_SCK */
	PINSEL_ConfigPin(RTC_FLASH_MISO_PORT_NUM, RTC_FLASH_MISO_PIN_NUM, RTC_FLASH_MISO_FUN_NUM);	 /* SSP0_MISO */
	PINSEL_ConfigPin(RTC_FLASH_SCK_PORT_NUM, RTC_FLASH_SCK_PIN_NUM, RTC_FLASH_SCK_FUN_NUM);	 /* SSP0_MOSI */

	PINSEL_SetPinMode(RTC_FLASH_MOSI_PORT_NUM,RTC_FLASH_MOSI_PIN_NUM,IOCON_MODE_PULLUP);
	PINSEL_SetPinMode(RTC_FLASH_MISO_PORT_NUM,RTC_FLASH_MISO_PIN_NUM,IOCON_MODE_PLAIN);
	/* P2.19 CS is output */
	GPIO_SetDir(FLASH_CS_PORT_NUM, (1<<FLASH_CS_PIN_NUM), 1);
	SPI_FLASH_CS_HIGH();
/* P2.21 CS is output */
//	GPIO_SetDir(RTC_CS_PORT_NUM, (1<<RTC_CS_PIN_NUM), 1);
//	SPI_RTC_CS_HIGH();
	/* initialize SSP configuration structure to default */
	SSP_ConfigStructInit(&SSP_ConfigStruct);
	
	SSP_ConfigStruct.CPHA = SSP_CPHA_FIRST;
	SSP_ConfigStruct.CPOL = SSP_CPOL_HI;
	//SSP_ConfigStruct.FrameFormat = SSP_FRAME_TI;
	//SSP_ConfigStruct.Databit = SSP_DATABIT_8;
	/* Initialize SSP peripheral with parameter given in structure above */
	SSP_ConfigStruct.ClockRate = 3000000;
	//SSP_ConfigStruct.Mode = SSP_MASTER_MODE;
	SSP_Init(RTC_FLASH_SSP, &SSP_ConfigStruct);
	/* Enable SSP peripheral */
	SSP_Cmd(RTC_FLASH_SSP, ENABLE);
}
Exemplo n.º 2
0
/*********************************************************************//**
 * @brief		EINT0 Interrupt Handler
 * @param[in]	None
 *
 * @return 		None
 **********************************************************************/
void EINT_BSP_Init(EINT_init_t *eint)
{
	//EXTI_InitTypeDef exti_cfg;
	if(eint == NULL)
		return;
	PINSEL_ConfigPin (eint->port, eint->pin, eint->func_num);
	PINSEL_SetPinMode(eint->port,eint->pin,IOCON_MODE_PLAIN);
	GPIO_SetDir(eint->port,1<<eint->pin,0);
	
		
//	exti_cfg.EXTI_Line = EXTI_EINT0;
//	exti_cfg.EXTI_Mode = EXTI_MODE_EDGE_SENSITIVE;
//	exti_cfg.EXTI_polarity = EXTI_POLARITY_HIGH_ACTIVE_OR_RISING_EDGE;
	if(eint->exti_cfg == NULL)
		return;
	EXTI_Config(eint->exti_cfg);
	if(eint->irq_msg == NULL)
		return;
	if(eint->eint_num == 0)
	{
		//EINT0_msg_p = eint->irq_msg;
		memcpy(&EINT0_msg_p,eint->irq_msg,sizeof(IRQ_msg_t));
		NVIC_EnableIRQ(EINT0_IRQn);
	}
	else if(eint->eint_num == 1)
	{
		memcpy(&EINT1_msg_p,eint->irq_msg,sizeof(IRQ_msg_t));
		NVIC_EnableIRQ(EINT1_IRQn);
	}
	else if(eint->eint_num == 2)
	{
		memcpy(&EINT2_msg_p,eint->irq_msg,sizeof(IRQ_msg_t));
		NVIC_EnableIRQ(EINT2_IRQn);
	}
	else if(eint->eint_num == 3)
	{
		memcpy(&EINT3_msg_p,eint->irq_msg,sizeof(IRQ_msg_t));
		NVIC_EnableIRQ(EINT3_IRQn);
	}
}
Exemplo n.º 3
0
/*
TX_EN     P1_4
TXD0      P1_0
TXD1      P1_1

RXD0      P1_9
RXD1      P1_10
RX_ER     P1_14
CRS_DV    P1_8

MDC       P1_16
MDIO      P1_17

PHY_RESET P3_19
REF_CLK   P1_15
*/
static rt_err_t lpc17xx_emac_init(rt_device_t dev)
{
	/* Initialize the EMAC ethernet controller. */
	rt_uint32_t regv, tout;

	/* Power Up the EMAC controller. */
	LPC_SC->PCONP |= (1UL<<30);

	/* config RESET */
	PINSEL_ConfigPin(3, 19, 0);
	PINSEL_SetPinMode(3, 19, IOCON_MODE_PLAIN);
	LPC_GPIO3->DIR |= 1<<19;
	LPC_GPIO3->CLR = 1<<19;

	/* Enable P1 Ethernet Pins. */
	PINSEL_ConfigPin(1, 0, 1);  /**< P1_0  ENET_TXD0 */
	PINSEL_ConfigPin(1, 1, 1);  /**< P1_1  ENET_TXD1 */
	PINSEL_ConfigPin(1, 4, 1);  /**< P1_4  ENET_TX_EN */
	PINSEL_ConfigPin(1, 8, 1);  /**< P1_8  ENET_CRS_DV */
	PINSEL_ConfigPin(1, 9, 1);  /**< P1_9  ENET_RXD0 */
	PINSEL_ConfigPin(1, 10, 1); /**< P1_10 ENET_RXD1 */
	PINSEL_ConfigPin(1, 14, 1); /**< P1_14 ENET_RX_ER */
	PINSEL_ConfigPin(1, 15, 1); /**< P1_15 ENET_REF_CLK */
	PINSEL_ConfigPin(1, 16, 1); /**< P1_16 ENET_MDC */
	PINSEL_ConfigPin(1, 17, 1); /**< P1_17 ENET_MDIO */

	LPC_GPIO3->SET = 1<<19;

	/* Reset all EMAC internal modules. */
	LPC_EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX |
				 MAC1_SIM_RES | MAC1_SOFT_RES;
	LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES;

	/* A short delay after reset. */
	for (tout = 100; tout; tout--);

	/* Initialize MAC control registers. */
	LPC_EMAC->MAC1 = MAC1_PASS_ALL;
	LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
	LPC_EMAC->MAXF = ETH_MAX_FLEN;
	LPC_EMAC->CLRT = CLRT_DEF;
	LPC_EMAC->IPGR = IPGR_DEF;

	/* PCLK=18MHz, clock select=6, MDC=18/6=3MHz */
	/* Enable Reduced MII interface. */
	LPC_EMAC->MCFG = MCFG_CLK_DIV20 | MCFG_RES_MII;
	for (tout = 100; tout; tout--);
	LPC_EMAC->MCFG = MCFG_CLK_DIV20;

	/* Enable Reduced MII interface. */
	LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM | CR_PASS_RX_FILT;

	/* Reset Reduced MII Logic. */
	LPC_EMAC->SUPP = SUPP_RES_RMII | SUPP_SPEED;
	for (tout = 100; tout; tout--);
	LPC_EMAC->SUPP = SUPP_SPEED;

	/* Put the PHY in reset mode */
	write_PHY (PHY_REG_BMCR, 0x8000);
	for (tout = 1000; tout; tout--);

//	/* Wait for hardware reset to end. */
//	for (tout = 0; tout < 0x100000; tout++)
//	{
//		regv = read_PHY (PHY_REG_BMCR);
//		if (!(regv & 0x8000))
//		{
//			/* Reset complete */
//			break;
//		}
//	}
//	if (tout >= 0x100000)
//    {
//        rt_kprintf("reset failed\r\n");
//        return -RT_ERROR; /* reset failed */
//    }

//	/* Check if this is a DP83848C PHY. */
//	id1 = read_PHY (PHY_REG_IDR1);
//	id2 = read_PHY (PHY_REG_IDR2);
//
//	if (((id1 << 16) | (id2 & 0xFFF0)) != DP83848C_ID)
//		return -RT_ERROR;

	/* Configure the PHY device */
	/* Configure the PHY device */
	switch (lpc17xx_emac_device.phy_mode)
	{
	case EMAC_PHY_AUTO:
		/* Use autonegotiation about the link speed. */
		write_PHY (PHY_REG_BMCR, PHY_AUTO_NEG);
		/* Wait to complete Auto_Negotiation. */
//		for (tout = 0; tout < 0x100000; tout++)
//		{
//			regv = read_PHY (PHY_REG_BMSR);
//			if (regv & 0x0020)
//			{
//				/* Autonegotiation Complete. */
//				break;
//			}
//		}
		break;
	case EMAC_PHY_10MBIT:
		/* Connect at 10MBit */
		write_PHY (PHY_REG_BMCR, PHY_FULLD_10M);
		break;
	case EMAC_PHY_100MBIT:
		/* Connect at 100MBit */
		write_PHY (PHY_REG_BMCR, PHY_FULLD_100M);
		break;
	}
	if (tout >= 0x100000) return -RT_ERROR; // auto_neg failed

//	/* Check the link status. */
//	for (tout = 0; tout < 0x10000; tout++)
//	{
//		regv = read_PHY (PHY_REG_STS);
//		if (regv & 0x0001)
//		{
//			/* Link is on. */
//			break;
//		}
//	}
//	if (tout >= 0x10000) return -RT_ERROR;

	regv = 0x0004;
	/* Configure Full/Half Duplex mode. */
	if (regv & 0x0004)
	{
		/* Full duplex is enabled. */
		LPC_EMAC->MAC2    |= MAC2_FULL_DUP;
		LPC_EMAC->Command |= CR_FULL_DUP;
		LPC_EMAC->IPGT     = IPGT_FULL_DUP;
	}
	else
	{
		/* Half duplex mode. */
		LPC_EMAC->IPGT = IPGT_HALF_DUP;
	}

	/* Configure 100MBit/10MBit mode. */
	if (regv & 0x0002)
	{
		/* 10MBit mode. */
		LPC_EMAC->SUPP = 0;
	}
	else
	{
		/* 100MBit mode. */
		LPC_EMAC->SUPP = SUPP_SPEED;
	}

	/* Set the Ethernet MAC Address registers */
	LPC_EMAC->SA0 = (lpc17xx_emac_device.dev_addr[1]<<8) | lpc17xx_emac_device.dev_addr[0];
	LPC_EMAC->SA1 = (lpc17xx_emac_device.dev_addr[3]<<8) | lpc17xx_emac_device.dev_addr[2];
	LPC_EMAC->SA2 = (lpc17xx_emac_device.dev_addr[5]<<8) | lpc17xx_emac_device.dev_addr[4];

	/* Initialize Tx and Rx DMA Descriptors */
	rx_descr_init ();
	tx_descr_init ();

	/* Receive Broadcast and Perfect Match Packets */
	LPC_EMAC->RxFilterCtrl = RFC_BCAST_EN | RFC_PERFECT_EN;

	/* Reset all interrupts */
	LPC_EMAC->IntClear  = 0xFFFF;

	/* Enable EMAC interrupts. */
	LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE | INT_WAKEUP | INT_SOFT_INT | INT_RX_OVERRUN | INT_TX_UNDERRUN;

	/* Enable receive and transmit mode of MAC Ethernet core */
	LPC_EMAC->Command  |= (CR_RX_EN | CR_TX_EN);
	LPC_EMAC->MAC1     |= MAC1_REC_EN;

	/* Enable the ENET Interrupt */
	NVIC_EnableIRQ(ENET_IRQn);

	return RT_EOK;
}
Exemplo n.º 4
0
/*********************************************************************//**
 * @brief       c_entry: Main program body
 * @param[in]   None
 * @return      int
 **********************************************************************/
int c_entry(void)
{
    I2C_M_SETUP_Type transferMCfg;
    uint32_t tempp;
    uint8_t *pdat;

    /* Initialize debug via UART0
     * – 115200bps
     * – 8 data bit
     * – No parity
     * – 1 stop bit
     * – No flow control
     */
    debug_frmwrk_init();

    print_menu();


    /* I2C block ------------------------------------------------------------------- */

    /*
     * Init I2C pin connect
     */
#if ((I2CDEV_M == 0))
    PINSEL_ConfigPin (5, 2, 5);
    PINSEL_ConfigPin (5, 3, 5);
#elif ((I2CDEV_M == 1))
    PINSEL_ConfigPin (0, 19, 3);
    PINSEL_ConfigPin (0, 20, 3);
    PINSEL_SetOpenDrainMode(0, 19, ENABLE);
    PINSEL_SetOpenDrainMode(0, 20, ENABLE);
    PINSEL_SetPinMode(0, 19, PINSEL_BASICMODE_PLAINOUT);
    PINSEL_SetPinMode(0, 20, PINSEL_BASICMODE_PLAINOUT);
#elif ((I2CDEV_M == 2)&& (_CURR_USING_OEM_BRD != LPC4088_OEM_BOARD))
    PINSEL_ConfigPin (0, 10, 2);
    PINSEL_ConfigPin (0, 11, 2);
    PINSEL_SetOpenDrainMode(0, 10, ENABLE);
    PINSEL_SetOpenDrainMode(0, 11, ENABLE);
    PINSEL_SetPinMode(0, 10, PINSEL_BASICMODE_PLAINOUT);
    PINSEL_SetPinMode(0, 11, PINSEL_BASICMODE_PLAINOUT);
#else
    #error "Please choose the correct peripheral."
#endif

    // Initialize Slave I2C peripheral
    I2C_Init((en_I2C_unitId)I2CDEV_M, 100000);

    /* Enable Slave I2C operation */
    I2C_Cmd((en_I2C_unitId)I2CDEV_M, I2C_MASTER_MODE, ENABLE);

    /* Transmit -------------------------------------------------------- */
    _DBG_("Press '1' to transmit");
    while (_DG != '1');


    /* Initialize buffer */
    Buffer_Init(1);
     _DBG_("Transmit Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = Master_Buf;
    transferMCfg.tx_length = sizeof(Master_Buf);
    transferMCfg.rx_data = NULL;
    transferMCfg.rx_length = 0;
    transferMCfg.retransmissions_max = 3;
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif


    /* Receive -------------------------------------------------------- */
    _DBG_("Press '2' to receive");
    while(_DG != '2');

    /* Initialize buffer */
    Buffer_Init(0);

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = NULL ;
    transferMCfg.tx_length = 0;
    transferMCfg.rx_data = Master_Buf;
    transferMCfg.rx_length = sizeof(Master_Buf);
    transferMCfg.retransmissions_max = 3;
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif

    pdat = (uint8_t*)Master_Buf;
    _DBG_("Receive Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));

    // Verify
    for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
        if (*pdat != (tempp)){
                 _DBG_("Verify error ");
             break;
        }
    pdat++;
    }
    if (tempp == sizeof(Master_Buf)){
        _DBG_("Verify successfully");
    }

    /* Transmit and receive -------------------------------------------------------- */
    _DBG_("Press '3' to Transmit, then repeat start and receive...");
    while (_DG != '3');

    /* Initialize buffer */
    Buffer_Init(0);
    master_test[0] = 0xAA;
    master_test[1] = 0x55;

    /* Start I2C slave device first */
    transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
    transferMCfg.tx_data = master_test ;
    transferMCfg.tx_length = sizeof(master_test);
    transferMCfg.rx_data = Master_Buf;
    transferMCfg.rx_length = sizeof(Master_Buf);
    transferMCfg.tx_count = 0;
    transferMCfg.rx_count = 0;
    transferMCfg.retransmissions_count = 0;
    transferMCfg.retransmissions_max = 3;
     #if (I2CDEV_TRANSFER_POLLING == 0)
    complete = FALSE;
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_INTERRUPT);
     while(!complete) ;
     #else
    I2C_MasterTransferData((en_I2C_unitId)I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
    #endif

    pdat = (uint8_t*)Master_Buf;
    _DBG_("Transmit Data: ");
    Buffer_Print((uint8_t*)master_test, sizeof(master_test));
    _DBG_("Receive Data: ");
    Buffer_Print((uint8_t*)Master_Buf, sizeof(Master_Buf));
    // Verify
    for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
        if (*pdat++ != tempp){
            _DBG_("Verify error: ");
            break;
        }
    }
    if (tempp == sizeof(Master_Buf)){
        _DBG_("Verify successfully");
    }
    I2C_DeInit((en_I2C_unitId)I2CDEV_M);
     /* Loop forever */
    while(1);
}