static void system_init(void) { /** * Configure the 32 kHz pins, PD6 and PD7, for crystal operation * By default they are configured as GPIOs */ GPIODirModeSet(GPIO_D_BASE, 0x40, GPIO_DIR_MODE_IN); GPIODirModeSet(GPIO_D_BASE, 0x80, GPIO_DIR_MODE_IN); IOCPadConfigSet(GPIO_D_BASE, 0x40, IOC_OVERRIDE_ANA); IOCPadConfigSet(GPIO_D_BASE, 0x80, IOC_OVERRIDE_ANA); /** * Set the real-time clock to use the 32.768 kHz external crystal * Set the system clock to use the 32 MHz external crystal */ SysCtrlClockSet(true, false, SYS_CTRL_SYSDIV_32MHZ); /** * Set the IO clock to operate at 16 MHz * This way peripherals can run while the system clock is gated */ SysCtrlIOClockSet(SYS_CTRL_SYSDIV_16MHZ); /** * Wait until the 32 MHz oscillator becomes stable */ while (!((HWREG(SYS_CTRL_CLOCK_STA)) & (SYS_CTRL_CLOCK_STA_XOSC_STB))); }
static void clock_init(void) { /** * Disable global interrupts */ bool bIntDisabled = IntMasterDisable(); /** * Configure the 32 kHz pins, PD6 and PD7, for crystal operation * By default they are configured as GPIOs */ GPIODirModeSet(GPIO_D_BASE, 0x40, GPIO_DIR_MODE_IN); GPIODirModeSet(GPIO_D_BASE, 0x80, GPIO_DIR_MODE_IN); IOCPadConfigSet(GPIO_D_BASE, 0x40, IOC_OVERRIDE_ANA); IOCPadConfigSet(GPIO_D_BASE, 0x80, IOC_OVERRIDE_ANA); /** * Set the real-time clock to use the 32khz internal crystal * Set the system clock to use the external 32 MHz crystal * Set the system clock to 32 MHz */ SysCtrlClockSet(true, false, SYS_CTRL_SYSDIV_32MHZ); /** * Set the IO clock to operate at 16 MHz * This way peripherals can run while the system clock is gated */ SysCtrlIOClockSet(SYS_CTRL_SYSDIV_16MHZ); /** * Wait until the selected clock configuration is stable */ while (!((HWREG(SYS_CTRL_CLOCK_STA)) & (SYS_CTRL_CLOCK_STA_XOSC_STB))); /** * Define what peripherals run in each mode */ SysCtrlDeepSleepSetting(); SysCtrlSleepSetting(); SysCtrlRunSetting(); SysCtrlWakeupSetting(); /** * Re-enable interrupt if initially enabled. */ if(!bIntDisabled) { IntMasterEnable(); } }
/************************************************************************************************** * @fn SysCtrlClockStartupSetting * * @brief Setup clock startup sequence * * input parameters * * @param None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void SysCtrlClockStartSetting(void) { /* Setup the clock startup sequence to 32 MHz external * osc and 32k sourced from external oscillator */ IOCPadConfigSet(GPIO_D_BASE, 0xC0, IOC_OVERRIDE_ANA); SysCtrlClockSet(OSC_32KHZ, false, SYS_CTRL_SYSDIV_32MHZ); }
//***************************************************************************** // //! Configures pin(s) for use by the Timer peripheral //! //! \param ui32Port is the base address of the GPIO port. //! \param ui8Pins is the bit-packed representation of the pin(s). //! //! The CCP pins must be properly configured for the timer peripheral to //! function correctly. This function provides a typical configuration for //! those pin(s); other configurations might work as well depending upon the //! board setup (for example, using the on-chip pull-ups). //! //! The pin(s) are specified using a bit-packed byte, where each bit that is //! set identifies the pin to be accessed, and where bit 0 of the byte //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on. //! //! \note This function cannot be used to turn any pin into a timer pin but only //! configures a timer pin for proper operation. //! //! \return None // //***************************************************************************** void GPIOPinTypeTimer(uint32_t ui32Port, uint8_t ui8Pins) { // // Check the arguments. // ASSERT(GPIOBaseValid(ui32Port)); // // Make the pin(s) be peripheral controlled. // GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW); // // Set the pad(s) to no drive type. // IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS); }
//***************************************************************************** // //! Configures pin(s) for use as GPIO outputs //! //! \param ui32Port is the base address of the GPIO port. //! \param ui8Pins is the bit-packed representation of the pin(s). //! //! The GPIO pins must be properly configured to function correctly as //! GPIO outputs. This function provides the proper configuration for those //! pin(s). //! //! The pin(s) are specified using a bit-packed byte, where each bit that is //! set identifies the pin to be accessed, and where bit 0 of the byte //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on. //! //! \return None // //***************************************************************************** void GPIOPinTypeGPIOOutput(uint32_t ui32Port, uint8_t ui8Pins) { // // Check the arguments. // ASSERT(GPIOBaseValid(ui32Port)); // // Make the pin(s) be outputs. // GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_OUT); // // Set the pad(s) no override of the drive type. // IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS); }
//***************************************************************************** // //! Configures output pin(s) for use by the UART peripheral //! //! \param ui32Port is the base address of the GPIO port. //! \param ui8Pins is the bit-packed representation of the pin(s). //! //! The UART output pins must be properly configured for the UART peripheral to //! function correctly. This function provides a typical configuration for //! those pin(s); other configurations might work as well depending upon the //! board setup (for example, using the on-chip pull-ups). //! //! The pin(s) are specified using a bit-packed byte, where each bit that is //! set identifies the pin to be accessed, and where bit 0 of the byte //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on. //! //! \note This function cannot be used to turn any pin into a UART pin; but only //! configures a UART pin for proper operation. //! //! \return None // //***************************************************************************** void GPIOPinTypeUARTOutput(uint32_t ui32Port, uint8_t ui8Pins) { // // Check the arguments. // ASSERT(GPIOBaseValid(ui32Port)); ASSERT(!((ui32Port == GPIO_C_BASE) && ((ui8Pins & 0xf) > 0))); // // Make the pin(s) be peripheral controlled. // GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW); // // Set the pad(s) to output enable. // IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_OE); }
/************************************************************************************************** * @fn main * * @brief C-code main functionality. * * input parameters * * None. * * output parameters * * None. * * @return None. ************************************************************************************************** */ void main(void) { /* Setup the clock startup sequence to 32 MHz external * osc and 32k sourced from external oscillator */ IOCPadConfigSet(GPIO_D_BASE, 0xC0, IOC_OVERRIDE_ANA); SysCtrlClockSet(OSC_32KHZ, false, SYS_CTRL_SYSDIV_32MHZ); /* Check if clock is stable */ HAL_CLOCK_STABLE(); /* Turn on cache prefetch mode */ PREFETCH_ENABLE(); /* Boot Loader code execute */ sblExec(); /* Code should not come here */ HAL_SYSTEM_RESET(); }
/************************************************************************************************** * @fn HalAdcRead * * @brief Read the ADC based on given channel and resolution * * @param channel - channel where ADC will be read * @param resolution - the resolution of the value * * @return 16 bit value of the ADC in offset binary format. * * Note that the ADC is "bipolar", which means the GND (0V) level is mid-scale. * Note2: This function assumes that ADCCON3 contains the voltage reference. **************************************************************************************************/ uint16 HalAdcRead (uint8 channel, uint8 resolution) { int16 reading = 0; #if (HAL_ADC == TRUE) uint8 i, resbits; uint8 adcChannel = 1; uint32 padConfig, dirConfig; halIntState_t s; /* * If Analog input channel is AIN0..AIN7, make sure corresponing PA pin is * setup. Only port A can be used as input to the ADC. If any pin on port A * is to be used as an ADC input, the appropriate register, IOC_PAx_OVER, * must be set to analog (that is, bit 0 must be set to 1). */ /* Hold off interrupts */ HAL_ENTER_CRITICAL_SECTION(s); switch (channel) { case HAL_ADC_CHN_AIN0: case HAL_ADC_CHN_AIN1: case HAL_ADC_CHN_AIN2: case HAL_ADC_CHN_AIN3: case HAL_ADC_CHN_AIN4: case HAL_ADC_CHN_AIN5: case HAL_ADC_CHN_AIN6: case HAL_ADC_CHN_AIN7: adcChannel <<= channel; break; case HAL_ADC_CHN_A0A1: adcChannel = HAL_BITS_CHN_A0A1; break; case HAL_ADC_CHN_A2A3: adcChannel = HAL_BITS_CHN_A2A3; break; case HAL_ADC_CHN_A4A5: adcChannel = HAL_BITS_CHN_A4A5; break; case HAL_ADC_CHN_A6A7: adcChannel = HAL_BITS_CHN_A6A7; break; default: adcChannel = 0; break; } /* save the current pad setting of the PortA pin */ padConfig = IOCPadConfigGet(GPIO_A_BASE, adcChannel); /* save the current gpio setting of the PortA pin */ dirConfig = GPIODirModeGet(GPIO_A_BASE, adcChannel); /* set the PortA pin to Analog */ IOCPadConfigSet(GPIO_A_BASE, adcChannel, IOC_OVERRIDE_ANA); /* set the PortA pin direction to input */ GPIODirModeSet(GPIO_A_BASE, adcChannel, GPIO_DIR_MODE_IN); /* Convert resolution to decimation rate */ switch (resolution) { case HAL_ADC_RESOLUTION_8: resbits = HAL_ADC_DEC_064; break; case HAL_ADC_RESOLUTION_10: resbits = HAL_ADC_DEC_128; break; case HAL_ADC_RESOLUTION_12: resbits = HAL_ADC_DEC_256; break; case HAL_ADC_RESOLUTION_14: default: resbits = HAL_ADC_DEC_512; break; } /* writing to this register starts the extra conversion */ ADCCON3 = channel | resbits | adcRef; /* Wait for the conversion to be done */ while (!(ADCCON1 & HAL_ADC_EOC)); /* Set the pad configuration to previous value*/ IOCPadConfigSet(GPIO_A_BASE, adcChannel, padConfig); /* Set the GPIO direction to previous value*/ GPIODirModeSet(GPIO_A_BASE, adcChannel, dirConfig); /* Read the result */ reading = (int16) (ADCL); reading |= (int16) (ADCH << 8); /* Enable interrupts */ HAL_EXIT_CRITICAL_SECTION(s); /* Treat small negative as 0 */ if (reading < 0) reading = 0; switch (resolution) { case HAL_ADC_RESOLUTION_8: reading >>= 8; break; case HAL_ADC_RESOLUTION_10: reading >>= 6; break; case HAL_ADC_RESOLUTION_12: reading >>= 4; break; case HAL_ADC_RESOLUTION_14: default: reading >>= 2; break; } #else /* unused arguments */ (void) channel; (void) resolution; #endif return ((uint16)reading); }