Last active
December 11, 2015 03:48
-
-
Save ossicode/4539945 to your computer and use it in GitHub Desktop.
snippet of how to change clock when XT2 fails and how to use resulted clock on flash write
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 "ossiobc.h" | |
#include "flash.h" | |
uint8_t data[128] ={0}; | |
uint8_t flashData[128] ={0}; | |
int main(void) | |
{ | |
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer | |
// 5.5 SMCLK 5.6 ACLK | |
// To temporary check for clock out for speed check | |
P5DIR |= 0x60; // P5.5,6 output | |
P5SEL |= 0x60; | |
// to check timing | |
P6DIR |= BIT0; // P6.0 output | |
P6OUT |= BIT0; | |
// TODO: check timeout time when XT2 fails by toggle gpio | |
clock_setup(); | |
uint8_t systemClock; | |
systemClock = clock_getMode(); | |
if(systemClock == CLOCK_XT2_LFXT1) | |
{ | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_1); | |
} | |
else if(systemClock == CLOCK_DCO_LFXT1) | |
{ | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_1); | |
} | |
while(1) | |
{ | |
// TODO: check how long does it take to write 128 bytes to flash by toggling gpio | |
volatile uint8_t i; | |
for(i =0;i < 128;i++) | |
{ | |
data[i]=22; | |
} | |
systemClock = clock_getMode(); | |
if(systemClock == CLOCK_XT2_LFXT1) | |
{ | |
// set divider for flash write | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_8, ACLK_DIVIDED_BY_1); | |
// 7.3728MHz / 8 / (2+1) = 307 kHz | |
flash_writeBegin(FLASH_SMCLK,2); | |
flash_writeData(0,128,data); | |
flash_writeEnd(); | |
flash_readData(0,128,flashData); | |
// restore divider for the system | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_1); | |
} | |
else if(systemClock == CLOCK_DCO_LFXT1) | |
{ | |
// set divider for flash write | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_1); | |
// 1MHz / 1 / (2+1) = 333 kHz | |
P6OUT ^= BIT0; | |
flash_writeBegin(FLASH_SMCLK,2); | |
flash_writeData(0,128,data); | |
flash_writeEnd(); | |
P6OUT ^= BIT0; | |
flash_readData(0,128,flashData); | |
// restore divider for the system | |
clock_dividerSetup(MCLK_DIVIDED_BY_1, SMCLK_DIVIDED_BY_1, ACLK_DIVIDED_BY_1); | |
} | |
// _NOP() for break point | |
_NOP(); | |
// when debugging flash module, make sure you go to LPM | |
// other wise flash might be hard.. | |
LPM3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment