Created
January 17, 2013 12:12
-
-
Save ossicode/4555531 to your computer and use it in GitHub Desktop.
MSP430F2132 Watchdog timer Testcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* clock.c | |
* | |
* Created on: 2013. 1. 16. | |
* Author: OSSI | |
*/ | |
#include "clock.h" | |
// main clock will be shared via system | |
static volatile uint8_t clockMode; | |
uint8_t clock_getMode(void) | |
{ | |
return clockMode; | |
} | |
uint8_t clock_setup(void) | |
{ | |
// after POR: MCLK = SMCLK = DCO ~ 1.2MHz | |
// set DCO speed to calibrated 8MHz | |
// NO VLOCLK use | |
BCSCTL1 = CALBC1_8MHZ; | |
DCOCTL = CALDCO_8MHZ; | |
// TODO: Capacitor setting for ACLK | |
return CLOCK_DCO_LFXT1; | |
} | |
void clock_dividerSetup(uint8_t MCLKDividerSelect, uint8_t SMCLKDividerSelect, uint8_t ACLKDividerSelect) | |
{ | |
// reset divider first | |
// reset ACLK divider | |
BCSCTL1 &= 0xCF; // Clear BIT5 and BIT4 | |
// select ACLK Divider | |
BCSCTL1 |= ACLKDividerSelect; | |
// reset MCLK, SMCLK divider | |
BCSCTL2 &= 0x00; // Clear BIT5,BIT4, BIT2, BIT1 and reset as 2132 only use DCO | |
// select MCLK Divider | |
BCSCTL2 |= MCLKDividerSelect; | |
// select SMCLK Divider | |
BCSCTL2 |= SMCLKDividerSelect; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* clock.h | |
* | |
* Created on: 2013. 1. 16. | |
* Author: OSSI | |
*/ | |
/* Clock setting for OSSI MSP430F2132 | |
* | |
* default: CLOCK_DCO_LFXT1 | |
* DCO Calibrated 8MHz | |
* LFXT1 Low Power Mode: 32.768 kHz | |
* MCLK = DCOCLK / SMCLK = DCOCLK / ACLK = LFXT1 | |
* | |
* TODO: LFXT1 to VLO switching? | |
* | |
*/ | |
#ifndef CLOCK_H_ | |
#define CLOCK_H_ | |
#include "msp430x21x2.h" | |
#include "ossitypes.h" | |
#include "debug.h" | |
#define CLOCK_DCO_LFXT1 (1) | |
#define MCLK_DIVIDED_BY_1 DIVM_0 /* MCLK Divider 0: /1 */ | |
#define MCLK_DIVIDED_BY_2 DIVM_1 /* MCLK Divider 1: /2 */ | |
#define MCLK_DIVIDED_BY_4 DIVM_2 /* MCLK Divider 2: /4 */ | |
#define MCLK_DIVIDED_BY_8 DIVM_3 /* MCLK Divider 3: /8 */ | |
#define SMCLK_DIVIDED_BY_1 DIVS_0 /* SMCLK Divider 0: /1 */ | |
#define SMCLK_DIVIDED_BY_2 DIVS_1 /* SMCLK Divider 1: /2 */ | |
#define SMCLK_DIVIDED_BY_4 DIVS_2 /* SMCLK Divider 2: /4 */ | |
#define SMCLK_DIVIDED_BY_8 DIVS_3 /* SMCLK Divider 3: /8 */ | |
#define ACLK_DIVIDED_BY_1 DIVA_0 /* ACLK Divider 0: /1 */ | |
#define ACLK_DIVIDED_BY_2 DIVA_1 /* ACLK Divider 1: /2 */ | |
#define ACLK_DIVIDED_BY_4 DIVA_2 /* ACLK Divider 2: /4 */ | |
#define ACLK_DIVIDED_BY_8 DIVA_3 /* ACLK Divider 3: /8 */ | |
uint8_t clock_getMode(void); | |
uint8_t clock_setup(void); | |
void clock_dividerSetup(uint8_t MCLKDividerSelect, uint8_t SMCLKDividerSelect, uint8_t ACLKDividerSelect); | |
#endif /* CLOCK_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* --COPYRIGHT--,BSD | |
* Copyright (c) 2012, Texas Instruments Incorporated | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
* * Neither the name of Texas Instruments Incorporated nor the names of | |
* its contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
* --/COPYRIGHT--*/ | |
//***************************************************************************** | |
// | |
//debug.h - Macros for assisting debug of the driver library. | |
// | |
// | |
//This is part of revision 0001 of the MSP430 Peripheral Driver Library. | |
// | |
//***************************************************************************** | |
#ifndef __DEBUG_H__ | |
#define __DEBUG_H__ | |
//***************************************************************************** | |
// | |
//Prototype for the function that is called when an invalid argument is passed | |
//to an API. This is only used when doing a DEBUG build. | |
// | |
//***************************************************************************** | |
extern void __error__ (char *pcFilename, unsigned long ulLine); | |
//***************************************************************************** | |
// | |
//The ASSERT macro, which does the actual assertion checking. Typically, this | |
//will be for procedure arguments. | |
// | |
//***************************************************************************** | |
#ifdef DEBUG | |
#define ASSERT(expr) { \ | |
if (!(expr)) \ | |
{ \ | |
__error__(__FILE__, __LINE__); \ | |
} \ | |
} | |
#else | |
#define ASSERT(expr) | |
#endif | |
#endif //__DEBUG_H__ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ossibeacon.h" | |
#include "wdt.h" | |
/* | |
* main.c | |
*/ | |
int main(void) { | |
// stop wdt in the beginning | |
wdt_hold(); | |
// ACLK = 32.768kHz / 8 = 4096Hz | |
clock_setup(); | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_8); | |
// after system clock set, do wdt clock setup | |
// wdt clock = ACLK = 4096Hz, wdt clock divider = 8192 | |
// PUC happens after wdt start 8192 / 4096 = 2 sec | |
wdt_init(WDT_CLOCKSOURCE_ACLK, WDT_CLOCKDIVIDER_8192); | |
wdt_start(); | |
volatile uint16_t i; | |
IO_DIRECTION(LED,OUTPUT); | |
IO_SET(LED,LOW); | |
// 1sec delay | |
for (i = 0; i < 1000 ; i++) | |
{ | |
__delay_cycles(7372); | |
} | |
IO_SET(LED,HIGH); | |
while(1) | |
{ | |
// keep resetting the wdt in the code | |
// to prevent the system from PUC reset | |
wdt_reset(); | |
// 1sec delay | |
for (i = 0; i < 1000 ; i++) | |
{ | |
__delay_cycles(7372); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ossibeacon.h | |
* | |
* Created on: 2012. 12. 22. | |
* Author: OSSI | |
*/ | |
#ifndef OSSIBEACON_H_ | |
#define OSSIBEACON_H_ | |
#include "msp430x21x2.h" | |
#include "debug.h" | |
#include "system.h" | |
#include "clock.h" | |
#include "ossitypes.h" | |
#include "util.h" | |
#endif /* OSSIBEACON_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ossitypes.h | |
* | |
* Created on: 2012. 12. 22. | |
* Author: OSSI | |
*/ | |
#ifndef OSSITYPES_H_ | |
#define OSSITYPES_H_ | |
typedef unsigned char uint8_t; | |
typedef signed char int8_t; | |
typedef unsigned short int uint16_t; | |
typedef signed short int int16_t; | |
typedef unsigned long int uint32_t; | |
typedef signed long int int32_t; | |
#endif /* OSSITYPES_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* setup2132.c | |
* | |
* Created on: 2012. 12. 19. | |
* Author: OSSI | |
*/ | |
#include "system.h" | |
void int_wdt_disable(void) | |
{ | |
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer | |
} | |
void IO_setup(void) | |
{ | |
// general IO init | |
// TODO: set unused pins!!! | |
IO_DIRECTION(LED,OUTPUT); | |
IO_SET(LED,LOW); | |
} | |
void ext_wdt_setup(void) | |
{ | |
IO_DIRECTION(EXTWDT,OUTPUT); | |
} | |
void ext_wdt_rst(void) | |
{ | |
//LOW-HIGH-LOW | |
IO_SET(EXTWDT,LOW); | |
delay_ms(1); | |
IO_SET(EXTWDT,HIGH); | |
delay_ms(1); | |
IO_SET(EXTWDT,LOW); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* setup2132.h | |
* | |
* Created on: 2012. 12. 19. | |
* Author: OSSI | |
*/ | |
#ifndef SETUP2132_H_ | |
#define SETUP2132_H_ | |
#include "msp430x21x2.h" | |
#include "ossitypes.h" | |
#include "util.h" | |
// System IO Define | |
#define LED_PORT 3 | |
#define LED_PIN 0 | |
#define EXTWDT_PORT 2 | |
#define EXTWDT_PIN 2 | |
void int_wdt_disable(void); | |
void IO_setup(void); | |
void ext_wdt_setup(void); | |
void ext_wdt_rst(void); | |
#endif /* SETUP2132_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* util.c | |
* | |
* Created on: 2012. 12. 19. | |
* Author: OSSI | |
*/ | |
#include "util.h" | |
void delay_ms(uint16_t delay) | |
{ | |
volatile uint16_t i; | |
for (i=0; i<delay;i++) | |
{ | |
__delay_cycles(8000); // @ DCO 8MHz | |
} | |
} | |
void min_max(uint8_t min, uint8_t max, uint8_t value) | |
{ | |
if (value <= min) | |
{ | |
value = min; | |
} | |
if (value > max) | |
{ | |
value = max; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* global.h | |
* | |
* Created on: 2012. 6. 3. | |
* Author: donghee | |
*/ | |
#ifndef GLOBAL_H_ | |
#define GLOBAL_H_ | |
#include "ossitypes.h" | |
// Macros for turning on and off the LEDs (for testing only) | |
enum {FALSE = 0, TRUE = 1}; | |
#define _BV(bit) (1<<(bit)) | |
//macros for IO config (with preprocessor abuse) | |
#define st(x) do{x} while(__LINE__ == -1) | |
enum IoState {LOW = 0, HIGH = 1, TOGGLE = 2}; | |
#define IO_SET(name,val) _IO_SET(name##_PORT,name##_PIN,val) | |
#define _IO_SET(port,pin,val) st(__IO_SET(port,pin,val);) | |
#define __IO_SET(port,pin,val) if (val == TOGGLE)\ | |
{\ | |
st((P##port##OUT ^= _BV(pin)););\ | |
}\ | |
else if (val == HIGH)\ | |
{\ | |
st((P##port##OUT |= _BV(pin)););\ | |
}\ | |
else\ | |
{\ | |
st((P##port##OUT &= ~_BV(pin)););\ | |
} | |
enum IoDirection {INPUT = 0, OUTPUT = 1}; | |
#define IO_DIRECTION(name,dir) _IO_DIRECTION(name##_PORT,name##_PIN,dir) | |
#define _IO_DIRECTION(port,pin,dir) st(__IO_DIRECTION(port,pin,dir);) | |
#define __IO_DIRECTION(port,pin,dir) if (dir)\ | |
{\ | |
st((P##port##DIR |= _BV(pin)););\ | |
}\ | |
else\ | |
{\ | |
st((P##port##DIR &= ~_BV(pin)););\ | |
} | |
void delay_ms(uint16_t delay); | |
void min_max(uint8_t min, uint8_t max, uint8_t value); | |
#endif /* GLOBAL_H_ */ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* wdt.c | |
* | |
* Created on: 2013. 1. 17. | |
* Author: OSSI | |
*/ | |
#include "wdt.h" | |
void wdt_hold(void) | |
{ | |
volatile uint8_t wdtStatus = (WDTCTL & 0x0F) | WDTHOLD; | |
// Stop watchdog timer | |
WDTCTL = WDTPW + wdtStatus ; | |
} | |
void wdt_init(uint8_t clockSelect, uint8_t clockDividerSelect) | |
{ | |
// reset wdt counter, wdt stop | |
// reset wdt clock, wdt clock dividier | |
WDTCTL = WDTPW + WDTCNTCL + WDTHOLD+ clockSelect + clockDividerSelect; | |
} | |
void wdt_start(void) | |
{ | |
volatile uint8_t wdtStatus = (WDTCTL & 0x0F) & ~(WDTHOLD); | |
// Start watchdog timer | |
WDTCTL = WDTPW + wdtStatus ; | |
} | |
void wdt_reset(void) | |
{ | |
// reset wdt counter | |
volatile uint8_t wdtStatus = (WDTCTL & 0x0F) | WDTCNTCL; | |
WDTCTL = WDTPW + wdtStatus ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* wdt.h | |
* | |
* Created on: 2013. 1. 17. | |
* Author: OSSI | |
*/ | |
#ifndef WDT_H_ | |
#define WDT_H_ | |
#include "msp430f2132.h" | |
#include "ossitypes.h" | |
#include "debug.h" | |
#define WDT_CLOCKSOURCE_SMCLK (0) | |
#define WDT_CLOCKSOURCE_ACLK (WDTSSEL) | |
#define WDT_CLOCKDIVIDER_32768 (0) | |
#define WDT_CLOCKDIVIDER_8192 (WDTIS0) | |
#define WDT_CLOCKDIVIDER_512 (WDTIS1) | |
#define WDT_CLOCKDIVIDER_64 (WDTIS0 + WDTIS1) | |
void wdt_hold(void); | |
void wdt_init(uint8_t clockSelect, uint8_t clockDividerSelect); | |
void wdt_start(void); | |
void wdt_reset(void); | |
#endif /* WDT_H_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment