详见msp432p401r第6页
开发板手册(slau597f)第38页
数据手册(slau356)第904页
1.uart.h
初始化串口函数
UART_initModule(EUSCI_Ax_BASE, &uartConfig);
使能串口模块
UART_enableModule(EUSCI_Ax_BASE);
开启串口相关中断
UART_enableInterrupt(EUSCI_Ax_BASE, EUSCI_x_INTERRUPT);
获取数据
UART_receiveData(EUSCI_Ax_BASE);
发送数据
UART_transmitData(EUSCI_Ax_BASE,Data_8bit);
开启串口端口中断
Interrupt_enableInterrupt(INT_EUSCIAx);
开启总中断
Interrupt_enableMaster(void);
usart.h
/****************************************************/
// MSP432P401R
// 串口配置
// Bilibili:m-RNA
// E-mail:[email protected]
/****************************************************/
/****************** 版本更新说明 *****************
*
* CCS支持printf
* Keil支持标准C库跟微库
* 用Keil开发终于可以不开微库啦
*
* ? 需要注意:
* ①使用标准C库时,将无法使用scanf。
* 如果需要使用scanf时,请使用微库 MicroLIB
* ①低频时钟频率下,高波特率使得传输时误差过大,
* 比如35768Hz下19200波特率,
* 会使得传输出错,这时可以尝试降低波特率。
* ②baudrate_calculate的问题请去文件内查看。
*
* **************************************************
*
* ? v3.2 2021/10/28
* 简化对CCS支持的printf代码
*
* ? v3.1 2021/10/18
* 添加对CCS的printf支持
*
* ? v3.0 2021/10/15
* 此版本支持使用 标准C库
* 文件正式改名为与正点原子同名的
* usart.c 和 usart.h,方便移植
* 仅支持Keil平台开发
*
* ? v2.1 2021/8/27
* 添加支持固件库v3_21_00_05
* 仅支持 MicroLIB 微库、Keil平台开发
*
* ? v2.0 2021/8/25
* uart_init增添了波特率传入参数,可直接配置波特率。
* 计算UART的代码单独打包为名为
* baudrate_calculate的c文件和h文件
* 仅支持 MicroLIB 微库、Keil平台开发
*
* ? v1.0 2021/7/17
* 仅支持固件库v3_40_01_02
* 配置了SMCLK 48MHz 波特率 115200的初始化代码,
* 对接标准输入输出库,使其能使用printf、scanf函数
* 仅支持 MicroLIB 微库、Keil平台开发
*
****************************************************/
#ifndef __USART_H
#define __USART_H
#include "driverlib.h"
#include "stdio.h" //1.61328125kb
#ifdef __TI_COMPILER_VERSION__
//CCS平台
#include "stdarg.h"
#include "string.h"
#define USART0_MAX_SEND_LEN 600 //最大发送缓存字节数
int printf(const char *str, ...);
#endif
void uart_init(uint32_t baudRate);
#endif
usart.c
/****************************************************/
// MSP432P401R
// 串口配置
// Bilibili:m-RNA
// E-mail:[email protected]
/****************************************************/
/****************** 版本更新说明 *****************
*
* CCS支持printf
* Keil支持标准C库跟微库
* 用Keil开发终于可以不开微库啦
*
* ? 需要注意:
* ①使用标准C库时,将无法使用scanf。
* 如果需要使用scanf时,请使用微库 MicroLIB
* ①低频时钟频率下,高波特率使得传输时误差过大,
* 比如35768Hz下19200波特率,
* 会使得传输出错,这时可以尝试降低波特率。
* ②baudrate_calculate的问题请去文件内查看。
*
* **************************************************
*
* ? v3.2 2021/10/28
* 简化对CCS支持的printf代码
*
* ? v3.1 2021/10/18
* 添加对CCS的printf支持
*
* ? v3.0 2021/10/15
* 此版本支持使用 标准C库
* 文件正式改名为与正点原子同名的
* usart.c 和 usart.h,方便移植
* 仅支持Keil平台开发
*
* ? v2.1 2021/8/27
* 添加支持固件库v3_21_00_05
* 仅支持 MicroLIB 微库、Keil平台开发
*
* ? v2.0 2021/8/25
* uart_init增添了波特率传入参数,可直接配置波特率。
* 计算UART的代码单独打包为名为
* baudrate_calculate的c文件和h文件
* 仅支持 MicroLIB 微库、Keil平台开发
*
* ? v1.0 2021/7/17
* 仅支持固件库v3_40_01_02
* 配置了SMCLK 48MHz 波特率 115200的初始化代码,
* 对接标准输入输出库,使其能使用printf、scanf函数
* 仅支持 MicroLIB 微库、Keil平台开发
*
****************************************************/
#include "usart.h"
#include "baudrate_calculate.h"
#ifdef __TI_COMPILER_VERSION__
//CCS平台
uint8_t USART0_TX_BUF[USART0_MAX_SEND_LEN]; //发送缓冲,最大USART3_MAX_SEND_LEN字节
int printf(const char *str, ...)
{
uint16_t i,j;
va_list ap;
va_start(ap,str);
vsprintf((char*)USART0_TX_BUF,str,ap);
va_end(ap);
i=strlen((const char*)USART0_TX_BUF); //此次发送数据的长度
for(j=0;j<i;j++) //循环发送数据
{
//while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //循环发送,直到发送完毕
UART_transmitData(EUSCI_A0_BASE, USART0_TX_BUF[j]);
}
return 0;
}
/***************** 函数说明 *****************
*
* 函数:int printf(const char *str, ...);
* 源码来自@正点原子
* 稍作改动适配CCS工程,在此也表感谢正点原子。
*
***************** 说明结束 *****************/
#else
//Keil支持标准C库跟微库
//预编译
//if 1 使用标准C库 如果报错就使用微库
//if 0 使用微库 得去勾选魔术棒里的 Use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
#else
int fgetc(FILE *f)
{
while (EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG !=
UART_getInterruptStatus(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG))
;
return UART_receiveData(EUSCI_A0_BASE);
}
#endif
int fputc(int ch, FILE *f)
{
UART_transmitData(EUSCI_A0_BASE, ch & 0xFF);
return ch;
}
/***************** 函数说明 *****************
*
* 以上两条对接标准输入输出库的函数:
* int fputc(int ch, FILE *f);
* int fgetc(FILE *f);
* 源码为BiliBili平台UP主 “CloudBoyStudio” 编写
* 本人RNA,不是作者
* 在此也表感谢
*
***************** 说明结束 *****************/
#endif
void uart_init(uint32_t baudRate)
{
#ifdef EUSCI_A_UART_7_BIT_LEN
//固件库v3_40_01_02
//默认SMCLK 48MHz 比特率 115200
const eUSCI_UART_ConfigV1 uartConfig =
{
EUSCI_A_UART_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
26, // BRDIV = 26
0, // UCxBRF = 0
111, // UCxBRS = 111
EUSCI_A_UART_NO_PARITY, // No Parity
EUSCI_A_UART_LSB_FIRST, // MSB First
EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
EUSCI_A_UART_MODE, // UART mode
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION, // Oversampling
EUSCI_A_UART_8_BIT_LEN // 8 bit data length
};
eusci_calcBaudDividers((eUSCI_UART_ConfigV1 *)&uartConfig, baudRate); //配置波特率
#else
//固件库v3_21_00_05
//默认SMCLK 48MHz 比特率 115200
const eUSCI_UART_Config uartConfig =
{
EUSCI_A_UART_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
26, // BRDIV = 26
0, // UCxBRF = 0
111, // UCxBRS = 111
EUSCI_A_UART_NO_PARITY, // No Parity
EUSCI_A_UART_LSB_FIRST, // MSB First
EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
EUSCI_A_UART_MODE, // UART mode
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION, // Oversampling
};
eusci_calcBaudDividers((eUSCI_UART_Config *)&uartConfig, baudRate); //配置波特率
#endif
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P1, GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);//2.配置GPIO复用
MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);//3.初始化串口
MAP_UART_enableModule(EUSCI_A0_BASE);//4.开启串口模块
UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);//5.开启串口相关中断
Interrupt_enableInterrupt(INT_EUSCIA0);//6.开启串口端口中断
Interrupt_enableMaster();//7.开启总中断
}
//8.编写UART ISR
void EUSCIA0_IRQHandler(void)
{
uint32_t status = UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG) //接收中断
{
UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE)); //发送数据
}
}
sysinit.h
/* --COPYRIGHT--,BSD
* Copyright (c) 2017, 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--*/
#ifndef __SYSCTL_H__
#define __SYSCTL_H__
#include
#include "driverlib.h"
/* Define to ensure that our current MSP432 has the SYSCTL module. This
definition is included in the device specific header file */
#ifdef __MCU_HAS_SYSCTL__
//*****************************************************************************
//
//! \addtogroup sysctl_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif
//*****************************************************************************
//
// Control specific variables
//
//*****************************************************************************
#define SYSCTL_SRAM_BANK7 SYSCTL_SRAM_BANKEN_BNK7_EN
#define SYSCTL_SRAM_BANK6 SYSCTL_SRAM_BANKEN_BNK6_EN
#define SYSCTL_SRAM_BANK5 SYSCTL_SRAM_BANKEN_BNK5_EN
#define SYSCTL_SRAM_BANK4 SYSCTL_SRAM_BANKEN_BNK4_EN
#define SYSCTL_SRAM_BANK3 SYSCTL_SRAM_BANKEN_BNK3_EN
#define SYSCTL_SRAM_BANK2 SYSCTL_SRAM_BANKEN_BNK2_EN
#define SYSCTL_SRAM_BANK1 SYSCTL_SRAM_BANKEN_BNK1_EN
#define SYSCTL_HARD_RESET 1
#define SYSCTL_SOFT_RESET 0
#define SYSCTL_PERIPH_DMA SYSCTL_PERIHALT_CTL_HALT_DMA
#define SYSCTL_PERIPH_WDT SYSCTL_PERIHALT_CTL_HALT_WDT
#define SYSCTL_PERIPH_ADC SYSCTL_PERIHALT_CTL_HALT_ADC
#define SYSCTL_PERIPH_EUSCIB3 SYSCTL_PERIHALT_CTL_HALT_EUB3
#define SYSCTL_PERIPH_EUSCIB2 SYSCTL_PERIHALT_CTL_HALT_EUB2
#define SYSCTL_PERIPH_EUSCIB1 SYSCTL_PERIHALT_CTL_HALT_EUB1
#define SYSCTL_PERIPH_EUSCIB0 SYSCTL_PERIHALT_CTL_HALT_EUB0
#define SYSCTL_PERIPH_EUSCIA3 SYSCTL_PERIHALT_CTL_HALT_EUA3
#define SYSCTL_PERIPH_EUSCIA2 SYSCTL_PERIHALT_CTL_HALT_EUA2
#define SYSCTL_PERIPH_EUSCIA1 SYSCTL_PERIHALT_CTL_HALT_EUA1
#define SYSCTL_PERIPH_EUSCIA0 SYSCTL_PERIHALT_CTL_HALT_EUA0
#define SYSCTL_PERIPH_TIMER32_0_MODULE SYSCTL_PERIHALT_CTL_HALT_T32_0
#define SYSCTL_PERIPH_TIMER16_3 SYSCTL_PERIHALT_CTL_HALT_T16_3
#define SYSCTL_PERIPH_TIMER16_2 SYSCTL_PERIHALT_CTL_HALT_T16_2
#define SYSCTL_PERIPH_TIMER16_1 SYSCTL_PERIHALT_CTL_HALT_T16_1
#define SYSCTL_PERIPH_TIMER16_0 SYSCTL_PERIHALT_CTL_HALT_T16_0
#define SYSCTL_NMIPIN_SRC SYSCTL_NMI_CTLSTAT_PIN_SRC
#define SYSCTL_PCM_SRC SYSCTL_NMI_CTLSTAT_PCM_SRC
#define SYSCTL_PSS_SRC SYSCTL_NMI_CTLSTAT_PSS_SRC
#define SYSCTL_CS_SRC SYSCTL_NMI_CTLSTAT_CS_SRC
#define SYSCTL_REBOOT_KEY 0x6900
#define SYSCTL_1_2V_REF (uint32_t)&TLV->ADC14_REF1P2V_TS30C - (uint32_t)TLV_BASE
#define SYSCTL_1_45V_REF (uint32_t)&TLV->ADC14_REF1P45V_TS30C - (uint32_t)TLV_BASE
#define SYSCTL_2_5V_REF (uint32_t)&TLV->ADC14_REF2P5V_TS30C - (uint32_t)TLV_BASE
#define SYSCTL_85_DEGREES_C 4
#define SYSCTL_30_DEGREES_C 0
#define TLV_START 0x00201004
#define TLV_TAG_RESERVED1 1
#define TLV_TAG_RESERVED2 2
#define TLV_TAG_CS 3
#define TLV_TAG_FLASHCTL 4
#define TLV_TAG_ADC14 5
#define TLV_TAG_RESERVED6 6
#define TLV_TAG_RESERVED7 7
#define TLV_TAG_REF 8
#define TLV_TAG_RESERVED9 9
#define TLV_TAG_RESERVED10 10
#define TLV_TAG_DEVINFO 11
#define TLV_TAG_DIEREC 12
#define TLV_TAG_RANDNUM 13
#define TLV_TAG_RESERVED14 14
#define TLV_TAG_BSL 15
#define TLV_TAGEND 0x0BD0E11D
//*****************************************************************************
//
// Structures for TLV definitions
//
//*****************************************************************************
typedef struct
{
uint32_t maxProgramPulses;
uint32_t maxErasePulses;
} SysCtl_FlashTLV_Info;
typedef struct
{
uint32_t rDCOIR_FCAL_RSEL04;
uint32_t rDCOIR_FCAL_RSEL5;
uint32_t rDCOIR_MAXPOSTUNE_RSEL04;
uint32_t rDCOIR_MAXNEGTUNE_RSEL04;
uint32_t rDCOIR_MAXPOSTUNE_RSEL5;
uint32_t rDCOIR_MAXNEGTUNE_RSEL5;
uint32_t rDCOIR_CONSTK_RSEL04;
uint32_t rDCOIR_CONSTK_RSEL5;
uint32_t rDCOER_FCAL_RSEL04;
uint32_t rDCOER_FCAL_RSEL5;
uint32_t rDCOER_MAXPOSTUNE_RSEL04;
uint32_t rDCOER_MAXNEGTUNE_RSEL04;
uint32_t rDCOER_MAXPOSTUNE_RSEL5;
uint32_t rDCOER_MAXNEGTUNE_RSEL5;
uint32_t rDCOER_CONSTK_RSEL04;
uint32_t rDCOER_CONSTK_RSEL5;
} SysCtl_CSCalTLV_Info;
//*****************************************************************************
//
// Prototypes for the APIs.
//
//*****************************************************************************
//*****************************************************************************
//
//! Gets the size of the SRAM.
//!
//! \return The total number of bytes of SRAM.
//
//*****************************************************************************
extern uint_least32_t SysCtl_getSRAMSize(void);
//*****************************************************************************
//
//! Gets the size of the flash.
//!
//! \return The total number of bytes of flash.
//
//*****************************************************************************
extern uint_least32_t SysCtl_getFlashSize(void);
//*****************************************************************************
//
//! Reboots the device and causes the device to re-initialize itself.
//!
//! \return This function does not return.
//
//*****************************************************************************
extern void SysCtl_rebootDevice(void);
//*****************************************************************************
//
//! The TLV structure uses a tag or base address to identify segments of the
//! table where information is stored. Some examples of TLV tags are Peripheral
//! Descriptor, Interrupts, Info Block and Die Record. This function retrieves
//! the value of a tag and the length of the tag.
//!
//! \param tag represents the tag for which the information needs to be
//! retrieved.
//! Valid values are:
//! - \b TLV_TAG_RESERVED1
//! - \b TLV_TAG_RESERVED2
//! - \b TLV_TAG_CS
//! - \b TLV_TAG_FLASHCTL
//! - \b TLV_TAG_ADC14
//! - \b TLV_TAG_RESERVED6
//! - \b TLV_TAG_RESERVED7
//! - \b TLV_TAG_REF
//! - \b TLV_TAG_RESERVED9
//! - \b TLV_TAG_RESERVED10
//! - \b TLV_TAG_DEVINFO
//! - \b TLV_TAG_DIEREC
//! - \b TLV_TAG_RANDNUM
//! - \b TLV_TAG_RESERVED14
//! \param instance In some cases a specific tag may have more than one
//! instance. For example there may be multiple instances of timer
//! calibration data present under a single Timer Cal tag. This variable
//! specifies the instance for which information is to be retrieved (0,
//! 1, etc.). When only one instance exists; 0 is passed.
//! \param length Acts as a return through indirect reference. The function
//! retrieves the value of the TLV tag length. This value is pointed to
//! by *length and can be used by the application level once the
//! function is called. If the specified tag is not found then the
//! pointer is null 0.
//! \param data_address acts as a return through indirect reference. Once the
//! function is called data_address points to the pointer that holds the
//! value retrieved from the specified TLV tag. If the specified tag is
//! not found then the pointer is null 0.
//!
//! \return None
//
//*****************************************************************************
extern void SysCtl_getTLVInfo(uint_fast8_t tag, uint_fast8_t instance,
uint_fast8_t *length, uint32_t **data_address);
//*****************************************************************************
//
//! Enables a set of banks in the SRAM. This can be used to optimize power
//! consumption when every SRAM bank isn't needed. It is important to note
//! that when a higher bank is enabled, all of the SRAM banks below that bank
//! are also enabled. For example, if the user enables SYSCTL_SRAM_BANK7,
//! the banks SYSCTL_SRAM_BANK1 through SYSCTL_SRAM_BANK7 will be enabled
//! (SRAM_BANK0 is reserved and always enabled).
//!
//! \param sramBank The SRAM bank tier to enable.
//! Must be only one of the following values:
//! - \b SYSCTL_SRAM_BANK1,
//! - \b SYSCTL_SRAM_BANK2,
//! - \b SYSCTL_SRAM_BANK3,
//! - \b SYSCTL_SRAM_BANK4,
//! - \b SYSCTL_SRAM_BANK5,
//! - \b SYSCTL_SRAM_BANK6,
//! - \b SYSCTL_SRAM_BANK7
//!
//! \note \b SYSCTL_SRAM_BANK0 is reserved and always enabled.
//!
//! \return None.
//
//*****************************************************************************
extern void SysCtl_enableSRAMBank(uint_fast8_t sramBank);
//*****************************************************************************
//
//! Disables a set of banks in the SRAM. This can be used to optimize power
//! consumption when every SRAM bank isn't needed. It is important to note
//! that when a higher bank is disabled, all of the SRAM banks above that bank
//! are also disabled. For example, if the user disables SYSCTL_SRAM_BANK5,
//! the banks SYSCTL_SRAM_BANK6 through SYSCTL_SRAM_BANK7 will be disabled.
//!
//! \param sramBank The SRAM bank tier to disable.
//! Must be only one of the following values:
//! - \b SYSCTL_SRAM_BANK1,
//! - \b SYSCTL_SRAM_BANK2,
//! - \b SYSCTL_SRAM_BANK3,
//! - \b SYSCTL_SRAM_BANK4,
//! - \b SYSCTL_SRAM_BANK5,
//! - \b SYSCTL_SRAM_BANK6,
//! - \b SYSCTL_SRAM_BANK7
//!
//! \note \b SYSCTL_SRAM_BANK0 is reserved and always enabled.
//!
//! \return None.
//
//*****************************************************************************
extern void SysCtl_disableSRAMBank(uint_fast8_t sramBank);
//*****************************************************************************
//
//! Enables retention of the specified SRAM bank register when the device goes
//! into LPM3 mode. When the system is placed in LPM3 mode, the SRAM
//! banks specified with this function will be placed into retention mode. By
//! default, retention of every SRAM bank except SYSCTL_SRAM_BANK0 (reserved) is
//! disabled. Retention of individual banks can be set without the restrictions
//! of the enable/disable functions.
//!
//! \param sramBank The SRAM banks to enable retention
//! Can be a bitwise OR of the following values:
//! - \b SYSCTL_SRAM_BANK1,
//! - \b SYSCTL_SRAM_BANK2,
//! - \b SYSCTL_SRAM_BANK3,
//! - \b SYSCTL_SRAM_BANK4,
//! - \b SYSCTL_SRAM_BANK5,
//! - \b SYSCTL_SRAM_BANK6,
//! - \b SYSCTL_SRAM_BANK7
//! \note \b SYSCTL_SRAM_BANK0 is reserved and retention is always enabled.
//!
//!
//! \return None.
//
//*****************************************************************************
extern void SysCtl_enableSRAMBankRetention(uint_fast8_t sramBank);
//*****************************************************************************
//
//! Disables retention of the specified SRAM bank register when the device goes
//! into LPM3 mode. When the system is placed in LPM3 mode, the SRAM
//! banks specified with this function will not be placed into retention mode.
//! By default, retention of every SRAM bank except SYSCTL_SRAM_BANK0 (reserved)
//! is disabled. Retention of individual banks can be set without the
//! restrictions of the enable/disable SRAM bank functions.
//!
//! \param sramBank The SRAM banks to disable retention
//! Can be a bitwise OR of the following values:
//! - \b SYSCTL_SRAM_BANK1,
//! - \b SYSCTL_SRAM_BANK2,
//! - \b SYSCTL_SRAM_BANK3,
//! - \b SYSCTL_SRAM_BANK4,
//! - \b SYSCTL_SRAM_BANK5,
//! - \b SYSCTL_SRAM_BANK6,
//! - \b SYSCTL_SRAM_BANK7
//! \note \b SYSCTL_SRAM_BANK0 is reserved and retention is always enabled.
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_disableSRAMBankRetention(uint_fast8_t sramBank);
//*****************************************************************************
//
//! Makes it so that the provided peripherals will either halt execution after
//! a CPU HALT. Parameters in this function can be combined to account for
//! multiple peripherals. By default, all peripherals keep running after a
//! CPU HALT.
//!
//! \param devices The peripherals to continue running after a CPU HALT
//! This can be a bitwise OR of the following values:
//! - \b SYSCTL_PERIPH_DMA,
//! - \b SYSCTL_PERIPH_WDT,
//! - \b SYSCTL_PERIPH_ADC,
//! - \b SYSCTL_PERIPH_EUSCIB3,
//! - \b SYSCTL_PERIPH_EUSCIB2,
//! - \b SYSCTL_PERIPH_EUSCIB1
//! - \b SYSCTL_PERIPH_EUSCIB0,
//! - \b SYSCTL_PERIPH_EUSCIA3,
//! - \b SYSCTL_PERIPH_EUSCIA2
//! - \b SYSCTL_PERIPH_EUSCIA1,
//! - \b SYSCTL_PERIPH_EUSCIA0,
//! - \b SYSCTL_PERIPH_TIMER32_0_MODULE,
//! - \b SYSCTL_PERIPH_TIMER16_3,
//! - \b SYSCTL_PERIPH_TIMER16_2,
//! - \b SYSCTL_PERIPH_TIMER16_1,
//! - \b SYSCTL_PERIPH_TIMER16_0
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_enablePeripheralAtCPUHalt(uint_fast16_t devices);
//*****************************************************************************
//
//! Makes it so that the provided peripherals will either halt execution after
//! a CPU HALT. Parameters in this function can be combined to account for
//! multiple peripherals. By default, all peripherals keep running after a
//! CPU HALT.
//!
//! \param devices The peripherals to disable after a CPU HALT
//!
//! The \e devices parameter can be a bitwise OR of the following values:
//! This can be a bitwise OR of the following values:
//! - \b SYSCTL_PERIPH_DMA,
//! - \b SYSCTL_PERIPH_WDT,
//! - \b SYSCTL_PERIPH_ADC,
//! - \b SYSCTL_PERIPH_EUSCIB3,
//! - \b SYSCTL_PERIPH_EUSCIB2,
//! - \b SYSCTL_PERIPH_EUSCIB1
//! - \b SYSCTL_PERIPH_EUSCIB0,
//! - \b SYSCTL_PERIPH_EUSCIA3,
//! - \b SYSCTL_PERIPH_EUSCIA2
//! - \b SYSCTL_PERIPH_EUSCIA1,
//! - \b SYSCTL_PERIPH_EUSCIA0,
//! - \b SYSCTL_PERIPH_TIMER32_0_MODULE,
//! - \b SYSCTL_PERIPH_TIMER16_3,
//! - \b SYSCTL_PERIPH_TIMER16_2,
//! - \b SYSCTL_PERIPH_TIMER16_1,
//! - \b SYSCTL_PERIPH_TIMER16_0
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_disablePeripheralAtCPUHalt(uint_fast16_t devices);
//*****************************************************************************
//
//! Sets the type of RESET that happens when a watchdog timeout occurs.
//!
//! \param resetType The type of reset to set
//!
//! The \e resetType parameter must be only one of the following values:
//! - \b SYSCTL_HARD_RESET,
//! - \b SYSCTL_SOFT_RESET
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_setWDTTimeoutResetType(uint_fast8_t resetType);
//*****************************************************************************
//
//! Sets the type of RESET that happens when a watchdog password violation
//! occurs.
//!
//! \param resetType The type of reset to set
//!
//! The \e resetType parameter must be only one of the following values:
//! - \b SYSCTL_HARD_RESET,
//! - \b SYSCTL_SOFT_RESET
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_setWDTPasswordViolationResetType(uint_fast8_t resetType);
//*****************************************************************************
//
//! Disables NMIs for the provided modules. When disabled, a NMI flag will not
//! occur when a fault condition comes from the corresponding modules.
//!
//! \param flags The NMI sources to disable
//! Can be a bitwise OR of the following parameters:
//! - \b SYSCTL_NMIPIN_SRC,
//! - \b SYSCTL_PCM_SRC,
//! - \b SYSCTL_PSS_SRC,
//! - \b SYSCTL_CS_SRC
//!
//
//*****************************************************************************
extern void SysCtl_disableNMISource(uint_fast8_t flags);
//*****************************************************************************
//
//! Enables NMIs for the provided modules. When enabled, a NMI flag will
//! occur when a fault condition comes from the corresponding modules.
//!
//! \param flags The NMI sources to enable
//! Can be a bitwise OR of the following parameters:
//! - \b SYSCTL_NMIPIN_SRC,
//! - \b SYSCTL_PCM_SRC,
//! - \b SYSCTL_PSS_SRC,
//! - \b SYSCTL_CS_SRC
//!
//
//*****************************************************************************
extern void SysCtl_enableNMISource(uint_fast8_t flags);
//*****************************************************************************
//
//! Returns the current sources of NMIs that are enabled
//!
//! \return Bitwise OR of NMI flags that are enabled
//
//*****************************************************************************
extern uint_fast8_t SysCtl_getNMISourceStatus(void);
//*****************************************************************************
//
//! Enables glitch suppression on the reset pin of the device. Refer to the
//! device data sheet for specific information about glitch suppression
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_enableGlitchFilter(void);
//*****************************************************************************
//
//! Disables glitch suppression on the reset pin of the device. Refer to the
//! device data sheet for specific information about glitch suppression
//!
//! \return None.
//
//
//*****************************************************************************
extern void SysCtl_disableGlitchFilter(void);
//*****************************************************************************
//
//! Retrieves the calibration constant of the temperature sensor to be used
//! in temperature calculation.
//!
//! \param refVoltage Reference voltage being used.
//!
//! The \e refVoltage parameter must be only one of the following values:
//! - \b SYSCTL_1_2V_REF
//! - \b SYSCTL_1_45V_REF
//! - \b SYSCTL_2_5V_REF
//!
//! \param temperature is the calibration temperature that the user wants to be
//! returned.
//!
//! The \e temperature parameter must be only one of the following values:
//! - \b SYSCTL_30_DEGREES_C
//! - \b SYSCTL_85_DEGREES_C
//!
//! \return None.
//
//
//*****************************************************************************
extern uint_fast16_t SysCtl_getTempCalibrationConstant(uint32_t refVoltage,
uint32_t temperature);
//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
#endif /* __MCU_HAS_SYSCTL__ */
#endif // __SYSCTL_H__
sysinit.c
/****************************************************/
//MSP432P401R
//时钟配置
//Bilibili:m-RNA
//E-mail:[email protected]
//创建日期:2021/8/11
/****************************************************/
#include "sysinit.h"
//High:48MHz Low:32768Hz
//MCLK=48MHz SMCLK=48MHz
void SysInit(void)
{
WDTCTL = WDTPW | WDTHOLD; // 停用看门狗
/* 第一步需要配置我们的时钟引脚,这里的高速时钟使用的是外部晶振*/
//低速时钟初始化比较慢
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN3 | GPIO_PIN2, GPIO_PRIMARY_MODULE_FUNCTION); //High
MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN0 | GPIO_PIN1, GPIO_PRIMARY_MODULE_FUNCTION); //Low
CS_setExternalClockSourceFrequency(32768, 48000000);
/* Starting HFXT in non-bypass mode without a timeout. Before we start
* we have to change VCORE to 1 to support the 48MHz frequency */
MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
/* 更改闪存控制器使用的等待状态数用于读取操作。
当改变时钟的频率范围时,必须使用此函数以允许可读闪存
通俗来讲就是CPU跑太快了,Flash跟不上,让CPU等等它 */
MAP_FlashCtl_setWaitState(FLASH_BANK0, 1);
MAP_FlashCtl_setWaitState(FLASH_BANK1, 1);
CS_startHFXT(false); //这是晶体 需要驱动
CS_startLFXT(CS_LFXT_DRIVE3); //驱动等级3
MAP_CS_initClockSignal(CS_MCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); //48MHz 16分频时,滴答延时可达到最长
MAP_CS_initClockSignal(CS_SMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); //48MHz
}
baudrate_calculate.h
/****************************************************/
// MSP432P401R
// 串口波特率计算
// Bilibili:m-RNA
// E-mail:[email protected]
/****************************************************/
/****************************** 说明 ******************************
*
* 源码为TI官方编写,本人只是将JS程序移植到了C语言平台,仅作为学习使用。源码出处为:
* http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
*
* ? 已知问题:
* 调试时发现某些情况下,C语言的小数的大小与JS的相差较大,
* 导致了算出的UCSx(即secondModReg)不一样,
* 这时如果出现不能准确传输时,请换一个波特率。
*
* ? 需要注意:
* 波特率不能大于时钟频率,否则会退出函数
*
* ***************************** 版本说明 ******************************
*
* ? v1.2 2021/8/29
* 注释掉了闪烁灯的代码
*
* ? v1.1 2021/8/27
* 添加支持固件库v3_21_00_05
*
* ? v1.0 2021/8/25
* 仅支持固件库v3_40_01_02
*
* ******************************* 结束 *******************************/
#ifndef __RNA_BAUDRATE_CALCULATE_H
#define __RNA_BAUDRATE_CALCULATE_H
#include "driverlib.h"
//错误指示灯宏定义 方便移植使用
//MSP432P401R 有两个红灯P1.0 P2.0
//#define WARN_LED_1_PORT GPIO_PORT_P1
//#define WARN_LED_2_PORT GPIO_PORT_P2
//#define WARN_LED_1_PIN GPIO_PIN0
//#define WARN_LED_2_PIN GPIO_PIN0
//#define WARN_LED_INIT MAP_GPIO_setAsOutputPin
//#define WARN_LED_ON MAP_GPIO_setOutputHighOnPin
//#define WARN_LED_OFF MAP_GPIO_setOutputLowOnPin
#ifdef EUSCI_A_UART_7_BIT_LEN
void eusci_calcBaudDividers(eUSCI_UART_ConfigV1 *uart_config, uint32_t baudRate); //固件库v3_40_01_02
#else
void eusci_calcBaudDividers(eUSCI_UART_Config *uart_config, uint32_t baudRate); //固件库v3_21_00_05
#endif
#endif
baudrate_calculate.c
/****************************************************/
// MSP432P401R
// 串口波特率计算
// Bilibili:m-RNA
// E-mail:[email protected]
/****************************************************/
/****************************** 说明 ******************************
*
* 源码为TI官方编写,本人只是将JS程序移植到了C语言平台,仅作为学习使用。源码出处为:
* http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
*
* ? 已知问题:
* 调试时发现某些情况下,C语言的小数的大小与JS的相差较大,
* 导致了算出的UCSx(即secondModReg)不一样,
* 这时如果出现不能准确传输时,请换一个波特率。
*
* ? 需要注意:
* 波特率不能大于时钟频率,否则会退出函数
*
* ***************************** 版本说明 ******************************
*
* ? v1.2 2021/8/29
* 注释掉了闪烁灯的代码
*
* ? v1.1 2021/8/27
* 添加支持固件库v3_21_00_05
*
* ? v1.0 2021/8/25
* 仅支持固件库v3_40_01_02
*
* ******************************* 结束 *******************************/
#include "baudrate_calculate.h"
//void uart_warning_led(void);
/*
* ======== bitPosition ========
* return 1(0) if the specified bit position in value is set(clear)
*/
bool bitPosition(uint16_t value, uint16_t position)
{
if ((value & (1 << position)))
return 1;
return 0;
}
/*
* ======== eusci_calcBaudDividers ========
* computes the eUSCI_UART register settings for a given clock and baud rate
*
* UCOS16: the oversampling bit (0 or 1)
* UCBRx: the Baud Rate Control Word
* UCFx: the First modulation stage select (UCBRFx)
* UCSx: the Second modulation stage select (UCBRSx)
* maxAbsError: the maximum TX error for the register setting above
*
* The first four field names match the names used in Table 18-5,
* "Recommended Settings for Typical Crystals and Baudrates", of the
* MSP430FR57xx Family User's Guide (SLAU272A).
*/
#ifdef EUSCI_A_UART_7_BIT_LEN
void eusci_calcBaudDividers(eUSCI_UART_ConfigV1 *uart_config, uint32_t baudRate) //固件库v3_40_01_02
#else
void eusci_calcBaudDividers(eUSCI_UART_Config *uart_config, uint32_t baudRate) //固件库v3_21_00_05
#endif
{
float maxAbsErrorInByte;
float minAbsError;
float error;
uint8_t ii;
uint16_t jj;
uint16_t NN;
uint32_t count;
uint32_t clockRate;
if (!uart_config || !baudRate) //传参错误 退出函数
{
//uart_warning_led(); //闪烁错误指示灯10次
return;
}
if (uart_config->selectClockSource == EUSCI_A_UART_CLOCKSOURCE_SMCLK)
clockRate = MAP_CS_getSMCLK();
else if (uart_config->selectClockSource == EUSCI_A_UART_CLOCKSOURCE_ACLK)
clockRate = MAP_CS_getACLK();
else
{
uart_config->selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
clockRate = MAP_CS_getSMCLK();
}
if (baudRate > clockRate) //判断波特率是否大于时钟频率 是则退出函数
{
//uart_warning_led(); //闪烁错误指示灯10次
return;
}
//var result = {UCOS16 : 0, UCBRx : 0, UCFx : 0, UCSx : 0, maxAbsError : 0};
NN = (uint16_t)((float)clockRate / (float)baudRate); //应该是不需要floor
minAbsError = 100000;
for (jj = 0; jj <= 255; jj++)
{
maxAbsErrorInByte = 0;
count = 0;
for (ii = 0; ii <= 10; ii++)
{
count += NN + bitPosition(jj, 7 - (ii % 8));
//error = (ii + 1) * baudPeriod - count * clockPeriod;
error = (ii + 1) / (float)baudRate - count / (float)clockRate; //为了减少变量,改为此代码
if (error < 0)
error = -error;
if (error > maxAbsErrorInByte)
maxAbsErrorInByte = error;
}
if (maxAbsErrorInByte - minAbsError < -7.3e-12f) //这里就是“已知问题”
{
minAbsError = maxAbsErrorInByte;
uart_config->secondModReg = jj;
}
}
if (NN < 20)
{
uart_config->overSampling = 0;
uart_config->clockPrescalar = NN;
uart_config->firstModReg = 0;
}
else
{
uart_config->overSampling = 1;
uart_config->clockPrescalar = (uint16_t)((float)NN / 16.0f); //应该是不需要floor
uart_config->firstModReg = NN - (uart_config->clockPrescalar * 16);
}
//return minAbsError * baudRate * 100;
}
闪烁错误指示灯10次
//void uart_warning_led(void)
//{
// uint8_t ii;
// uint32_t jj;
// WARN_LED_INIT(WARN_LED_1_PORT, WARN_LED_1_PIN);
// WARN_LED_INIT(WARN_LED_2_PORT, WARN_LED_2_PIN);
// for (ii = 0; ii < 10; ii++)
// {
// WARN_LED_ON(WARN_LED_1_PORT, WARN_LED_1_PIN);
// WARN_LED_OFF(WARN_LED_2_PORT, WARN_LED_2_PIN);
// for (jj = 0; jj < 100000; jj++)
// ;
// WARN_LED_OFF(WARN_LED_1_PORT, WARN_LED_1_PIN);
// WARN_LED_ON(WARN_LED_2_PORT, WARN_LED_2_PIN);
// for (jj = 0; jj < 100000; jj++)
// ;
// }
//}
main.c
#include "driverlib.h"
/* Standard Includes */
#include
#include
#include "sysinit.h"
#include "usart.h"
#include "baudrate_calculate.h"
int main(void)
{
SysInit(); //1.配置时钟
uart_init(115200); //包含了2.配置GPIO复用 3.初始化串口 4.开启串口模块
printf("MSP432\r\n");
printf("2021/8/24\r\n\r\n");
char c = '!';
char *s = "printf test";
int i = -12345;
unsigned u = 4321;
long int l = -123456780;
unsigned long n = 1098765432;
unsigned x = 0x89AB;
printf("Char %c\r\n", c);
printf("String %s\r\n", s);
printf("Integer %d\r\n", i);
printf("Unsigned %u\r\n", u);
printf("Long %d\r\n", l);
printf("Unsigned long %u\r\n", n);
printf("HEX %X\r\n", x);
while (1)
{
// 使用微库则可支持 scanf
// char a[100];
// scanf("%s", a);
// printf("%s\r\n", a);
}
}
注意:未知原因scanf用不了,勾选了微库也无法解决