Exemplo n.º 1
0
/* Setup slave controller */
static void setupSlave(void)
{
	/* Initialize SPI controller */
	Chip_SPI_Init(LPC_SPISLAVEPORT);

	/* Call to initialize first SPI controller for mode0, master mode, MSB first */
	Chip_SPI_ConfigureSPI(LPC_SPISLAVEPORT, SPI_MODE_SLAVE |/* Enable slave mode */
						  SPI_CLOCK_CPHA0_CPOL0 |	/* Set Clock polarity to 0 */
						  SPI_CFG_MSB_FIRST_EN |/* Enable MSB first option */
						  SPI_CFG_SPOL_LO);	/* Chipselect is active low */

	/* Initialize SPI controllers */
	Chip_SPI_Init(LPC_SPISLAVEPORT);

	/* Setup slave controller for 8-bit transfer. Sizes can be altered later
	   for each slave select in the slave select assertion callback once the
	   transfer starts if needed. The master slave transfer size is setup as
	   part of the master transfer description in the options field. */
	Chip_SPI_SetXferSize(LPC_SPISLAVEPORT, 16);

	/* For the SPI controller configured in slave mode, enable SPI slave interrupts
	   for interrupt service. Do not enable SPI_INTENSET_TXDYEN. */
	Chip_SPI_EnableInts(LPC_SPISLAVEPORT, (SPI_INTENSET_RXDYEN |
										   SPI_INTENSET_RXOVEN | SPI_INTENSET_TXUREN | SPI_INTENSET_SSAEN |
										   SPI_INTENSET_SSDEN));

	/* Setup slave transfer callbacks in the transfer descriptor */
	spiSlaveXfer.pCB = &spiSlaveCallbacks;
}
Exemplo n.º 2
0
/* Setup master controller */
static void setupMaster(void)
{
	SPIM_DELAY_CONFIG_T masterDelay;

	/* Initialize SPI controller */
	Chip_SPI_Init(LPC_SPIMASTERPORT);

	/* Call to initialize first SPI controller for mode0, master mode, MSB first */
	Chip_SPI_ConfigureSPI(LPC_SPIMASTERPORT, SPI_MODE_MASTER |	/* Enable master mode */
						  SPI_CLOCK_CPHA0_CPOL0 |	/* Set Clock polarity to 0 */
						  SPI_CFG_MSB_FIRST_EN |/* Enable MSB first option */
						  SPI_CFG_SPOL_LO);	/* Chipselect is active low */

	/* Setup master clock rate, slave clock doesn't need to be setup */
	Chip_SPIM_SetClockRate(LPC_SPIMASTERPORT, LPCMASTERCLOCKRATE);

	/* Setup master delay (all chip selects) */
	masterDelay.PreDelay = 0xD;
	masterDelay.PostDelay = 0xD;
	masterDelay.FrameDelay = 0xD;
	masterDelay.TransferDelay = 0xD;
	Chip_SPIM_DelayConfig(LPC_SPIMASTERPORT, &masterDelay);

	/* For the SPI controller configured in master mode, enable SPI master interrupts
	   for interrupt service. Do not enable SPI_INTENSET_TXDYEN. */
	Chip_SPI_EnableInts(LPC_SPIMASTERPORT, (SPI_INTENSET_RXDYEN |
											SPI_INTENSET_RXOVEN | SPI_INTENSET_TXUREN | SPI_INTENSET_SSAEN |
											SPI_INTENSET_SSDEN));

	/* Setup master transfer callbacks in the transfer descriptor */
	spiMasterXfer.pCB = &spiMasterCallbacks;
}
/* Initialize the SPI */
void Chip_SPI_IF_Init(LPC_SPI_T *pSPI, SPI_MODECONFIG_T *pConfig)
{
	uint32_t EnStat;

	Chip_SPI_Init(pSPI);

	EnStat = pSPI->CFG & SPI_CFG_SPI_EN;
	/* Disable before update CFG register */
	if (EnStat) {
		Chip_SPI_Disable(pSPI);
	}

	/* SPI Configurate */
	pSPI->CFG = ((uint32_t) pConfig->ClockMode) | ((uint32_t) pConfig->DataOrder) | ((uint32_t) pConfig->Mode) |
				((uint32_t) pConfig->SSELPol);

	if ( pConfig->Mode == SPI_CFG_MASTER_EN ) {
		/* Rate Divider setting */
		pSPI->DIV = SPI_DIV_VAL(pConfig->ClkDiv);
	}

	/* Clear status flag*/
	Chip_SPI_ClearStatus(pSPI, SPI_STAT_RXOV | SPI_STAT_TXUR | SPI_STAT_SSA | SPI_STAT_SSD);

	/* Return the previous state */
	if (EnStat) {
		Chip_SPI_Enable(pSPI);
	}
}
/* Setup SPI handle and parameters */
static void setupSpiMaster()
{
	SPI_CFG_T spiCfg;
	SPI_DELAY_CONFIG_T spiDelayCfg;
	/* Initialize SPI Block */
	Chip_SPI_Init(LPC_SPI0);
	/* Set SPI Config register */
	spiCfg.ClkDiv = 0xFFFF;	/* Set Clock divider to maximum */
	spiCfg.Mode = SPI_MODE_MASTER;	/* Enable Master Mode */
	spiCfg.ClockMode = SPI_CLOCK_MODE0;	/* Enable Mode 0 */
	spiCfg.DataOrder = SPI_DATA_MSB_FIRST;	/* Transmit MSB first */
	/* Slave select polarity is active low */
	spiCfg.SSELPol = (SPI_CFG_SPOL0_LO | SPI_CFG_SPOL1_LO | SPI_CFG_SPOL2_LO | SPI_CFG_SPOL3_LO);
	Chip_SPI_SetConfig(LPC_SPI0, &spiCfg);
	/* Set Delay register */
	spiDelayCfg.PreDelay = 2;
	spiDelayCfg.PostDelay = 2;
	spiDelayCfg.FrameDelay = 2;
	spiDelayCfg.TransferDelay = 2;
	Chip_SPI_DelayConfig(LPC_SPI0, &spiDelayCfg);
	/* Enable Loopback mode for this example */
	Chip_SPI_EnableLoopBack(LPC_SPI0);
	/* Enable SPI0 */
	Chip_SPI_Enable(LPC_SPI0);
}
Exemplo n.º 5
0
uint32_t SPI_ReadCh0(void){

	/* Disable clocks to SWM and IOCON to save power */
	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON);

	// initialize pins
	Chip_SWM_DisableFixedPin(SWM_FIXED_XTALIN);
	Chip_SWM_MovablePinAssign(SWM_SPI1_SCK_IO, 8);
	Chip_SWM_MovablePinAssign(SWM_SPI1_MOSI_IO, 17);
	Chip_SWM_MovablePinAssign(SWM_SPI1_MISO_IO, 16);
	//TODO: do we need to set slave select if it's tied to ground?
	Chip_SWM_MovablePinAssign(SWM_SPI1_SSEL_IO, 4);


	ConfigStruct.Mode = SPI_MODE_MASTER;
	ConfigStruct.ClkDiv = Chip_SPI_CalClkRateDivider(LPC_SPI1, 100000);
	ConfigStruct.ClockMode = SPI_CLOCK_CPHA0_CPOL0;
	ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST;		//enable MSB first option
	ConfigStruct.SSELPol = SPI_CFG_SPOL_LO;
	Chip_SPI_Init(LPC_SPI1, &ConfigStruct);


	DelayConfigStruct.FrameDelay = 0;
	DelayConfigStruct.PostDelay = 0;
	DelayConfigStruct.PreDelay = 0;
	DelayConfigStruct.TransferDelay = 0;
	Chip_SPI_DelayConfig(LPC_SPI1, &DelayConfigStruct);

	Chip_SPI_Enable(LPC_SPI1);


	//Chip_SPI_EnableLoopBack(LPC_SPI1);

	// init transmit buffer
	TxBuf[0] = 0x68;
	TxBuf[1] = 0x00;

	XfSetup.Length = BUFFER_SIZE;
	XfSetup.pTx = TxBuf;
	XfSetup.RxCnt = XfSetup.TxCnt = 0;
	XfSetup.DataSize = 8;
	XfSetup.pRx = RxBuf;


	Chip_SPI_RWFrames_Blocking(LPC_SPI1, &XfSetup);

	uint16_t value = ((RxBuf[0] & 0x03) << 8) + (RxBuf[1] & 0xFF);


	//Chip_SPI_DisableLoopBack(LPC_SPI1);

	/* DeInitialize SPI peripheral */
	Chip_SPI_DeInit(LPC_SPI1);

	/* Disable clocks to SWM and IOCON to save power */
	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_IOCON);

	return value;
}
Exemplo n.º 6
0
int main(void) {
	char str_buffer[16];
	SystemCoreClockUpdate();

    gpioInit();
    interruptInit();
    adcInit();

	Chip_PMU_GetSleepFlags(LPC_PMU);
	Chip_PMU_ClearSleepFlags(LPC_PMU, PMU_PCON_DPDFLAG);

    Chip_SPI_Init(SPI_PORT);


    SysTick_Config(Chip_Clock_GetSystemClockRate() / TICKRATE_HZ);

    //StuckI2CHack();
    delayms(10);

    MoonLander_I2C_Init(SENSOR_I2C, MOONLANDER_I2C_100K);

    delayms(100);

    // Initialize sensors:
    HTU21D_Init(&g_HTU21D, SENSOR_I2C);
    delayms(10);
    HMC5883L_Init(&g_HMC5883L, SENSOR_I2C);
    delayms(10);

	HMC5883L_SetRange(&g_HMC5883L, HMC5883L_RANGE_2_5);

    eGFX_InitDriver();
    C12832A_Init(&g_C12832A, SPI_PORT, LCD_A0_PIN, LCD_RST_PIN, LCD_SSEL);
    delayms(10);

    Plot_Init(&g_plot_temp, -10, 40, "Temp (C)", 0);
    Plot_Init(&g_plot_rh, 0, 100, "RH", 0);

    Plot_Init(&g_plot_mag, -400, 300, "uTesla", 1);
    Plot_SetSpecialValue(&g_plot_mag, 9000, "OL");

    Compass_Init(&g_compass);

    g_left_display = DISPLAY_TEMP;
    g_right_display = DISPLAY_COMPASS;

    // Straight to sleep on boot:
    g_go_to_sleep = 1;

    // Or not:
    //g_go_to_sleep = 0;
    //wakeup();

    while(1) {

    	fillScreen(0x0);

    	if (g_go_to_sleep) {

    		// Write the empty back buffer to the screen:
    		eGFX_Dump(&eGFX_BackBuffer, &g_C12832A);

    		g_ignore_switches = 1;
    		// Sleep!
    		goToSleep();

    		// Processor has been woken up, restart clocks, etc.:
    		wakeup();

    		delayms(SW_DEBOUNCE_MS);
    		g_ignore_switches = 0;
    	}

    	switch (g_left_display) {
    	case DISPLAY_TEMP:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_temp, PLOT_LEFT);
    		break;
    	case DISPLAY_RH:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_rh, PLOT_LEFT);
    		break;
    	case DISPLAY_MAG:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_mag, PLOT_LEFT);
    		break;
    	case DISPLAY_COMPASS:
    		Compass_Draw(&eGFX_BackBuffer, &g_compass, COMPASS_LEFT);
    		break;
    	case DISPLAY_RANGE:
    		eGFX_DrawString(&eGFX_BackBuffer, "Range", 24, 1, &FONT_3_5_1BPP);
    		sprintf(str_buffer, "%0.2f cm", getRangeCentimeters());
    		eGFX_DrawString(&eGFX_BackBuffer, str_buffer, 24, 13, &FONT_3_5_1BPP);
    		break;
    	default:
    		break;
    	}

    	switch (g_right_display) {
    	case DISPLAY_TEMP:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_temp, PLOT_RIGHT);
    		break;
    	case DISPLAY_RH:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_rh, PLOT_RIGHT);
    		break;
    	case DISPLAY_MAG:
    		Plot_Draw(&eGFX_BackBuffer, &g_plot_mag, PLOT_RIGHT);
    		break;
    	case DISPLAY_COMPASS:
    		Compass_Draw(&eGFX_BackBuffer, &g_compass, COMPASS_RIGHT);
    		break;
    	case DISPLAY_RANGE:
    		eGFX_DrawString(&eGFX_BackBuffer, "Range", 88, 1, &FONT_3_5_1BPP);
    		sprintf(str_buffer, "%0.2f cm", getRangeCentimeters());
    		eGFX_DrawString(&eGFX_BackBuffer, str_buffer, 88, 13, &FONT_3_5_1BPP);
    		break;
    	default:
    		break;
    	}


    	eGFX_Dump(&eGFX_BackBuffer, &g_C12832A);

    }
    return 0 ;
}
Exemplo n.º 7
0
int32_t main(void) {

  int32_t i,cnt=0;
  uint8_t RC5_System_prev=0;
  uint8_t RC5_Command_prev=0;
  CHIP_PMU_MCUPOWER_T mcupower=PMU_MCU_SLEEP;

  SystemCoreClockUpdate();

// init GPIO
  Chip_GPIO_Init(LPC_GPIO_PORT);
  Chip_SYSCTL_PeriphReset(RESET_GPIO);

// init SPI0 at SystemCoreClock speed
  Chip_SPI_Init(LPC_SPI0);
  Chip_SPI_ConfigureSPI(LPC_SPI0,SPI_MODE_MASTER|
                                 SPI_CLOCK_CPHA0_CPOL0|
                                 SPI_DATA_MSB_FIRST|
                                 SPI_SSEL_ACTIVE_LO);
  LPC_SPI0->DIV=0;
  Chip_SPI_Enable(LPC_SPI0);

// init MRT
  Chip_MRT_Init();

// init SWM
  Chip_SWM_Init();
  Chip_SWM_DisableFixedPin(SWM_FIXED_SWCLK);//PIO0_3
  Chip_SWM_DisableFixedPin(SWM_FIXED_SWDIO);//PIO0_2
  Chip_SWM_DisableFixedPin(SWM_FIXED_ACMP_I2);//PIO0_1
  Chip_SWM_DisableFixedPin(SWM_FIXED_ACMP_I1);//PIO0_0
  Chip_SWM_MovablePinAssign(SWM_SPI0_SCK_IO,CLK_PIN);
  Chip_SWM_MovablePinAssign(SWM_SPI0_MOSI_IO,MOSI_PIN);
  Chip_SWM_MovablePinAssign(SWM_CTIN_0_I,IR_PIN);
  Chip_SWM_Deinit();

// init onboard LED
  Chip_GPIO_SetPinState(LPC_GPIO_PORT,0,LED_PIN,true);
  Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,0,LED_PIN);

