diff --git a/source/lib/hal_mcu/hal_mcu.c b/source/lib/hal_mcu/hal_mcu.c index 43be34852b99b8fac3a81ed9ea65e4f3404701a7..51f9bb56d869e80a03a896afe979af81511bfdfb 100644 --- a/source/lib/hal_mcu/hal_mcu.c +++ b/source/lib/hal_mcu/hal_mcu.c @@ -168,55 +168,7 @@ void msp_setup(void) { #endif #if defined (__MSP430FR5994__) -//Define the Main Clock setup options. Done here for the delay function setup -#define DCO_RANGE_SEL DCORSEL -#define DCO_FREQ_SEL DCOFSEL_4 -#define F_CPU_SCALE DIVM_0 - -/** - * @defgroup Delay_function - * @brief Directives to create the _delay_us macro. - * @{ - */ -/** - * @def _delay_us(__us) - * @brief Delay for a specified time using the __delay_cycles primitive. - * - * @param[in] __us - time to delay for - * @return none - * @note Assumes using the internal DCO. Will need modification to work with - * XTAL. VLO is two innacurate (6%) for this to be a good delay anyway. - * Max possible delay is (2**32-1)*F_CPU - * That would be 178s for 24MHz. - * helped by https://docs.microsoft.com/en-us/cpp/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp?view=vs-2019 - * and https://github.com/ab2tech/msp430/blob/master/include/msp/delay.h - * - */ -#if DCO_RANGE_SEL==DCORSEL - #define F_CPU_RANGE 16000000UL -#else - #define F_CPU_RANGE 5330000UL -#endif - -#if DCO_FREQ_SEL==DCOFSEL_0 - #define F_CPU F_CPU_RANGE*1.0 -#elif DCO_FREQ_SEL==DCOFSEL_1 - #define F_CPU F_CPU_RANGE*1.25 -#elif DCO_FREQ_SEL==DCOFSEL_2 - #define F_CPU F_CPU_RANGE*1.0 -#else //DCO_FREQ_SEL==DCOFSEL_3 - #define F_CPU F_CPU_RANGE*1.5 -#endif - -#define F_CPU_SCALED_US F_CPU/(1<<F_CPU_SCALE)/1000000.0 - /* Number of CPU cycles per us */ - -#define _delay_us(__us) \ - if((uint32_t) (F_CPU_SCALED_US * __us) != F_CPU_SCALED_US * __us)\ - __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)+1);\ - else __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)) -/** @} */ - +#include "hal_mcu_fr5.h" /******************************************************************************* * @brief Setup all the peripherals of the MSP430, set the CPU speed to 16MHz, * enable the 250kHz and configure WDT for a 1 sec tick speed. @@ -228,7 +180,6 @@ void msp_setup(void) { * @return none *******************************************************************************/ void msp_setup(void) { - // Enable the interupts on port 2 to catch the user button (TRXEB) BUTTON_DIR &= ~BUTTON_PIN; // input direction @@ -241,7 +192,7 @@ void msp_setup(void) { // Removed XTAL configuration and DCO Fault detection as not on exp430_fr5994 // Unlock CS registers. - CSCTL0_H = 0xA5; + CSCTL0 = CSKEY; // Set DCO to 24MHz. CSCTL1 = DCO_RANGE_SEL + DCO_FREQ_SEL; // ACLK = VLO, SMCLK = MCLK = DCO @@ -251,7 +202,7 @@ void msp_setup(void) { // Power down unused clocks. CSCTL4 = HFXTOFF + VLOOFF; // Lock clock registers. - CSCTL0_H = 0; + CSCTL0 = 0; // Setup Watch dog timer for 0.5 second tick using 16MHz DCO on MSP430FR5994 // WDT 0.5s @ 16mHz, SMCLK, interval timer diff --git a/source/lib/hal_mcu/hal_mcu_fr5.h b/source/lib/hal_mcu/hal_mcu_fr5.h new file mode 100644 index 0000000000000000000000000000000000000000..9c7139bcec19021fb4b22a83fde03461ab11e0f5 --- /dev/null +++ b/source/lib/hal_mcu/hal_mcu_fr5.h @@ -0,0 +1,59 @@ + +//Define the Main Clock setup options. Done here for the delay function setup +#define DCO_RANGE_SEL DCORSEL +#define DCO_FREQ_SEL DCOFSEL_4 +#define F_CPU_SCALE DIVM_0 + +/** + * @defgroup Delay_function + * @brief Directives to create the _delay_us macro. + * @{ + */ +/** + * @def _delay_us(__us) + * @brief Delay for a specified time using the __delay_cycles primitive. + * + * @param[in] __us - time to delay for + * @return none + * @note Assumes using the internal DCO. Will need modification to work with + * XTAL. VLO is two innacurate (6%) for this to be a good delay anyway. + * Max possible delay is (2**32-1)*F_CPU + * That would be 178s for 24MHz. + * helped by https://docs.microsoft.com/en-us/cpp/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp?view=vs-2019 + * and https://github.com/ab2tech/msp430/blob/master/include/msp/delay.h + * + */ +#if DCO_RANGE_SEL==DCORSEL + #if DCO_FREQ_SEL>DCOFSEL_3 + #define F_CPU_RANGE 5333000UL + #else + #define F_CPU_RANGE 5333000UL*1.5 + #endif +#else + #define F_CPU_RANGE 2667000UL +#endif + +#if DCO_FREQ_SEL==DCOFSEL_1 + #define F_CPU F_CPU_RANGE*1.0 +#elif DCO_FREQ_SEL==DCOFSEL_2 + #define F_CPU F_CPU_RANGE*1.313 +#elif DCO_FREQ_SEL==DCOFSEL_3 + #define F_CPU F_CPU_RANGE*1.5 +#elif DCO_FREQ_SEL==DCOFSEL_4 + #define F_CPU F_CPU_RANGE*2.0 +#elif DCO_FREQ_SEL==DCOFSEL_5 + #define F_CPU F_CPU_RANGE*2.62 +#elif DCO_FREQ_SEL==DCOFSEL_6 + #define F_CPU F_CPU_RANGE*3.0 +#else //DCO_FREQ_SEL==DCOFSEL_0 + #define F_CPU 1000000UL +#endif + +#define F_CPU_SCALED_US F_CPU/(1<<F_CPU_SCALE)/1000000.0 + /* Number of CPU cycles per us */ + +#define _delay_us(__us) \ + if((uint32_t) (F_CPU_SCALED_US * __us) != F_CPU_SCALED_US * __us)\ + __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)+1);\ + else __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)) +/** @} */ diff --git a/source/test/min2.c b/source/test/min2.c index ce9e3f50d93210659fbe502247bdc471d7ad1c7a..b191d0c1765d907ca9cd24c4c9a9a8bb2e8f0b35 100644 --- a/source/test/min2.c +++ b/source/test/min2.c @@ -1,10 +1,12 @@ #include "msp430fr5994.h" #include "stdio.h" #include "lib/radio_drv/hal_types.h" +#include "lib/hal_mcu/hal_mcu_fr5.h" /****************************************************************************** * LOCAL FUNCTIONS */ +extern void msp_setup(void); unsigned long volatile time_counter; @@ -29,42 +31,6 @@ unsigned long volatile time_counter; * */ - #define DCO_RANGE_SEL DCORSEL - #define DCO_FREQ_SEL DCOFSEL_4 - #define F_CPU_SCALE DIVM_0 - - #if DCO_RANGE_SEL==DCORSEL - #if DCO_FREQ_SEL>DCOFSEL_3 - #define F_CPU_RANGE 5333000UL - #else - #define F_CPU_RANGE 5333000UL*1.5 - #endif - #else - #define F_CPU_RANGE 2667000UL - #endif - - #if DCO_FREQ_SEL==DCOFSEL_1 - #define F_CPU F_CPU_RANGE*1.0 - #elif DCO_FREQ_SEL==DCOFSEL_2 - #define F_CPU F_CPU_RANGE*1.313 - #elif DCO_FREQ_SEL==DCOFSEL_3 - #define F_CPU F_CPU_RANGE*1.5 - #elif DCO_FREQ_SEL==DCOFSEL_4 - #define F_CPU F_CPU_RANGE*2.0 - #elif DCO_FREQ_SEL==DCOFSEL_5 - #define F_CPU F_CPU_RANGE*2.62 - #elif DCO_FREQ_SEL==DCOFSEL_6 - #define F_CPU F_CPU_RANGE*3.0 - #else //DCO_FREQ_SEL==DCOFSEL_0 - #define F_CPU 1000000UL - #endif - - #define F_CPU_SCALED_US F_CPU/(1<<F_CPU_SCALE)/1000000.0 - /* Number of CPU cycles per us */ - #define _delay_us(__us) \ - if((uint32_t) (F_CPU_SCALED_US * __us) != F_CPU_SCALED_US * __us)\ - __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)+1);\ - else __delay_cycles((uint32_t) ( F_CPU_SCALED_US * __us)) enum state_names {IDLE_RESET, WAIT, OPERATE}; void main (void) { @@ -76,39 +42,11 @@ void main (void) /* Setup MSP specific functions, IO's, timers and WDT */ //Comes from lib/hal_mcu/hal_mcu.c - //msp_setup(); + msp_setup(); // Enable the interupts on port 2 to catch the user button (TRXEB) - BUTTON_DIR &= ~BUTTON_PIN; // input direction - BUTTON_OUT |= BUTTON_PIN; // set high on port - BUTTON_PxIE |= BUTTON_PIN; // enable interupt - BUTTON_PxIES |= BUTTON_PIN; // Hi/lo edge - BUTTON_REN |= BUTTON_PIN; // Pull up resistor - BUTTON_PxIES &= ~BUTTON_PIN; // IFG cleared - - // Removed XTAL configuration and DCO Fault detection as not on exp430_fr5994 - - // Unlock CS registers. - CSCTL0 = CSKEY; - // Set DCO to 24MHz. - CSCTL1 = DCO_RANGE_SEL + DCO_FREQ_SEL; - // ACLK = VLO, SMCLK = MCLK = DCO - CSCTL2 = SELA_1 + SELS_3 + SELM_3; - // ACLK/1, SMCLK/8, MCLK/1 - CSCTL3 = DIVA_0 + DIVS_0 + F_CPU_SCALE; - // Power down unused clocks. - CSCTL4 = HFXTOFF + VLOOFF; - // Lock clock registers. - CSCTL0 = 0; - - // Setup Watch dog timer for 0.5 second tick using 16MHz DCO on MSP430FR5994 - // WDT 0.5s @ 16mHz, SMCLK, interval timer - WDTCTL= WDTPW + WDTSSEL_0 + WDTTMSEL + WDTIS_2; - SFRIE1 |= WDTIE; // Enable WDT interrupt - // Unlock the system. - PM5CTL0 &= ~LOCKLPM5; P1DIR |= BIT0 | BIT1; __enable_interrupt();