int main(void)
{
	char temp;

	LED1_INIT();
	LED2_INIT();
	uart_config_default_stdio();
	uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
	sei();
	LED1_ON();
	LED2_ON();
	printf("\n\n********RFM22 Communication Test********\n");	
	LED1_ON();
	LED2_OFF();
	
	rfm22b_init();
	txtest_configure_radio();

	//====================//
	//Communications Test
	
	temp = rfm22b_read(DTYPE);
	temp = rfm22b_read(DVERS);
	temp = rfm22b_read(INTEN1);
	temp = rfm22b_read(INTEN2);
	temp = rfm22b_read(OMFC1);
	temp = rfm22b_read(OMFC2);
	
	LED1_OFF();
	LED2_ON();
	
	printf("*****************************************\n\n");
	txtest_send_current_packet();	// Send test packet	'0123456789:;<=>?"
	
	// This example allows you to enter a 16-byte packet to send
	printf("Entering TX Mode...Give me a 16-byte packet\n\n");
	
	LED1_OFF();
	LED2_OFF();
	txtest_fill_packet_from_uart();
	
	while(1)
	{
		txtest_send_current_packet();
		_delay_ms(1000);
	}
	return 0;
}
Esempio n. 2
0
/*!
 * Init board.
 */
void BoardInit(void)
{
	SPI_InitTypeDef   SPI_InitStructure;
	EXTI_InitTypeDef   EXTI_InitStructure;
	NVIC_InitTypeDef   NVIC_InitStructure;
#ifdef STM32F10X_MD
	RCC_APB2PeriphClockCmd( 0
		| RCC_APB2Periph_GPIOA
		| RCC_APB2Periph_GPIOB
		| RCC_APB2Periph_AFIO
		, ENABLE);
#else
#ifdef STM32L1XX_MD
	RCC_AHBPeriphClockCmd( 0
		| RCC_AHBPeriph_GPIOA
		| RCC_AHBPeriph_GPIOB
		, ENABLE);
#else
	#error "Unknown CPU type"
#endif
#endif
	DBGMCU_Config( 0
		| DBGMCU_TIM2_STOP
		| DBGMCU_TIM3_STOP
		| DBGMCU_SLEEP
		| DBGMCU_STOP
		| DBGMCU_STANDBY
		, ENABLE);
	MAC_TIMER_CLOCK();
	DELAY_TIMER_CLOCK();

	LED1_OFF();
	LED1_INIT();
	LED2_OFF();
	LED2_INIT();
	
	TimerInit( MAC_TIMER,   MAC_TIMER_PRESCALER,	MAC_TIMER_IRQn );
	TimerInit( DELAY_TIMER, DELAY_TIMER_PRESCALER,	0 );
	
	RF_SDN_HIGH();
	RF_SDN_INIT();
	RF_NSS_HIGH();
	RF_NSS_INIT();
	RF_SCLK_INIT();
	RF_MISO_INIT();
	RF_MOSI_INIT();
	RF_IRQ_INIT();

	DELAY_uS(10 * DELAY_1MS_TIMER2);
	RF_SDN_LOW();
	DELAY_uS(50 * DELAY_1MS_TIMER2);

	RF_SPI_CLOCK();

	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(RF_SPI, &SPI_InitStructure);
	SPI_Cmd(RF_SPI, ENABLE);
	
#ifdef STM32F10X_MD
	GPIO_EXTILineConfig(RF_IRQ_EXT_PORT, RF_IRQ_EXT_PIN);
#else
#ifdef STM32L1XX_MD
	SYSCFG_EXTILineConfig(RF_IRQ_EXT_PORT, RF_IRQ_EXT_PIN);
#endif
#endif

	EXTI_InitStructure.EXTI_Line = RF_IRQ_EXT_LINE;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);

	DISABLE_MAC_EXT_INTERRUPT();
	CLEAR_MAC_EXT_INTERRUPT();

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

	NVIC_InitStructure.NVIC_IRQChannel = RF_IRQ_EXT_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

#ifdef RTC_ENABLED
    RTC_INIT();
#endif
#ifdef UART0_ENABLED
	Uart0Init();
#endif
}
Esempio n. 3
0
/*!
 * @brief main function
 */
int main(void)
{
    int32_t currentTemperature = 0;
    uint32_t updateBoundariesCounter = 0;
    int32_t tempArray[UPDATE_BOUNDARIES_TIME * 2];
    lowPowerAdcBoundaries_t boundaries;

    /* Init hardware */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();
    /* Init using Led in Demo app */
    LED1_INIT();
    LED2_INIT();

    /* Set to allow entering vlps mode */
    SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeVlp);

    /* Calibrate param Temperature sensor */
    ADC16_CalibrateParams(DEMO_ADC16_BASEADDR);

    /* Initialize Demo ADC */
    if (!ADC16_InitHardwareTrigger(DEMO_ADC16_BASEADDR))
    {
        PRINTF("Failed to do the ADC init\r\n");
        return -1;
    }

    PRINTF("\n\r ADC LOW POWER DEMO\n");
    PRINTF("\r The Low Power ADC project is designed to work with the Tower System or in a stand alone setting\n\n");
    PRINTF("\r 1. Set your target board in a place where the temperature is constant.\n");
    PRINTF("\r 2. Wait until two Led light turns on.\n");
    PRINTF("\r 3. Increment or decrement the temperature to see the changes.\n");
    PRINTF("\r Wait two led on...\n\r");

    /* setup the HW trigger source */
    LPTMR_InitTriggerSourceOfAdc(DEMO_LPTMR_BASE);
    ADC16_EnableDMA(DEMO_ADC16_BASEADDR, false);
    NVIC_EnableIRQ(DEMO_ADC16_IRQ_ID);
    /* Warm up microcontroller and allow to set first boundaries */
    while (updateBoundariesCounter < (UPDATE_BOUNDARIES_TIME * 2))
    {
        while (!conversionCompleted)
        {
        }
        currentTemperature = GetCurrentTempValue();
        tempArray[updateBoundariesCounter] = currentTemperature;
        updateBoundariesCounter++;
        conversionCompleted = false;
    }

    /* Temp Sensor Calibration */
    boundaries = TempSensorCalibration(updateBoundariesCounter, tempArray);
    updateBoundariesCounter = 0;

    /* Two LED is turned on indicating calibration is done */
    LED1_ON();
    LED2_ON();

    /* Wait for user input before beginning demo */
    PRINTF("\r Enter any character to begin...\n");
    GETCHAR();
    PRINTF("\r ---> OK! Main process is running...!\n");

    while (1)
    {
        /* Prevents the use of wrong values */
        while (!conversionCompleted)
        {
        }

        /* Get current Temperature Value */
        currentTemperature = GetCurrentTempValue();
        /* Store temperature values that are going to be use to calculate average temperature */
        tempArray[updateBoundariesCounter] = currentTemperature;

        if (currentTemperature > boundaries.upperBoundary)
        {
            LED2_OFF();
            LED1_ON();
        }
        else if (currentTemperature < boundaries.lowerBoundary)
        {
            LED2_ON();
            LED1_OFF();
        }
        else
        {
            LED2_ON();
            LED1_ON();
        }

        /* Call update function */
        if (updateBoundariesCounter >= (UPDATE_BOUNDARIES_TIME))
        {
            boundaries = TempSensorCalibration(updateBoundariesCounter, tempArray);
            updateBoundariesCounter = 0;
        }
        else
        {
            updateBoundariesCounter++;
        }

        /* Clear conversionCompleted flag */
        conversionCompleted = false;

        /* Enter to Very Low Power Stop Mode */
        SMC_SetPowerModeVlps(SMC);
    }
}
int  main (int argc, char **argv)
{
    GPIO_InitTypeDef   GPIO_InitStruct;

    board_init();                      // Turn off WDT, init MCU clocks, ...

    board_systick_timer_config();      // configure 1 ms timer

    LED1_INIT();                       // Setup LED1 for output
//  LED2_INIT();                       // Setup LED2 for output  not on STM32

#if (UART_CHECKOUT)
while (uart_test1)  // TEMP DEBUG of Baud Rate
   CONSOLE_WRITE ("\n\rInitialization complete. Type in a config parm:\n\r");
while ( ! CONSOLE_CHECK_FOR_INPUT() )
      ;        // loop till get some data
CONSOLE_READ_STRING ((char*) uart_cmd_buf, 12);

CONSOLE_WRITE ("Type in a single char: ");
ret_code = 0;
while (ret_code == 0)              // wait till we get a char in
   ret_code = CONSOLE_GET_CHAR();
CONSOLE_WRITE_CHAR (ret_code);     // echo it
#endif

       //-------------------------------------------------------------
       // Setup GPIOs to operate in Timer/PWM mode
       //
       // Start with PA8 (Arduino D7) = TIM1_CH1
       //-------------------------------------------------------------
       /* TIM1 module - Channel 1 output */
    GPIO_InitStruct.Pin       = GPIO_PIN_8;      // PA8 = Arduino D7
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;   // Alt Func = Timer/PWM
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    HAL_GPIO_Init (GPIOA, &GPIO_InitStruct);

       /* TIM1 module - Channel 1 complementary output */
    GPIO_InitStruct.Pin = GPIO_PIN_13;           // PB13 - not on Arduino - Morpho CN10 - 30
    HAL_GPIO_Init (GPIOB, &GPIO_InitStruct);

       //------------------------------------
       // Configure PWM module to be used
       //------------------------------------
       // SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
    uwPeriod = (SystemCoreClock / 20000 ) - 1;       // set period = 20 K Hz
    ret_code = board_pwm_init_module (PWM_MODULE_1, PWM_COUNT_DOWN,
                                      uwPeriod, 0);

       //------------------------------------
       // Configure PWM channel(s) to be used
       //------------------------------------
    ret_code = board_pwm_config_channel (PWM_MODULE_1, PWM_CHANNEL_1,
                                         (uwPeriod/4), 0);   // 25 % duty cycle

       //---------------------------------------------------
       // Enable the PWM module and its associated channels
       //---------------------------------------------------
    ret_code = board_pwm_enable (PWM_MODULE_1, 0);

    while (1)
      {
            // initial checkout - see if PWM is working
        LED1_TOGGLE();              // show activity
        board_delay_ms (1000);
      }

// -----   INITIAL CHECKOUT   -----

#if (LATER)
       //-------------------------------------------------------------
       // Ensure DRV8848 is OFF during config.
       // setup nSLEEP PB_2 pin (J2-2) to LOW to turn off the DRV8848
       //-------------------------------------------------------------
    pin_Config (Pin19, GPIO_OUTPUT);
    pin_Low (Pin19);

      //-----------------------------------------------------------------
      // Setup PWMs to drive the DRV8711 Stepper
      //-----------------------------------------------------------------
      // first, get the period for a 20 K Hz PWM frequency
    pwm_period = board_frequency_to_period_ticks (PWM_20K_FREQUENCY);

      // then initialize the needed PWMs, passing in startup period
        //-------------------------------------------------------------
        // do basic setup for PWM and Interval Timer  (turn on clocks)
        // Skipping the /32 gives better accuracy (20.004 KHz vs 20.81 K Hz using /32)
        //-------------------------------------------------------------
//  MAP_SysCtlPWMClockSet (SYSCTL_PWMDIV_32);         // do not do / 32 of Master clock and see results
    MAP_SysCtlPWMClockSet (SYSCTL_PWMDIV_1);          // Setup Master PWM I/O clock
    MAP_SysCtlPeripheralEnable (SYSCTL_PERIPH_PWM1);  // turn on PWM module 1 clock
//  mg_PWMclock  = MAP_SysCtlClockGet() / 32;   // save Max PWM Clock value
    mg_PWMclock  = MAP_SysCtlClockGet();        // save Max PWM Clock value
    mg_Sysclock  = MAP_SysCtlClockGet();        // This says 80 MHz
    nmap_SysClkTicks = MAP_SysCtlClockGet();    // This says 80 MHz after switching over to MAP_ version.
                                                // Before that, non MAP_ version was saying 66 MHz, which is whacked out

       //------------------------------------------------------------------------------
       // Setup PWM pins that are used by DRV8848
       //
       //    PWM A / AIN1 = PA_4 TA1.2   J2-8        PWM B / BIN1 = PA_6 TA1.1   J1-9
       //    DIR A / AIN2 = PA_3 TA1CCR2 J2-9        DIR B / BIN2 = PA_7 TA1CCR1 J1-10
       //    nSLEEP = PB_2               J2-2        nFAULT = PE_0               J2-3
       //    VREF   = PE_4  Adc9         J1-5
       //------------------------------------------------------------------------------

       // setup DRV8701 STEP_AIN1 PA_6 pin (J1-9) for PWM mode
       // setup DRV8848 BIN1      PA_6 pin (J1-9) for PWM mode
    MAP_GPIOPinTypePWM (GPIO_PORTA_BASE, GPIO_PIN_6);   // setup BIN1 PWM (J1-9)
    MAP_GPIOPinConfigure (GPIO_PA6_M1PWM2);

       //------------------------------------------------
       // Setup PWM and associated Count mode.
       // Tiva only supports DOWN counts, not UP counts
       //------------------------------------------------
    MAP_PWMGenConfigure (PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN);

       // setup default PWM Period and duty cycle
    mg_PWMperiod = (mg_PWMclock / PWM_20K_FREQUENCY) - 1;   // default period /32  = 124   (125 - 1)
                                                            // default period /1   = 3999  (4000 - 1)
    MAP_PWMGenPeriodSet (PWM1_BASE, PWM_GEN_1, mg_PWMperiod);

    MAP_PWMPulseWidthSet (PWM1_BASE, PWM_OUT_2, mg_PWMperiod / 4);  // duty of 25 %
    pwm_duty_value = mg_PWMperiod / 4;                   // save it for ADC checks

      //-----------------------
      //   Start up the PWM
      //-----------------------
    MAP_PWMGenEnable (PWM1_BASE, PWM_GEN_1);             // start up the PWM  ??? need ???

    MAP_PWMOutputState (PWM1_BASE, PWM_OUT_2_BIT, true); // Enable PWM pin output

       //-----------------------------------------
       // setup DIR BIN2 PA_7 pin (J1-10) to LOW
       //-----------------------------------------
    pin_Config (Pin10, GPIO_OUTPUT);   // setup BIN2 DIR (J1-10)
    pin_Low (Pin10);

       //-----------------------------------------
       // Turn off AIN1 PWM and AIN2 DIR pins PA_4 / PA_3 pins (J2-8/J2-9) to LOW
       //-----------------------------------------
    pin_Config (Pin13, GPIO_OUTPUT);   // setup AIN1 PWM (J2-8)
    pin_Low (Pin13);
    pin_Config (Pin12, GPIO_OUTPUT);   // setup AIN2 DIR (J2-9)
    pin_Low (Pin12);

       //-------------------------------------------------------------
       // set nSLEEP PB_2 pin (J2-2) to HIGH to turn on the DRV8848
       //-------------------------------------------------------------
    pin_High (Pin19);

    while (1)
      {
 //      LED1_TOGGLE();            // Setup LED1 for output
         if  (adc_Check_All_Complete())
             {     // we have a completed ADC reading. Pull in the latest reading
               adc_Read (&adc_value);
               if ( (adc_value > (adc_cur_value +10)) ||  (adc_value < (adc_cur_value - 1)) )
                  {    // we have a major change in valu. Update the PWM
                    adc_cur_value = adc_value;
                       // scale the PWM Duty_Cycle (o - 4000) value to match the ADC value (0-4096)
                    if (adc_cur_value > 3999)
                       pwm_duty_value = 3999;
                       else pwm_duty_value = adc_value;
                       // set the new PWM duty ccyle
                    MAP_PWMPulseWidthSet (PWM1_BASE, PWM_OUT_2, pwm_duty_value);
                       // start a new ADC sampling cycle
                    adc_Trigger_Start();
                  }
             }
         board_delay_ms (250);     // wait 250 ms (1/4 sec) between samples
      }
#endif

}