// init LCD reset pin
  Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT,0,RESET_PIN);

// init LCD
  LCDInit();
  LCDClearScreenBlack();

// init SCT
  Chip_SCT_Init(LPC_SCT);
  // set prescaler, SCT clock = 1 MHz, clear counter
  LPC_SCT->CTRL_L |= SCT_CTRL_PRE_L(SystemCoreClock/1000000-1) | SCT_CTRL_CLRCTR_L;
  sct_fsm_init();
  NVIC_EnableIRQ(SCT_IRQn);

// init PIO0_3 pin interrupt for wakeup from sleep mode
  Chip_SYSCTL_SetPinInterrupt(IR_IRQ,IR_PIN);
  Chip_PININT_EnableIntLow(LPC_PININT, PININTCH(IR_IRQ));
  NVIC_EnableIRQ(PININT3_IRQn);
  Chip_SYSCTL_EnablePINTWakeup(IR_IRQ);

// set sleep options
  Chip_SYSCTL_SetDeepSleepPD(SYSCTL_DEEPSLP_BOD_PD | SYSCTL_DEEPSLP_WDTOSC_PD);
  Chip_SYSCTL_SetWakeup(~(SYSCTL_SLPWAKE_IRCOUT_PD | SYSCTL_SLPWAKE_IRC_PD | SYSCTL_SLPWAKE_FLASH_PD ));

  LCDPutStr("kbiva.wordpress.com", MAX_X / 2 + 50, 10, WHITE, BLACK);
  LCDPutStr("RC5 Decoder", MAX_X / 2 + 35, 35, WHITE, BLACK);
  LCDPutStr("Frame:", MAX_X / 2 + 20, 1, WHITE, BLACK);
  LCDPutStr("System:", MAX_X / 2 + 5 , 1, WHITE, BLACK);
  LCDPutStr("Cmd:", MAX_X / 2 - 10, 1, WHITE, BLACK);
  LCDPutStr("Toggle:", MAX_X / 2 - 25, 1, WHITE, BLACK);
  LCDPutStr("Count:", MAX_X / 2 - 40, 1, WHITE, BLACK);

  while (1) {

// put chip to sleep
    switch(mcupower) {
      case PMU_MCU_SLEEP:
      default:
        LCDPutStr("SLEEP     ", MAX_X / 2 - 60, 10, WHITE, BLACK);
        Chip_PMU_SleepState(LPC_PMU);
        break;
      case PMU_MCU_DEEP_SLEEP:
        LCDPutStr("DEEP-SLEEP", MAX_X / 2 - 60, 10, WHITE, BLACK);
        Chip_PMU_DeepSleepState(LPC_PMU);
        break;
      case PMU_MCU_POWER_DOWN:
        LCDPutStr("POWER-DOWN", MAX_X / 2 - 60, 10, WHITE, BLACK);
        Chip_PMU_PowerDownState(LPC_PMU);
        break;
    }

// start MRT timer channel 0
    Chip_MRT_SetInterval(LPC_MRT_CH0, SystemCoreClock | MRT_INTVAL_LOAD);
    Chip_MRT_SetMode(LPC_MRT_CH0,MRT_MODE_ONESHOT);
    Chip_MRT_SetEnabled(LPC_MRT_CH0);

// turn on onboard LED
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,0,LED_PIN,false);

// start SCT
    LPC_SCT->CTRL_L &= ~SCT_CTRL_HALT_L;

// wait for timeout
    while(!RC5_timeout)
    {};

// stop SCT
    LPC_SCT->CTRL_L |= SCT_CTRL_HALT_L;

    if (RC5_flag) {
      // if frame received, output information on LCD

      if((RC5_System != RC5_System_prev) || (RC5_Command != RC5_Command_prev)) {
        cnt = 1;
      }
      else {
        cnt++;
      }

      for (i = 3; i >= 0; i--){

        LCDPutChar(ascii[(RC5_Frame >> (i * 4)) & 0x0F],MAX_X / 2 + 20,80+(3-i)*7,WHITE, BLACK);
        if(i < 2) {
          if((RC5_System!=RC5_System_prev) || (RC5_Command!=RC5_Command_prev)){
            LCDPutChar(ascii[(RC5_System >> (i * 4)) & 0x0F],MAX_X / 2 + 5,66+(3-i)*7,WHITE, BLACK);
            LCDPutChar(ascii[(RC5_Command >> (i * 4)) & 0x0F],MAX_X / 2 - 10,66+(3-i)*7,WHITE, BLACK);
          }
        }
        LCDPutChar(ascii[(cnt >> (i * 4)) & 0x0F],MAX_X / 2 - 40,80+(3-i)*7,WHITE, BLACK);
      }

      LCDPutStr(RC5_Toggle ? "ON ":"OFF", MAX_X / 2 - 25, 80, WHITE, BLACK);

      switch(RC5_Command) {
        case 0x50:
          mcupower = PMU_MCU_SLEEP;
        break;
        case 0x55:
          if(RC5_Toggle){
            spi0Transfer(SLEEPOUT);
            spi0Transfer(DISPON);
          }
          else {
            spi0Transfer(DISPOFF);
            spi0Transfer(SLEEPIN);
          }
        break;
        case 0x56:
          mcupower = PMU_MCU_DEEP_SLEEP;
        break;
        case 0x6B:
          mcupower = PMU_MCU_POWER_DOWN;
        break;
      }

      RC5_System_prev = RC5_System;
      RC5_Command_prev = RC5_Command;
    }
// turn off onboard LED
    Chip_GPIO_SetPinState(LPC_GPIO_PORT,0,LED_PIN,true);
// clear flags
    RC5_flag = 0;
    RC5_timeout = 0;
  }