Advertisement
gautam023

MSP430 LPM4

Sep 15th, 2023
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.26 KB | None | 0 0
  1. #include "main.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "driverlib.h"
  5.  
  6. extern int mode;
  7. extern int pingHost;
  8.  
  9. uint8_t RXData = 0;                               // UART Receive byte
  10.  
  11. bool status = 1;
  12. bool sleep = 0;
  13. bool wakeup = 0;
  14. bool status2 = 0;
  15. uint16_t SWPressed = 0;
  16.  
  17. void Init_GPIO(void);
  18. void Init_Clock(void);
  19. void Init_UART(void);
  20.  
  21.  
  22. //-----------------------------------------------------------------------------
  23. int _system_pre_init(void)
  24. {
  25.     // Stop Watchdog timer
  26.     WDT_A_hold(__MSP430_BASEADDRESS_WDT_A__);     // Stop WDT
  27.  
  28.     GPIO_setOutputHighOnPin(GPIO_PORT_P4, GPIO_PIN0);
  29.     GPIO_setAsOutputPin(GPIO_PORT_P4, GPIO_PIN0);
  30.  
  31.     /*==================================*/
  32.     /* Choose if segment initialization */
  33.     /* should be done or not. */
  34.     /* Return: 0 to omit initialization */
  35.     /* 1 to run initialization */
  36.     /*==================================*/
  37.     return 1;
  38. }
  39.  
  40.  
  41. void reset_gpio()
  42. {
  43.    
  44.     P1DIR = 0xFF;
  45.     P2DIR = 0xE7;
  46.     P3DIR = 0xFF;
  47.     P4DIR = 0xFF;
  48.  
  49.     P2SEL0 &= ~BIT0;
  50.     P2SEL1 &= ~BIT0;
  51.  
  52.     P2SEL0 &= ~BIT1;
  53.     P2SEL1 &= ~BIT1;
  54.  
  55.     P2SEL0 &= ~BIT5;
  56.     P2SEL1 &= ~BIT5;
  57.  
  58.     P2SEL0 &= ~BIT6;
  59.     P2SEL1 &= ~BIT6;
  60.  
  61.  
  62.     P4SEL0 &= ~BIT0;
  63.     P4SEL1 &= ~BIT0;
  64.  
  65.     PJSEL0 = 0;
  66.     PJSEL1 = 0;
  67. }
  68.  
  69. /*
  70.  * main.c
  71.  */
  72. int main(void) {
  73.  
  74.     Init_Clock();
  75.     Init_GPIO();
  76.     Init_UART();
  77.  
  78.     printf("test \n");
  79.     __delay_cycles(1000);
  80.  
  81.     EUSCI_A_UART_disable(EUSCI_A0_BASE);
  82.  
  83.  
  84.     reset_gpio();
  85.  
  86.     PMMCTL0_H = PMMPW_H;                // Open PMM Registers for write
  87.     PMMCTL0_L &= ~(SVSHE);              // Disable high-side SVS
  88.     PMMCTL0_L |= PMMREGOFF;             // and set PMMREGOFF
  89.     PMMCTL0_H = 0;                      // Lock PMM Registers
  90.  
  91.  
  92.     __bic_SR_register(GIE);
  93.     __bis_SR_register(LPM4_bits);
  94.     __no_operation();
  95.  
  96.  
  97.  
  98.  
  99. }
  100.  
  101. int fputc(int _c, register FILE *_fp)
  102. {
  103.     EUSCI_A_UART_transmitData(EUSCI_A0_BASE, _c);
  104.  
  105.     return((unsigned char)_c);
  106. }
  107.  
  108. int fputs(const char *_ptr, register FILE *_fp)
  109. {
  110.     unsigned int i, len;
  111.  
  112.     len = strlen(_ptr);
  113.  
  114.     for(i=0 ; i<len ; i++)
  115.     {
  116.         fputc(_ptr[i], _fp);
  117.     }
  118.  
  119.     return len;
  120. }
  121. /*
  122.  * GPIO Initialization
  123.  */
  124. void Init_GPIO()
  125. {
  126.     // Set all GPIO pins to output low for low power
  127.  
  128.     GPIO_setAsOutputPin(
  129.             GPIO_PORT_P1,
  130.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  131.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  132.  
  133.     GPIO_setAsOutputPin(
  134.             GPIO_PORT_P2,
  135.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  136.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  137.     GPIO_setAsOutputPin(
  138.             GPIO_PORT_P3,
  139.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  140.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  141.     GPIO_setAsOutputPin(
  142.             GPIO_PORT_P4,
  143.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  144.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  145.  
  146.     GPIO_setAsOutputPin(
  147.             GPIO_PORT_PJ,
  148.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  149.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7 | GPIO_PIN8 | GPIO_PIN9
  150.             | GPIO_PIN10 | GPIO_PIN11 | GPIO_PIN12 | GPIO_PIN13
  151.             | GPIO_PIN14 | GPIO_PIN15);
  152.     // Set all GPIO pins to output low for low power
  153.     GPIO_setOutputLowOnPin(
  154.             GPIO_PORT_P1,
  155.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  156.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  157.     GPIO_setOutputLowOnPin(
  158.             GPIO_PORT_P2,
  159.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  160.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  161.     GPIO_setOutputLowOnPin(
  162.             GPIO_PORT_P3,
  163.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  164.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7);
  165.     GPIO_setOutputLowOnPin(
  166.             GPIO_PORT_P4,
  167.             GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4 | GPIO_PIN5
  168.             | GPIO_PIN6 | GPIO_PIN7);
  169.  
  170.     GPIO_setOutputLowOnPin(
  171.             GPIO_PORT_PJ,
  172.             GPIO_PIN0 | GPIO_PIN1 | GPIO_PIN2 | GPIO_PIN3 | GPIO_PIN4
  173.             | GPIO_PIN5 | GPIO_PIN6 | GPIO_PIN7 | GPIO_PIN8 | GPIO_PIN9
  174.             | GPIO_PIN10 | GPIO_PIN11 | GPIO_PIN12 | GPIO_PIN13
  175.             | GPIO_PIN14 | GPIO_PIN15);
  176.  
  177.     // Configure P2.0 - UCA0TXD and P2.1 - UCA0RXD
  178.     //  GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0);
  179.     //  GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0);
  180.     //
  181.     GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2, GPIO_PIN0, GPIO_SECONDARY_MODULE_FUNCTION);
  182.     GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P2, GPIO_PIN1, GPIO_SECONDARY_MODULE_FUNCTION);
  183.  
  184.  
  185.     // Set PJ.4 and PJ.5 as Primary Module Function Input, LFXT.
  186.     GPIO_setAsPeripheralModuleFunctionInputPin(
  187.             GPIO_PORT_PJ,
  188.             GPIO_PIN4 + GPIO_PIN5,
  189.             GPIO_PRIMARY_MODULE_FUNCTION
  190.     );
  191.  
  192.     // Disable the GPIO power-on default high-impedance mode
  193.     // to activate previously configured port settings
  194.     PMM_unlockLPM5();
  195. }
  196.  
  197. /*
  198.  * Clock System Initialization
  199.  */
  200. void Init_Clock()
  201. {
  202.     // Set DCO frequency to 8 MHz
  203.     CS_setDCOFreq(CS_DCORSEL_0, CS_DCOFSEL_6);
  204.     //Set external clock frequency to 32.768 KHz
  205.     CS_setExternalClockSource(32768, 0);
  206.     //Set ACLK=LFXT
  207.     CS_initClockSignal(CS_ACLK, CS_LFXTCLK_SELECT, CS_CLOCK_DIVIDER_1);
  208.     // Set SMCLK = DCO with frequency divider of 1
  209.     CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
  210.     // Set MCLK = DCO with frequency divider of 1
  211.     CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1);
  212.     //Start XT1 with no time out
  213.     //    CS_turnOnLFXT(CS_LFXT_DRIVE_3);
  214. }
  215.  
  216. /*
  217.  * UART Communication Initialization
  218.  */
  219. void Init_UART()
  220. {
  221.     // Configure USCI_A0 on 115200
  222.     EUSCI_A_UART_initParam param = {0};
  223.     param.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
  224.     param.clockPrescalar = 4;
  225.     param.firstModReg = 5;
  226.     param.secondModReg = 0x55;
  227.     param.parity = EUSCI_A_UART_NO_PARITY;
  228.     param.msborLsbFirst = EUSCI_A_UART_LSB_FIRST;
  229.     param.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT;
  230.     param.uartMode = EUSCI_A_UART_MODE;
  231.     param.overSampling = EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
  232.  
  233.     if(STATUS_FAIL == EUSCI_A_UART_init(EUSCI_A0_BASE, &param))
  234.         return;
  235.  
  236.     EUSCI_A_UART_enable(EUSCI_A0_BASE);
  237.  
  238.     EUSCI_A_UART_clearInterrupt(EUSCI_A0_BASE,
  239.                                 EUSCI_A_UART_RECEIVE_INTERRUPT);
  240.  
  241.     // Enable USCI_A0 RX interrupt
  242.     EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE,
  243.                                  EUSCI_A_UART_RECEIVE_INTERRUPT); // Enable interrupt
  244.  
  245.     // Enable globale interrupt
  246.     //    __enable_interrupt();
  247. }
  248.  
  249. /*
  250.  * USCI_A0 Interrupt Service Routine that receives PC GUI's commands
  251.  */
  252. #pragma vector = USCI_A0_VECTOR
  253. __interrupt void USCI_A0_ISR(void)
  254. {
  255.     switch (__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG)) {
  256.     case USCI_NONE:
  257.         break;
  258.     case USCI_UART_UCRXIFG:
  259.         RXData = EUSCI_A_UART_receiveData(EUSCI_A0_BASE);
  260.         printf(RXData);
  261.         break;
  262.     case USCI_UART_UCTXIFG:
  263.         break;
  264.     case USCI_UART_UCSTTIFG:
  265.         break;
  266.     case USCI_UART_UCTXCPTIFG:
  267.         break;
  268.     }
  269. }
Tags: MSP430
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement