Jump to content

Recommended Posts

I've got an F5229LP app that sets SMCLK to XT2 with a divider of 4. (XT2 = 25Mhz crystal, which is also the source for MCLK)

When I initialize the clocks, HardwareSerial no longer produces an accurate Baud Rate, and it breaks terminal communication.

I see that HardwareSerial.cpp contains:

#define SMCLK F_CPU //SMCLK = F_CPU for now

I've tried changing it to F_CPU / 4 but the situation did not improve.


Here is my recreation code:  (Uncomment initClocks(25000000l); and change HardwareSerial.cpp to see it fail)

#include <WString.h>
void initClocks(uint32_t mclkFreq);
void SerialTestText() {
	Serial.println("Back channel active.");
	char* selfTest = (char*) ("Performing Self Test.");
	while (*selfTest)

void setup() {
  // put your setup code here, to run once:
//	initClocks(25000000l);


	//Turn on LED
	pinMode(RED_LED, OUTPUT);
	digitalWrite(RED_LED, true);
	Serial.println(' ');//Sacrificial character. (First character is often garbled.)
int _previous = 0;
void loop() {
  // put your main code here, to run repeatedly: 
  int _now = millis()/2000;
  digitalWrite(RED_LED, (int)(millis()/500) & 0x1);
  if (_previous != _now)
  _previous = _now;

void initClocks(uint32_t mclkFreq) {
#ifdef __MSP430F5529__
	//Enable XT2
	P5SEL |= BIT3 + BIT2;
	delay(100);  //Give time for XT2 to settle

	// Assign the XT1 as the FLL reference clock
	UCSCTL3 &= ~0x7;
	UCSCTL3 |= 0x2;//FLL reference clock divider of 1
	// Assign the XT2 as the MCLK reference clock
	UCSCTL4 &= ~0x7;//MCLK source bits
	UCSCTL4 |= 0x5;//Source = XT2 when available otherwise DCOCLKDIV
	UCSCTL5 &= ~0x7;//Clock divider of 1 (25Mhz)
	// Assign the XT2 as the SMCLK reference clock
	UCSCTL4 &= ~0x70;
	UCSCTL4 |= 0x50;//SMCLK
	UCSCTL5 &= ~0x70;
	UCSCTL5 |= 0x10; //Clock divider of 4 (6.25Mhz)
	// Assign the XT1 as the source for ACLK
	UCSCTL4 &= ~0x700;
	UCSCTL4 |= 0x100;//ACLK
	UCSCTL5 &= ~0x700;
	UCSCTL5 |= 0x100; //Clock divider of 16 (~4khz)

How do I use HardwareSerial with a custom value of SMCLK?

Link to post
Share on other sites

Short answer: you can't.


Longer answer: The MSP430 has a few clocks running, most notable are ACLK and SMCLK, a third is MCLK, which is used to clock the CPU itself.

You cannot change the SMCLK value for the Hardware Serial, you can however set SMCLK to a different value. This is not related to HardwareSerial, it is related to the MSP430 clock system. In the family guide there is a chapter describing the clock system.

Note that things change when you alter SMCLK to run at a different divider or source, for example, delay functions may depend on ACLK or SMCLK to be a certain value. As a result, you may observe things like delay(100) waiting for 200ms.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...