Firmware Code

main.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 * File {main.c}
 *
 ********************************************************************************
 *                       DATA STORAGE TAG FIRMWARE Description
 * This is the main entry point for the DST firmware developed in 2019 as a
 * part of Ali Aljumaili Master Thesis. The firmware is divided into folders for
 * peripherals-drivers, sensor-drivers, storage-drivers and finally, application
 * layer folder where the datalogger and wireless NFC functionalities exist
 ********************************************************************************
//            DST Rev.2.0.0 Simplified Schematics
//
//                   MSP430FR5738
//            ------------------------
//         /|\|                  P1.1|<---NFC_INT0
//          | |                  P1.2|<---MAG-ACC_INT1
//          --|RST                   |
 *            |                      |
//      |---- |PJ.4/XIN    P1.3/TA1.2|--> Acoustic Transmitter
//      32 kHz|                  P2.2|--> FLASH_RST
//      |---- |PJ.5/XOUT         P1.0|--> NFC_RST
 *            |                      |
//     LED1<--|P1.4              P1.6|--> I2C-SDA
//     LED2<--|PJ.0              P1.7|--> I2C-SCL
//     LED3<--|PJ.1                  |
//            |                  P2.1|<-- SPI - SOMI
//  SPI-CS1<--|PJ.2              P2.0|--> SPI - SIMO
//  SPI-CS2<--|PJ.3              P1.5|--> SPI - CLK
//
 */

/* ------------------------------------------------------------------------------------------------
 *                                          Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <core/mcu.h>
#include <core/rtc.h>
#include <fsm.h>

#include <settings.h>
#include <msp430.h>

#include <ctpl.h>
#include <devices/ms58370.h>

#include <stdint.h>


void main(void)
{
    MCU_Init();                    /* Initialize  WDT, GPIO's and Clocks */

    RTC_B_Init();                  /* Starts the Real Time Clock with a default polling Interval */

    Timer_B0_Init();               /* Configures the timer to up mode. */

    I2C_Init();                    /* Enables the I2C Interface eUSCI_B0 */

    MS5837_Init();                 /* Resets the pressure sensor as per datasheet */

    TMP117_Init();

    __enable_interrupt();          /* Enable Global Interrupts for using ISR */


    while(1)
    {

       FSM_Init();                /* Starts the finite-state-machine, program in control of handlers from now on */
    }
}


/*
 * This function will be called before main and initialization of variables.
 * The ctpl_init() function must be called at the start to enable the compute
 * through power loss library.
 */
int _system_pre_init(void)
{
    /* Initialize ctpl library */
    ctpl_init();

    /* Insert application pre-init code here. */

    return 1;
}

// This code is commented, it was used to test power loss  SVSH and SVSL
//void Reset_ISR(void) {
//
//   switch (__even_in_range(SYSRSTIV, SYSRSTIV_PMMKEY))
//   {
//       case SYSRSTIV_NONE:                       // No Interrupt pending
//                               __no_operation();
//                               break;
//       case SYSRSTIV_BOR:                        // SYSRSTIV : BOR
//                               __no_operation();
//                               break;
//       case SYSRSTIV_RSTNMI:                     // SYSRSTIV : RST/NMI
//                               __no_operation();
//                               break;
//       case SYSRSTIV_DOBOR:                      // SYSRSTIV : Do BOR
//                               __no_operation();
//                               break;
//       case SYSRSTIV_LPM5WU:                     // SYSRSTIV : Port LPM5 Wake Up
//                               __no_operation();
//                               break;
//       case SYSRSTIV_SECYV:                      // SYSRSTIV : Security violation
//                               __no_operation();
//                               break;
//       case SYSRSTIV_SVSLIFG:                       // SYSRSTIV : SVSL
//                               __no_operation();
//                               break;
//       case SYSRSTIV_SVSHIFG:                       // SYSRSTIV : SVSH
//                               __no_operation();
//                               break;
//
//       case SYSRSTIV_DOPOR:                      // SYSRSTIV : Do POR
//                               __no_operation();
//                               break;
//       case SYSRSTIV_WDTTO:                      // SYSRSTIV : WDT Time out
//                               __no_operation();
//                               break;
//       case SYSRSTIV_WDTKEY:                     // SYSRSTIV : WDTKEY violation
//                               __no_operation();
//                               break;
//
//       default: break;
//   }
//}

Core Layer

i2c.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//#############################################################################

//
//! \file   i2c.c
//!
//! \brief  please read i2c.h
//!
//! (C) Copyright 2019, Ali Aljumaili
//
//#############################################################################



//*****************************************************************************
// the includes
//*****************************************************************************
#include <core/i2c.h>
#include <driverlib.h>
#include <settings.h> // for global buffer

void I2C_Init(void)
{
    EUSCI_B_I2C_initMasterParam Settings_I2C_Parm = {0};
    Settings_I2C_Parm.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
    Settings_I2C_Parm.i2cClk = CS_getSMCLK();
    Settings_I2C_Parm.dataRate = EUSCI_B_I2C_SET_DATA_RATE_100KBPS;
    Settings_I2C_Parm.byteCounterThreshold = 0;
    Settings_I2C_Parm.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
    EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &Settings_I2C_Parm); /* Sets the Master Configurations defined in settings.h*/

    EUSCI_B_I2C_enable(EUSCI_B0_BASE); /* Enable I2C by reseting the UCSWRST bit */
}

void I2C_Set_SlaveAddr(uint8_t slaveAddr)
{
    EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,slaveAddr);
}


void I2C_Send_Single_Byte(uint8_t * txData)
{
    EUSCI_B_I2C_masterSendSingleByte(EUSCI_B0_BASE,&txData);
}

void I2C_Enable_Interrupt(void)
{
    //UCB0IE |= UCSTTIE; /* Enable Start Condition Interrupt*/
    //UCB0IE |= UCTXIE0; /* Enable Transmit Interrupt*/

    UCB0IE |= UCRXIE0;  /* Enable Receive Interrupt*/
}


#pragma vector = USCI_B0_VECTOR
__interrupt void USCIB0_ISR(void)
{
    switch(__even_in_range(UCB0IV,0x1E))
    {
    case 0x00: break; // Vector 0: No interrupts break;
    case 0x02: break; // Vector 2: ALIFG break;
    case 0x04: UCB0CTL1 |= UCTXSTT; // I2C start condition
    break; // Vector 4: NACKIFG break;
    case 0x06: break; // Vector 6: STTIFG break;
    case 0x08: break; // Vector 8: STPIFG break;
    case 0x0a:

        break; // Vector 10: RXIFG3 break;
    case 0x0c: break; // Vector 14: TXIFG3 break;
    case 0x0e:
        buffer = EUSCI_B_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE); // Reads the first byte of the buffer and store it into the buffer
        EUSCI_B_I2C_masterReceiveMultiByteFinish(EUSCI_B0_BASE); //stop and ignore the input.
        break; // Vector 16: RXIFG2 break;
    case 0x10: break; // Vector 18: TXIFG2 break;
    case 0x12:
        buffer = EUSCI_B_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE); // Reads the first byte of the buffer and store it into the buffer
        EUSCI_B_I2C_masterReceiveMultiByteFinish(EUSCI_B0_BASE); //stop and ignore the input.
        break; // Vector 20: RXIFG1 break;
    case 0x14: break; // Vector 22: TXIFG1 break;
    case 0x16:
        buffer = EUSCI_B_I2C_masterReceiveMultiByteNext(EUSCI_B0_BASE); // Reads the first byte of the buffer and store it into the buffer
        EUSCI_B_I2C_masterReceiveMultiByteFinish(EUSCI_B0_BASE); //stop and ignore the input.
        __no_operation();
        break; // Vector 24: RXIFG0
    case 0x18: break; // Vector 26: TXIFG0 break;
    case 0x1a: break; // Vector 28: BCNTIFG break;
    case 0x1c: break; // Vector 30: clock low timeout break;
    case 0x1e: break; // Vector 32: 9th bit break;
    default: break;
    }
}

i2c.h

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//#############################################################################

//
//! \file   i2c.h
//!
//! \brief  This is the i2c interface driver, it is dependent on driverlib for
//          low level register configurations.
//          This modules configures the eUSCIB0, transmit data over I2C bus by
//          the way of interrupt and read data from the I2C by the way of interrupt.
//          This module also configures the eUSCIB0 clock source, frequency,
//          peripheral address and initialize I2C ports.
// (C) Copyright 2019, Ali Aljumaili
//
//#############################################################################


#ifndef CORE_I2C_H_
#define CORE_I2C_H_

//*****************************************************************************
// the includes
//*****************************************************************************
#include <msp430.h>
#include <stdint.h>



//! \brief This defines the I/O ports needed by the USCIBO peripheral.
//  these can be edited in case of changing port or MCU.
#define I2C_PORT_I2C_OUT    P1OUT
#define I2C_PORT_I2C_DIR    P1DIR
#define I2C_PORT_I2C_SEL    P1SEL0
#define I2C_SDA_PIN         BIT6
#define I2C_SCL_PIN         BIT7


typedef unsigned char bool_t;

//! \brief This defines the I/O ports needed by the USCIBO peripheral. It makes
//! sense to declare the port here at the top so in case they change, they are
//!edited only here.
#define PORT_I2C_OUT    P1OUT
#define PORT_I2C_DIR    P1DIR
#define PORT_I2C_SEL    P1SEL0
#define SDA BIT6
#define SCL BIT7

//! \brief These defines are needed to keep track of data during read and write process.
#define START           1
#define CONTINUE        0
#define FAIL            0
#define PASS            1
#define SET             1
#define CLEAR           0
#define I2C_NACK_RCVD   2
#define I2C_TRANSMIT    3
#define LPM_MODE        LPM0_bits
#define I2C_ALLOW       (I2C_READY_IN & I2C_READY_PIN)

uint8_t RF430_I2C_State;
uint8_t RF430_I2C_Start;            //AK 10-23-2013



//! \brief This declares both the register address and data.
//!
typedef struct
{
    unsigned char configReg;
    unsigned char data;
} i2cCmd_t;


//*****************************************************************************
//
//! \brief  This function is called only once and it is called when the MSP430
//!         is initializing. Furthermore, the function is called indirectly by
//!         the I2C peripheral interface module when it itself is initializing.
//!         This function configures the USCIBO peripheral to act as a I2C peripheral,
//!         set to master transmitting mode, with SMCLK clock frequency
//!         with auto detection of frequency and set the daterate to 100 kbps with no
//!         automatic stop bit generation.
//!
//! \param  none
//
//! \return None
//
//*****************************************************************************

void I2C_Init(void);

//*****************************************************************************
//
//! \brief  This function is called only once and it is called when the MSP430
//!         is initializing. This function select I2C from eUSCIB (P1.6 and P1.7 as)
//! \param  none
//
//! \return None
//
//*****************************************************************************
void I2C_Init_Ports(void);

//*****************************************************************************
//
//! \brief  This function sets the slave address for the eUSCB_0 module.
//!
//! \param slaveAddr   Destination address of the remote IC
//
//! \return None
//
//*****************************************************************************
void I2C_Set_SlaveAddr(uint8_t slaveAddr);


//*****************************************************************************
//
//! \brief  This function transmits a single byte on the I2C bus, the slave
//!         address must be configured before calling this function.
//! \param txData Pointer to a buffer which contains the data to be written
//
//! \return None
//
//*****************************************************************************
void I2C_Send_Single_Byte(uint8_t * txData);

#endif /* CORE_I2C_H_ */

spi.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
               {spi.c} - Driver for the MSP430FR5738 SPI Interface
*****************************************************************************
*/

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <core/spi.h>
#include <driverlib.h>

void SPI_Init(void)
{
    /* initialize eUSCI SPI master mode */
    EUSCI_A_SPI_masterInit(EUSCI_A0_BASE,
                           EUSCI_A_SPI_CLOCKSOURCE_SMCLK,
                           12000000, /* SMCLK Speed */
                           1000000,  /* SPI Bus Bitrate */
                           EUSCI_A_SPI_LSB_FIRST,
                           EUSCI_A_SPI_PHASE_DATA_CHANGED_ONFIRST_CAPTURED_ON_NEXT,
                           EUSCI_A_SPI_CLOCKPOLARITY_INACTIVITY_LOW,
                           EUSCI_A_SPI_3PIN);

    /* enable eUSCI SPI */
    EUSCI_A_SPI_enable(EUSCI_A0_BASE);
}


/*  ======== eUSCI_A0 Interrupt Service Routine ========
*/
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR_HOOK(void)
{
   /* USER CODE START (section: USCI_A0_ISR_HOOK) */
   /* replace this comment with your code */
   /* USER CODE END (section: USCI_A0_ISR_HOOK) */
}

spi.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
               {spi.h} - Driver for the MSP430FR5738 SPI Interface
*****************************************************************************
*/

#ifndef CORE_SPI_H_
#define CORE_SPI_H_
/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 *                                           Enums
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 *                                           Defines
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 *                                      Function Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void SPI_Init(void);


#endif /* CORE_SPI_H_ */

mcu.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
                  {mcu.c} - Driver for the MSP430FR5738 Peripherals
*****************************************************************************
*/

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <core/mcu.h>
#include <driverlib/MSP430FR57xx/inc/hw_memmap.h>
#include <driverlib/MSP430FR57xx/cs.h>
#include <ctpl.h>

/* Sets up the MSP430FR738 GPIO, Watchdog and Clocks */
void MCU_Init(void){

    MCU_Stop_Watchdog();                    /* Stops the WDT*/
    MCU_Configure_GPIO();                   /* Setup GPIOs */
    MCU_Configure_Clocks();                 /* Configure DCO and ACLK */
}

/* Stops the watchdog timer to avoid timing out during start-up */
void MCU_Stop_Watchdog(void)
{
     WDTCTL = WDTPW | WDTHOLD;              /* Stop watchdog timer from
                                            timing out during initial start-up */
}

/* Initialize MSP430 General Purpose Input Output Ports
 *  The GPIO registers should be set in a specific order:
 *   PxOUT --> PxSEL or PxSELx -->  PxDIR -->  PxREN -->  PxIES --> PxIFG -->  PxIE
 *   (see section 8.2.6 of the MSP430 User's manual )
*/
void MCU_Configure_GPIO(void)
{
/*================================ Port 1 GPIO Setup ================================*/
    P1OUT = MCU_NFC_RST_PIN;                        /* Pins Output Registers PORT1 */

    P1SEL1 = (MCU_I2C_SDA_PIN | MCU_I2C_SCL_PIN /* Port 1 Port Select Register */
            | MCU_SPI_CLK_PIN);                 /* Select I2C and SPI */

    P1DIR = (MCU_ACC_MAG_PIN|                   /* PORT 1 Pins Direction Registers */
            MCU_ACOUSTIC_TRANSMITTER_PIN);      /* Set pins as output */

    P1REN = MCU_NFC_RST_PIN;                    /* PORT 1 Enable Pullup for NFC_RST */

    P1IES = (MCU_NFC_INT_PIN |                  /* PORT1 Interrupt Edge Select Register */
            MCU_ACC_MAG_INT_PIN);

    P1IFG = 0;                                  /* Port 1 Interrupt Flag Register */

    P1IE = (MCU_NFC_INT_PIN |
            MCU_ACC_MAG_INT_PIN);               /* Port 1 Enable Interrupts for pins */

    /*================================ Port 2 GPIO Setup ================================*/
    P2OUT = MCU_FLASH_RST_PIN;                  /* Pins Output Registers PORT2 */

    P2SEL1 = (MCU_SPI_SIMO_PIN |
            MCU_SPI_SOMI_PIN);                  /* Port 2 Port Select Register */

    P2DIR = 0;                                  /* Set All PORT2 Pins as Input*/

    P2REN = MCU_FLASH_RST_PIN;                  /* Enable Pullup for FLASH */

    P2IES = 0;                                  /* Interrupt Edge Select Register */

    P2IFG = 0;                                  /* Interrupt Flag Register */

    /*================================ Port J GPIO Setup ================================*/

    PJSEL0  =  XIN_PIN | XOUT_PIN;              /* Set up XIN and XOUT for bypass crystal mode*/

    //PJSEL0 |= BIT0 | BIT1; /* Output MCLK and SMCLK to J0 and J1 (only for testing)*/

    PJDIR |= BIT2;                              /* Set pin as output for ACLK */
    PJSEL0 |= SELECT_ACLK_OUTPUT_PIN_2;         /* Set PJ.2 as ACLK for testing */
    PJSEL1= 0; // for test

    PJDIR   = LED2 |LED3;                       /* Set pins as output for LED2 and LED3 */

   /* Disable the GPIO power-on default high-impedance mode to activate previously configured port settings */
    PM5CTL0 &= ~LOCKLPM5;
}


/* Sets up the DCO, SMCLK and  XT1 as a source for ACLK*/
void MCU_Configure_Clocks(void)
{
    CS_setExternalClockSource(32768,0);             /*   XT1 Frequency -  32768 Hz,
                                                     *   XT2 Frequency -  0 Hz     */

    CS_turnOnXT1WithTimeout(XT1DRIVE_3,100000);     /* Start XT1 crystal in low frequency mode */

    CS_setDCOFreq (CS_DCORSEL_1, CS_DCOFSEL_3);     /* Set DCO frequency to 24 MHz */
    //CS_setDCOFreq (CS_DCORSEL_1, CS_DCOFSEL_1);   /* Set DCO frequency to 20 MHz */
    //CS_setDCOFreq (CS_DCORSEL_0, CS_DCOFSEL_3);   /* Set DCO frequency to 8 MHz */

    CS_initClockSignal(CS_MCLK, CS_DCOCLK_SELECT,   //* Setting MCLK source from CS_DCOCLK_SELECT
                     CS_CLOCK_DIVIDER_1);           //* with the divider of CS_CLOCK_DIVIDER_1.

    CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT,  //* Setting SMCLK source from CS_DCOCLK_SELECT with the
                       CS_CLOCK_DIVIDER_2);         //* the divider of CS_CLOCK_DIVIDER_2 frequency 24(DCO)/2 = 12 MHz

    CS_initClockSignal(CS_ACLK, CS_XT1CLK_SELECT,  //* Setting ACLK source from CS_XT1CLK_SELECT
                        CS_CLOCK_DIVIDER_1);        //* with the divider of CS_CLOCK_DIVIDER_1.

    CS_clearAllOscFlagsWithTimeout(100000);        /* Clears all oscillator fault flags including
                                                    * global oscillator fault flag before switching clock sources. */
}


