Пример #1
0
void HardwareInit()
{
	g_Uart.Init(g_UartCfg);

	IOPinCfg(s_LedPins, s_NbLedPins);
	IOPinSet(BLUEIO_LED_BLUE_PORT, BLUEIO_LED_BLUE_PIN);
	IOPinSet(BLUEIO_LED_GREEN_PORT, BLUEIO_LED_GREEN_PIN);
	IOPinSet(BLUEIO_LED_RED_PORT, BLUEIO_LED_RED_PIN);

	IOPinCfg(s_ButPins, s_NbButPins);

	IOPinEnableInterrupt(0, APP_IRQ_PRIORITY_LOW, s_ButPins[0].PortNo, s_ButPins[0].PinNo, IOPINSENSE_LOW_TRANSITION, ButEvent);
}
Пример #2
0
void HardwareInit()
{
	// Set this only if nRF is power at 2V or more
	//nrf_power_dcdcen_set(true);
	NRF_POWER->DCDCEN = 1;

    IOPinCfg(s_GpioPins, s_NbGpioPins);

	IOPinClear(0, BLUEIO_TAG_BME680_LED2_BLUE_PIN);
	IOPinClear(0, BLUEIO_TAG_BME680_LED2_GREEN_PIN);
	IOPinClear(0, BLUEIO_TAG_BME680_LED2_RED_PIN);

	g_Timer.Init(s_TimerCfg);

	// Initialize I2C
#ifdef NEBLINA_MODULE
    g_Spi.Init(s_SpiCfg);
#else
    g_I2c.Init(s_I2cCfg);
#endif

	bsec_library_return_t bsec_status;

	// NOTE : For BME680 air quality calculation, this library is require to be initialized
	// before initializing the sensor driver.
	bsec_status = bsec_init();

	if (bsec_status != BSEC_OK)
	{
		printf("BSEC init failed\r\n");

		return;
	}

	// Inititalize sensor
    g_TphSensor.Init(s_TphSensorCfg, g_pIntrf, &g_Timer);

//    g_TphSensor.Disable();

//	g_I2c.Disable();

//	while(1) __WFE();


	if (g_TphSensor.DeviceID() == BME680_ID)
    {
    	g_GasSensor.Init(s_GasSensorCfg, g_pIntrf, NULL);
    }


    g_TphSensor.StartSampling();

	usDelay(300000);

    // Update sensor data
    TPHSENSOR_DATA tphdata;

    g_TphSensor.Read(tphdata);

    if (g_TphSensor.DeviceID() == BME680_ID)
    {
		GASSENSOR_DATA gdata;
		g_GasSensor.Read(gdata);
    }

	g_TphSensor.StartSampling();

	g_AdvData.Type = BLEADV_MANDATA_TYPE_TPH;
	// Do memcpy to adv data. Due to byte alignment, cannot read directly into
	// adv data
	memcpy(g_AdvData.Data, ((uint8_t*)&tphdata) + sizeof(tphdata.Timestamp), sizeof(BLEADV_MANDATA_TPHSENSOR));


	g_I2c.Disable();

#ifdef NRF52_SERIES
	g_Adc.Init(s_AdcCfg);
	g_Adc.OpenChannel(s_ChanCfg, s_NbChan);
	g_Adc.StartConversion();
#endif

#ifdef USE_TIMER_UPDATE
	// Only with SDK14

	uint64_t period = g_Timer.EnableTimerTrigger(0, 500UL, TIMER_TRIG_TYPE_CONTINUOUS);
#endif
}
Пример #3
0
bool SPIInit(SPIDEV *pDev, const SPICFG *pCfgData)
{
	SSPDEV *dev = NULL;

	IOPinCfg(pCfgData->IOPinMap, SPI_MAX_NB_IOPIN);

	if (pCfgData->DevNo == 0)
	{
		LPC_SYSCON->PRESETCTRL |= LPC11U_PRESETCTRL_SSP0_RST;
		LPC_SYSCON->SYSAHBCLKCTRL |= LPC11U_SYSAHBCLKCTRL_SSP0_EN;
		LPC_SYSCON->SSP0CLKDIV = 1;//0x2;			// Divided by 1

		dev = &g_SspDev[0];
		dev->pSspReg = (LPCSSPREG*)LPC_SSP0;
	}
	else if (pCfgData->DevNo == 1)
	{
		LPC_SYSCON->PRESETCTRL |= LPC11U_PRESETCTRL_SSP1_RST;
		LPC_SYSCON->SYSAHBCLKCTRL |= LPC11U_SYSAHBCLKCTRL_SSP1_EN;
		LPC_SYSCON->SSP1CLKDIV = 1;//0x02;			// Divided by 1

		dev = &g_SspDev[1];
		dev->pSspReg = (LPCSSPREG*)LPC_SSP1;
	}

	dev->PClkFreq = SystemCoreClock / LPC_SYSCON->SSP1CLKDIV;

	uint32_t d = (pCfgData->DataSize - 1);
	if (pCfgData->DataPhase == SPIDATAPHASE_SECOND_CLK)
		d |= LPCSSP_CR0_CPHA_SECOND;

	if (pCfgData->ClkPol == SPICLKPOL_HIGH)
		d |= LPCSSP_CR0_CPOL_HI;

	dev->pSspReg->CR0 = d | LPCSSP_CR0_FRF_SPI;

	d = LPCSSP_CR1_SSP_EN;

	if (pCfgData->Mode == SPIMODE_SLAVE)
		d |= LPCSSP_CR1_MS_SLAVE;

	dev->pSspReg->CR1 = d;
	dev->pSspReg->CPSR = 2;
	/* Enable AHB clock to the GPIO domain. */
	LPC_SYSCON->SYSAHBCLKCTRL |= LPC11U_SYSAHBCLKCTRL_GPIO_EN;
	//LPC_SSP1->IMSC = SSPIMSC_RORIM | SSPIMSC_RTIM;

	// Flush RX FIFO
	while (LpcSSPWaitRxFifo(dev, 2))
	{
		uint32_t d = dev->pSspReg->DR;
	}

	dev->pSpiDev = pDev;
	pDev->SerIntrf.pDevData = (void*)dev;
	pDev->Cfg = *pCfgData;
	pDev->SerIntrf.Disable = LpcSSPDisable;
	pDev->SerIntrf.Enable = LpcSSPEnable;
	pDev->SerIntrf.GetRate = LpcSSPGetRate;
	pDev->SerIntrf.SetRate = LpcSSPSetRate;
	pDev->SerIntrf.StartRx = LpcSSPStartRx;
	pDev->SerIntrf.RxData = LpcSSPRxData;
	pDev->SerIntrf.StopRx = LpcSSPStopRx;
	pDev->SerIntrf.StartTx = LpcSSPStartTx;
	pDev->SerIntrf.TxData = LpcSSPTxData;
	pDev->SerIntrf.StopTx = LpcSSPStopTx;

	LpcSSPSetRate(&pDev->SerIntrf, pCfgData->Rate);

	return true;
}
Пример #4
0
bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
{
	LPCUARTREG *reg = NULL;
	//g_UartClkDiv = 1;

	switch (pCfg->DevNo)
	{
		case 0:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART0_EN;
	        reg = (void*)LPC_USART;
	        LPC_SYSCON->UARTCLKDIV = 2;//g_UartClkDiv; //PCLKSEL0 &= ~LPC_PCLKSEL0_UART0_MASK;	// CCLK/4
			break;
/*		case 1:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART1_EN;
	        pDev->pUartReg = (LPC_UART_TypeDef*)LPC_UART1;
	        LPC_SC->PCLKSEL0 &= ~LPC_PCLKSEL0_UART1_MASK;	// CCLK/4
			break;
		case 2:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART2_EN;
	        pDev->pUartReg = LPC_UART2;
	        LPC_SC->PCLKSEL1 &= ~LPC_PCLKSEL1_UART2_MASK;	// CCLK/4
			break;*/
		default:
			return false;
	}

	//LPC_USART_Type *reg = (LPC_USART_Type *)pDev->pUartReg;

	// Configure I/O pins
	int idx = 0;

	while (pCfg->PinCfg[idx].PortNo >= 0 && idx < UART_NB_PINS)
	{
		IOPinCfg(&pCfg->PinCfg[idx], 1);
		idx++;
	}


	reg->TER = 0;	// Disable Tx
	reg->IER = 0;	// Disable all interrupts
	reg->ACR = 0;	// Disable auto baudrate

	// Clear all FIFO
	reg->FCR = LPCUART_FCR_RST_RXFIFO | LPCUART_FCR_RST_TXFIFO;


//	if (pCfg->DMAMode)
//		pDev->pUartReg->FCR |= LPCUART_FCR_DMA_MODE | LPCUART_FCR_RX_TRIG8;

	reg->LCR = (pCfg->DataBits - 5);
	if (pCfg->Parity != UART_PARITY_NONE)
	{
		reg->LCR |= (pCfg->Parity << 4);
	}

	if (pCfg->StopBits > 1)
		reg->LCR |= LPCUART_LCR_STOPBIT_MASK;

	reg->ICR = 0;
	if (pCfg->IrDAMode)
	{
		if (pCfg->IrDAInvert)
			reg->ICR |= LPCUART_ICR_IRDAINV;
		if (pCfg->IrDAFixPulse)
			reg->ICR |= LPCUART_ICR_IRDA_FIXPULSE | ((pCfg->IrDAPulseDiv & 7) << 3);
		reg->ICR |= LPCUART_ICR_IRDAEN;
	}

	g_LpcUartDev[pCfg->DevNo].DMAMode = pCfg->DMAMode;
	g_LpcUartDev[pCfg->DevNo].pUartReg = reg;
	g_LpcUartDev[pCfg->DevNo].pUartDev = pDev;

	pDev->SerIntrf.pDevData = (void*)&g_LpcUartDev[pCfg->DevNo];

	if (pCfg->Rate)
		pDev->Rate = LpcUARTSetRate(&pDev->SerIntrf, pCfg->Rate);
	else
	{
		// Auto baudrate
		reg->ACR = 7;
	}
	reg->FCR |= LPCUART_FCR_FIFOEN;

	uint32_t val = 0;

	while (LPC_USART->LSR & ~(3<<5))
	{
		val = LPC_USART->RBR;
	}

	val = LPC_USART->IIR;	// Clear interrupts

	// Start tx
	LPC_USART->TER = LPCUART_TER_TXEN;


	pDev->DataBits = pCfg->DataBits;
	pDev->FlowControl = pCfg->FlowControl;
	pDev->StopBits = pCfg->StopBits;
	pDev->IrDAFixPulse = pCfg->IrDAFixPulse;
	pDev->IrDAInvert = pCfg->IrDAInvert;
	pDev->IrDAMode = pCfg->IrDAMode;
	pDev->IrDAPulseDiv = pCfg->IrDAPulseDiv;
	pDev->Parity = pCfg->Parity;
	pDev->SerIntrf.Disable = LpcUARTDisable;
	pDev->SerIntrf.Enable = LpcUARTEnable;
	pDev->SerIntrf.GetRate = LpcUARTGetRate;
	pDev->SerIntrf.SetRate = LpcUARTSetRate;
	pDev->SerIntrf.StartRx = LpcUARTStartRx;
	pDev->SerIntrf.RxData = LpcUARTRxData;
	pDev->SerIntrf.StopRx = LpcUARTStopRx;
	pDev->SerIntrf.StartTx = LpcUARTStartTx;
	pDev->SerIntrf.TxData = LpcUARTTxData;
	pDev->SerIntrf.StopTx = LpcUARTStopTx;

	NVIC_EnableIRQ(UART_IRQn);
	LPC_USART->IER = LPCUART_IER_THRE | LPCUART_IER_RBR | LPCUART_IER_RLS;

	return true;
}
Пример #5
0
bool UARTInit(UARTDEV *pDev, const UARTCFG *pCfg)
{
	LPCUARTREG *reg = NULL;
	//g_UartClkDiv = 1;

	switch (pCfg->DevNo)
	{
		case 0:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART0_EN;
	        reg = (void*)LPC_USART;
	        LPC_SYSCON->UARTCLKDIV = 2;//g_UartClkDiv; //PCLKSEL0 &= ~LPC_PCLKSEL0_UART0_MASK;	// CCLK/4
			break;
/*		case 1:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART1_EN;
	        pDev->pUartReg = (LPC_UART_TypeDef*)LPC_UART1;
	        LPC_SC->PCLKSEL0 &= ~LPC_PCLKSEL0_UART1_MASK;	// CCLK/4
			break;
		case 2:
	        LPC_SYSCON->SYSAHBCLKCTRL |= LPC_SYSAHBCLKCTRL_UART2_EN;
	        pDev->pUartReg = LPC_UART2;
	        LPC_SC->PCLKSEL1 &= ~LPC_PCLKSEL1_UART2_MASK;	// CCLK/4
			break;*/
		default:
			return false;
	}

	//LPC_USART_Type *reg = (LPC_USART_Type *)pDev->pUartReg;

	// Configure I/O pins
	int idx = 0;

	while (pCfg->PinCfg[idx].PortNo >= 0 && idx < UART_NB_PINS)
	{
		IOPinCfg(&pCfg->PinCfg[idx], 1);
		idx++;
	}
/*
	LPC_GPIO->SET[pCfg->PinCfg[UARTPIN_TX_IDX].PortNo] = (1 << pCfg->PinCfg[UARTPIN_TX_IDX].PinNo);
	if (pCfg->PinCfg[UARTPIN_CTS_IDX].PortNo >= 0)
		LPC_GPIO->CLR[pCfg->PinCfg[UARTPIN_CTS_IDX].PortNo] = (1 << pCfg->PinCfg[UARTPIN_CTS_IDX].PinNo);

	if (pCfg->PinCfg[UARTPIN_RTS_IDX].PortNo >= 0)
	{
		LPC_GPIO->CLR[pCfg->PinCfg[UARTPIN_RTS_IDX].PortNo] = (1 << pCfg->PinCfg[UARTPIN_RTS_IDX].PinNo);
	}
*/

	reg->TER = 0;	// Disable Tx
	reg->IER = 0;	// Disable all interrupts
	reg->ACR = 0;	// Disable auto baudrate

	// Clear all FIFO
	reg->FCR = LPCUART_FCR_RST_RXFIFO | LPCUART_FCR_RST_TXFIFO;


//	if (pCfg->DMAMode)
//		pDev->pUartReg->FCR |= LPCUART_FCR_DMA_MODE | LPCUART_FCR_RX_TRIG8;

	// Data bis, Parity, Stop bit
	reg->LCR = (pCfg->DataBits - 5);
	if (pCfg->Parity != UART_PARITY_NONE)
	{
		reg->LCR |= (pCfg->Parity << 4);
	}

	if (pCfg->StopBits > 1)
		reg->LCR |= LPCUART_LCR_STOPBIT_MASK;

	reg->ICR = 0;
	if (pCfg->bIrDAMode)
	{
		if (pCfg->bIrDAInvert)
			reg->ICR |= LPCUART_ICR_IRDAINV;
		if (pCfg->bIrDAFixPulse)
			reg->ICR |= LPCUART_ICR_IRDA_FIXPULSE | ((pCfg->IrDAPulseDiv & 7) << 3);
		reg->ICR |= LPCUART_ICR_IRDAEN;
	}

	g_LpcUartDev[pCfg->DevNo].DMAMode = pCfg->bDMAMode;
	g_LpcUartDev[pCfg->DevNo].pUartReg = reg;
	g_LpcUartDev[pCfg->DevNo].pUartDev = pDev;

	pDev->SerIntrf.pDevData = (void*)&g_LpcUartDev[pCfg->DevNo];

	if (pCfg->Rate)
		pDev->Rate = LpcUARTSetRate(&pDev->SerIntrf, pCfg->Rate);
	else
	{
		// Auto baudrate
		reg->ACR = 7;
	}

	if (pCfg->FlowControl == UART_FLWCTRL_HW)
	{
//		LPC_GPIO->CLR[pCfg->PinCfg[UARTPIN_CTS_IDX].PortNo] = (1 << pCfg->PinCfg[UARTPIN_CTS_IDX].PinNo);
//		LPC_GPIO->CLR[pCfg->PinCfg[UARTPIN_RTS_IDX].PortNo] = (1 << pCfg->PinCfg[UARTPIN_RTS_IDX].PinNo);
		reg->MCR |= (3 << 6);	// Auto CTS/RTS flow control

	}
	else
	{
		reg->MCR &= ~(3 << 6);
	}

	reg->FCR = LPCUART_FCR_FIFOEN | LPCUART_FCR_RST_RXFIFO | LPCUART_FCR_RST_TXFIFO |
			   LPCUART_FCR_RX_TRIG8;

	uint32_t val = 0;

	while (LPC_USART->LSR & ~(3<<5))
	{
		val = LPC_USART->RBR;
	}

	val = LPC_USART->IIR;	// Clear interrupts
	pDev->LineState = 0;

	//LPC_USART->MCR |= (1<<4); // Loopback

	if (pCfg->pRxMem && pCfg->RxMemSize > 0)
	{
		pDev->hRxFifo = CFifoInit(pCfg->pRxMem, pCfg->RxMemSize, 1);
	}
	else
	{
		pDev->hRxFifo = CFifoInit(s_UARTRxFifoMem, UART_RX_CFIFO_MEM_SIZE, 1);
	}

	if (pCfg->pTxMem && pCfg->TxMemSize > 0)
	{
		pDev->hTxFifo = CFifoInit(pCfg->pTxMem, pCfg->TxMemSize, 1);
	}
	else
	{
		pDev->hTxFifo = CFifoInit(s_UARTTxFifoMem, UART_TX_CFIFO_MEM_SIZE, 1);
	}

	// Start tx
	LPC_USART->TER = LPCUART_TER_TXEN;


	pDev->DataBits = pCfg->DataBits;
	pDev->FlowControl = pCfg->FlowControl;
	pDev->StopBits = pCfg->StopBits;
	pDev->bIrDAFixPulse = pCfg->bIrDAFixPulse;
	pDev->bIrDAInvert = pCfg->bIrDAInvert;
	pDev->bIrDAMode = pCfg->bIrDAMode;
	pDev->IrDAPulseDiv = pCfg->IrDAPulseDiv;
	pDev->Parity = pCfg->Parity;
	pDev->SerIntrf.Disable = LpcUARTDisable;
	pDev->SerIntrf.Enable = LpcUARTEnable;
	pDev->SerIntrf.GetRate = LpcUARTGetRate;
	pDev->SerIntrf.SetRate = LpcUARTSetRate;
	pDev->SerIntrf.StartRx = LpcUARTStartRx;
	pDev->SerIntrf.RxData = LpcUARTRxData;
	pDev->SerIntrf.StopRx = LpcUARTStopRx;
	pDev->SerIntrf.StartTx = LpcUARTStartTx;
	pDev->SerIntrf.TxData = LpcUARTTxData;
	pDev->SerIntrf.StopTx = LpcUARTStopTx;
	pDev->EvtCallback = pCfg->EvtCallback;

	g_LpcUartDev[pCfg->DevNo].bTxReady = true;

	pDev->LineState = 0;

	if (pCfg->bIntMode)
	{
		LPC_USART->IER = LPCUART_IER_THRE | LPCUART_IER_RBR | LPCUART_IER_RLS | LPCUART_IER_MS | (1<<7);
		NVIC_ClearPendingIRQ(UART_IRQn);
		NVIC_SetPriority(UART_IRQn, pCfg->IntPrio);
		NVIC_EnableIRQ(UART_IRQn);
	}

	return true;
}