///* Calibrate DCO to a certain frequency */
//void MCU_Calibrate_DCO(uint16_t delta)
//{
//    uint16_t Compare, Oldcapture = 0;
//
//    /* Set Timer */
//  //  BCSCTL1 |= DIVA_3;                        // ACLK = LFXT1CLK/8 */
//  //  TACCTL0 = CM_1 + CCIS_1 + CAP;            /* CAPture, ACLK */
//  //  TACTL = TASSEL_2 + MC_2 + TACLR;          /* SMCLK, count-mode, clear */
//
//    while (1)
//    {
//        while (!(CCIFG & TACCTL0));            /* Wait until capture occured */
//        TACCTL0 &= ~CCIFG;                     /* Capture occured, clear flag */
//        Compare = TACCR0;                      /* Get current captured SMCLK */
//        Compare = Compare - Oldcapture;        /* SMCLK difference */
//        Oldcapture = TACCR0;                   /* Save current captured SMCLK */
//
//        if (delta == Compare)
//            break;                             // If equal, leave "while(1)"
//        else if (delta < Compare)
//        {
//            DCOCTL--;                          /* DCO is too fast, slow it down */
//            if (DCOCTL == 0xFF)                /* Did DCO roll under? */
//                if (BCSCTL1 & 0x0f)
//                    BCSCTL1--;                 /* Select lower RSEL */
//        }
//        else
//        {
//            DCOCTL++;                          /* DCO is too slow, speed it up */
//
//            if (DCOCTL == 0x00)                /* Did DCO roll over? */
//                if ((BCSCTL1 & 0x0f) != 0x0f)
//                    BCSCTL1++;                 /* Select higher RSEL */
//        }
//    }
//    TACCTL0 = 0;                               /* Stop TACCR0 */
//    TACTL = 0;                                 /* Stop Timer_A */
//    BCSCTL1 &= ~DIVA_3;                        /* ACLK = LFXT1CLK */
//
//    for (i = 0; i < 0x4000; i++);              /* SW Delay */
//}

mcu.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
                  {mcu.h} - Driver for the MSP430FR5738 Peripherals
*****************************************************************************
*/

#ifndef CORE_MCU_H_
#define CORE_MCU_H_

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <msp430.h>

/* ------------------------------------------------------------------------------------------------
 *                                           Defines
 * ------------------------------------------------------------------------------------------------
 */
#define MCU_SPI_CLK_PIN BIT5
#define MCU_SPI_SOMI_PIN BIT1
#define MCU_SPI_SIMO_PIN BIT0

#define MCU_I2C_SDA_PIN BIT6
#define MCU_I2C_SCL_PIN BIT7

#define MCU_NFC_INT_PIN     BIT1
#define MCU_ACC_MAG_INT_PIN BIT2

#define MCU_NFC_RST_PIN BIT0
#define MCU_FLASH_RST_PIN BIT2

#define MCU_ACC_MAG_PIN BIT2
#define MCU_ACOUSTIC_TRANSMITTER_PIN BIT3

#define LED1 BIT4
#define LED2 BIT0
#define LED3 BIT1

#define LED1_OUT P1OUT
#define LED2_OUT PJOUT
#define LED3_OUT PJOUT

#define LED1_DIR P1DIR
#define LED2_DIR PJDIR
#define LED3_DIR PJDIR

#define LED1_POWER_ON   LED1_DIR |=LED1; LED1_OUT|=LED1;
#define LED1_POWER_OFF  LED1_OUT &= ~LED1;
#define LED2_POWER_ON   LED2_DIR |=LED2; LED2_OUT|=LED2;
#define LED2_POWER_OFF  LED2_OUT &= ~LED2;
#define LED3_POWER_ON   LED3_DIR |=LED3; LED3_OUT|=LED3;
#define LED3_POWER_OFF  LED3_OUT &= ~LED3;

#define XIN_PIN  BIT4
#define XOUT_PIN BIT5

#define SELECT_ACLK_OUTPUT_PIN_2 BIT2
/* ------------------------------------------------------------------------------------------------
 *                                           Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void MCU_Stop_Watchdog(void);
void MCU_Configure_GPIO(void);
void MCU_Init(void);
void MCU_Configure_Clocks(void);
//void MCU_Calibrate_DCO(uint16_t delta);

#endif /* CORE_MCU_H_ */

timer.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
               {timer.h} - Driver for timer MSP430FR5738
*****************************************************************************
*/

#ifndef CORE_TIMER_H_
#define CORE_TIMER_H_

/* ------------------------------------------------------------------------------------------------
 *                                      Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <msp430.h>
#include <stdint.h>


#define TIMER_B_STOP_MODE                                                  MC_0
#define TIMER_B_UP_MODE                                                    MC_1
#define TIMER_B_CONTINUOUS_MODE                                            MC_2
#define TIMER_B_UPDOWN_MODE                                                MC_3


/* ------------------------------------------------------------------------------------------------
 *                                      Function Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void Low_Power_Delay_ms(uint32_t ms);
void Timer_B0_Init(void);

#endif /* CORE_TIMER_H_ */

timer.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
               {timer.h} - Driver for timer MSP430FR5738
*****************************************************************************
*/

#ifndef CORE_TIMER_H_
#define CORE_TIMER_H_

/* ------------------------------------------------------------------------------------------------
 *                                      Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <msp430.h>
#include <stdint.h>


#define TIMER_B_STOP_MODE                                                  MC_0
#define TIMER_B_UP_MODE                                                    MC_1
#define TIMER_B_CONTINUOUS_MODE                                            MC_2
#define TIMER_B_UPDOWN_MODE                                                MC_3


/* ------------------------------------------------------------------------------------------------
 *                                      Function Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void Low_Power_Delay_ms(uint32_t ms);
void Timer_B0_Init(void);

#endif /* CORE_TIMER_H_ */

Application Layer

fsm.c

/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
                  {fsm.c} - Driver for the Finite State Machine
*****************************************************************************
*/

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <fsm.h>
#include <settings.h>
#include <nfc_request_handler.h>
#include <ctpl.h>
#include <ms58370.h>
/*For Testing, shall be moved to an event handler */

void FSM_Init(void)
{
    switch(CurrentState_t)
    {
    case Start:
        //MAKE LED BLINK

        I2C_Send_Single_Byte(MS5837_CONVERT_D1_8192);
        //__delay_cycles(24000*2); // wait 3 ms for the pressure sensor to get restarted. This should be accomplished by timers now.

        MS5738_Convert_And_Read_ADC();

        //
        //   __delay_cycles(1000000);               // Delay between transmissions

        //   while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE));
        //   __delay_cycles(1000000);               // Delay between transmissions
        //        //

        /* Enter into LPM3.5 with restore on reset disabled. The RTC interrupt will wake up the MCU */
        ctpl_enterLpm35(CTPL_DISABLE_RESTORE_ON_RESET);
        break;
    case Power_Lost:

        break;

    case Data_logger_Init:

        break;

    case Wait_For_Command:

        //check NFC
        break;

    case Process_command:

        /* Switch Depending On Received Command */

        switch(Command_Received_t)
        {
        case Start_CMD:

            break;

        case Stop_CMD:

            break;

        case Clear_Data_CMD:

            break;

        case Reset_CMD:

            break;

        }

        break;
//
//    default:
//        CurrentState_t = Wait_For_Command;
//        break;
    }
}


fsm.h

/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
****************************************************************************
                  {fsm.h} - Driver for the Finite State Machine
*****************************************************************************
*/

#ifndef APPLICATION_LAYER_FSM_H_
#define APPLICATION_LAYER_FSM_H_

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <msp430.h>

/* ------------------------------------------------------------------------------------------------
 *                                        Structs and Enums
 * ------------------------------------------------------------------------------------------------
 */
typedef enum {
    Start,
    Power_Lost,
    Data_logger_Init,
    Wait_For_Command,
    Process_command,
} States_t;

/* ------------------------------------------------------------------------------------------------
 *                                           Defines
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 *                                           Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void FSM_Init(void);
#endif /* APPLICATION_LAYER_FSM_H_ */

settings.c

/*
 * settings.c
 *
 *  Created on: May 16, 2019
 *      Author: ali
 */

#include <settings.h>


States_t CurrentState_t = Start;

Datalogger_Commands_t Command_Received_t;

uint32_t buffer;

setings.h

/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 *****************************************************************************
               {settings.h} - Application Settings (mostly global)
 *****************************************************************************
 */

#ifndef APPLICATION_LAYER_SETTINGS_H_
#define APPLICATION_LAYER_SETTINGS_H_
/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include "application_layer/fsm.h"
#include "stdint.h"
#include "driverlib.h"
#include <nfc_request_handler.h>

typedef struct Settings_Global_t {
    uint8_t ui8PollingInterval; /* Polling Interval used by RTC for alarm in Minutes */
}Settings_Global_t;


extern uint32_t buffer; //global variable for testing to store I2C data


/* ------------------------------------------------------------------------------------------------
 *                                         Global States
 * ------------------------------------------------------------------------------------------------
 */

extern States_t CurrentState_t;

/* Used in FSM */
#pragma NOINIT (Command_Received_t);
extern Datalogger_Commands_t Command_Received_t;

//States_t CurrentState_t = Start;
//DEBUGGER MODE STATE
//FLAGS


#endif /* APPLICATION_LAYER_SETTINGS_H_ */

Devices Layer

tmp117.c

/**
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 * File {tmp117.c}
 */

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <devices/tmp117.h>


void TMP117_Init(void)
{
    // set temperature offset
    // set low temp limit
    // set high temp limit
}

//constant uint8_t TMP117_DeviceAddress=TMP117_DeviceID1;


//float uint2floatconvertdata(uint8_t HiByte, uint8_t LoByte) {
//    float temp;
//    temp = (float) (HiByte << 8 | LoByte);
//    temp *= 0.0078125;
//    return temp;
//}

tmp117.h

/**
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 * File {tmp117.h}
 */

#ifndef DEVICES_TMP117_H_
#define DEVICES_TMP117_H_
/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */

/* ------------------------------------------------------------------------------------------------
 *                                           Defines
 * ------------------------------------------------------------------------------------------------
 */
/* TMP117 Addresses */
#define TMP117_ADDR                0x48            /*  ADDR connected to GND */
#define TMP117_DeviceID2           0x49            /*  Vcc */
#define TMP117_DeviceID3           0x4A            /*  SDA */
#define TMP117_DeviceID4           0x4B            /*  SCL */

/* TMP117 Registers */
#define TMP117_REG_TEMPERATURESULT 0x00
#define TMP117_REG_CONFIGURATION 0x01
#define TMP117_REG_ALERTHIGH 0x02
#define TMP117_REG_ALERTLOW 0x03
#define TMP117_REG_EEPROMUNLOCK 0x04
#define TMP117_REG_EEPROM1 0x05
#define TMP117_REG_EEPROM2 0x06
#define TMP117_REG_OFFSET 0x07
#define TMP117_REG_EEPROM3 0x08
#define TMP117_REG_DEVICEID 0x0F



//from below is unverfied.
#define TMP117CONFIGREGISTERPOL 8
#define TMP117CONFIGREGISTERTnA 16
#define TMP117CONFIGREGISTERAVG0 32
#define TMP117CONFIGREGISTERAVG1 64
#define TMP117CONFIGREGISTERCONV0 128

#define TMP117CONFIGREGISTERCONV1 1
#define TMP117CONFIGREGISTERCONV2 2
#define TMP117CONFIGREGISTERMOD0 4
#define TMP117CONFIGREGISTERMOD1 8
#define TMP117CONFIGREGISTEREEPROMBUSY 16
#define TMP117CONFIGREGISTERDATAREADY 32
#define TMP117CONFIGREGISTERLOWALERT 64
#define TMP117CONFIGREGISTERHIGHALERT 128

#define TMP117SHUTDOWN 0x04
#define TMP117ONESHOT 0x06


/* ------------------------------------------------------------------------------------------------
 *                                      Function Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void TMP117_Init(void);

#endif /* DEVICES_TMP117_H_ */

ms58370.c

/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 *****************************************************************************
             {ms5738.c} - Driver for the pressure sensor
 *****************************************************************************
/*

/* ------------------------------------------------------------------------------------------------
 *                                           Includes
 * ------------------------------------------------------------------------------------------------
 */
#include <core/i2c.h>
#include <devices/ms58370.h>
#include <driverlib.h> //TO BE MOVED to I2C



/**
 * \brief Configures the I2C master to be used with the MS5837 device.
 */
void MS5837_Init(void)
{
    I2C_Set_SlaveAddr(MS5837_ADDR);     /* Set I2C Slave Address to MS5837 ADDR */

    I2C_Send_Single_Byte(MS5837_RESET); /* S-- > Device Address --> W --> A --> RESET COMMAND --> A --> Stop */
}

//uint32_t *adcValue
void MS5837_Convert_And_Read_ADC(void)
{

    //I2C_Set_SlaveAddr(MS5837_ADDR);     /* Set I2C Slave Address to MS5837 ADDR */
   // I2C_Send_Single_Byte(MS5837_RESET); /* S-- > Device Address --> W --> A --> RESET COMMAND --> A --> Stop */

    //__delay_cycles(48000); // wait 2 ms after reset.

    //(1/24MHz)*X=1ms,          MCLK= 24 Mhz (MCLK is the source for delay cycles)
    //X=24000 1 ms

    I2C_Send_Single_Byte(MS5837_CONVERT_D1_8192); //issue command to start ADC pressure conversion D1

    __delay_cycles(432000); // delay for 18 ms

    I2C_Send_Single_Byte(MS5837_ADC_READ); //issue the command for ADC read

    __delay_cycles(432000); // delay for 18 ms

    EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE); //set I2C in Read Mode

    //read the 3 byte buffer.
    UCB0IE |=  UCRXIE0;               //receive interrupt enable
    // then check interrupt
    __no_operation();
}


void MS5837_Calculate()
{
    int32_t dT = 0;
    int64_t SENS = 0;
    int64_t OFF = 0;
    int32_t SENSi = 0;
    int32_t OFFi = 0;
    int32_t Ti = 0;
    int64_t OFF2 = 0;
    int64_t SENS2 = 0;


}

ms58370.h

/*
 * Use of this software is copyright Ali Aljumaili and licensed under
 * the MIT license found in the LICENSE file associated to this repository.
 * Copyright (c) 2019, Ali Aljumaili
 *****************************************************************************
             {ms5738.h} - Driver for the pressure sensor
 *****************************************************************************
*/

#ifndef DEVICES_MS58370_H_
#define DEVICES_MS58370_H_


/* ------------------------------------------------------------------------------------------------
 *                                           Constants
 * ------------------------------------------------------------------------------------------------
 */
//const float MS5837_PA = 100.0f;
//const float MS5837_BAR = 0.001f;
//const float MS5837_MBAR = 1.0f;



/* ------------------------------------------------------------------------------------------------
 *                                           Defines
 * ------------------------------------------------------------------------------------------------
 */
/* MS5737 Device Commands */
#define MS5837_ADDR                                                 0x76
#define MS5837_RESET                                                0x1E
#define MS5837_ADC_READ                                             0x00
#define MS5837_PROM_READ                                            0xA0
#define MS5837_START_PRESSURE_ADC_CONVERSION                        0x40
#define MS5837_START_TEMPERATURE_ADC_CONVERSION                     0x50


#define MS5837_CONVERT_D1_256                                       0x40
#define MS5837_CONVERT_D1_1024                                      0x44
#define MS5837_CONVERT_D1_2048                                      0x46
#define MS5837_CONVERT_D1_4096                                      0x48
#define MS5837_CONVERT_D1_8192                                      0x4A

#define MS5837_CONVERT_D2_256                                       0x50
#define MS5837_CONVERT_D2_1024                                      0x54
#define MS5837_CONVERT_D2_2048                                      0x56
#define MS5837_CONVERT_D2_4096                                      0x58
#define MS5837_CONVERT_D2_8192                                      0x5A

/* PROM READ ADDRESSES */
#define MS5837_PROM_ADDRESS_0                                       0xA0
#define MS5837_PROM_ADDRESS_1                                       0xA2
#define MS5837_PROM_ADDRESS_2                                       0xA4
#define MS5837_PROM_ADDRESS_3                                       0xA6
#define MS5837_PROM_ADDRESS_4                                       0xA8
#define MS5837_PROM_ADDRESS_5                                       0xAA
#define MS5837_PROM_ADDRESS_6                                       0xAC
#define MS5837_PROM_ADDRESS_7                                       0xAE

/* Coefficients indexes for temperature and pressure computation */

#define MS5837_CRC_INDEX                                            0
#define MS5837_PRESSURE_SENSITIVITY_INDEX                           1
#define MS5837_PRESSURE_OFFSET_INDEX                                2
#define MS5837_TEMP_COEFF_OF_PRESSURE_SENSITIVITY_INDEX             3
#define MS5837_TEMP_COEFF_OF_PRESSURE_OFFSET_INDEX                  4
#define MS5837_REFERENCE_TEMPERATURE_INDEX                          5
#define MS5837_TEMP_COEFF_OF_TEMPERATURE_INDEX                      6
#define MS5837_COEFFICIENT_NUMBERS                                  7


/* ------------------------------------------------------------------------------------------------
 *                                      Function Prototypes
 * ------------------------------------------------------------------------------------------------
 */
void MS5837_Init(void);
void MS5837_Convert_And_Read_ADC(void);
#endif /* DEVICES_MS58370_H_ */