Cortex-M0 MCU Clocks & Pins 1/26/2016 Richard Kuo Assistant Professor
Outline Introduction of main.c Introduction of SYS_init.c Intorduction of MCU_init.h NuTool – PinView
main.c Comment for EVB, MCU & connections pin connections // // proj_SmartHome : receive command from Android phone/tablet // EVB : NuTiny-EVB-Nano102 // MCU : Nano102SC2AN (LQFP64) // Features : // printf to display message on Debugger's UART#1 view // UART1 to HC05 : transmit/receive data through Bluetooth // baudrate=9600, databit=8, stopbit=1, paritybit=0, flowcontrol=None // TIMER0 interrupt every 0.1 sec, then the command from Android phone/tablet // GPIO to input device : read button or sensor // GPIO to output device: control LED on/off // ADC4 to photoresistor : read luminance // I2C1 to 3-axis Accelerometer & Gyroscope, OLED display // RTC to report time #include <stdio.h> #include <math.h> #include <string.h> #include "Nano1X2Series.h“ #include “MCU_init.h” #include “SYS_init.h” Comment for EVB, MCU & connections pin connections include header files
main.c Global Variables Interrupt Service Routine uint8_t Text[24]; uint8_t Text_RTC[32]; volatile char RX_buffer[RXBUFSIZE]; volatile uint8_t command; volatile uint32_t u32ADCvalue; volatile uint32_t g_u32TICK = 0; volatile uint8_t Flag_report =0; void UART1_IRQHandler(void) { uint32_t u32IntSts= UART1->ISR; if(u32IntSts & UART_IS_RX_READY(UART1)) { UART_Read(UART1, RX_buffer, RXBUFSIZE); command=RX_buffer[0]; Flag_report=1; //printf("%c\n", command); } Global Variables Interrupt Service Routine
Host Clock Selection
SYS_init.c – System Initialization void SYS_Init(void) { SYS_UnlockReg(); // Unlock protected registers // select Chip clock source & set clock source CLK_EnableXtalRC(MCU_CLOCK_SOURCE_MASK); // Enable HXT external 12MHz crystal CLK_WaitClockReady(MCU_CLOCK_STABLE_MASK); CLK_SetCoreClock(MCU_CLOCK_FREQUENCY); // Set HCLK clock to 32MHz // Select IP clock source & enable module #ifdef MCU_INTERFACE_RTC CLK_EnableModuleClock(RTC_MODULE); CLK_SetModuleClock(RTC_MODULE, CLK_CLKSEL2_RTC_SEL_10K_LXT, 0); #endif Set HCLK Set IP clock
SYS_init.c – IP Clock Cetting #ifdef MCU_INTERFACE_WDT CLK_EnableModuleClock(WDT_MODULE); #ifdef WDT_CLOCK_SOURCE_LIRC CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0); #endif #ifdef CLK_CLKSEL1_WDT_S_HCLK_DIV2048 CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_HCLK_DIV2048, 0); #ifdef WDT_CLOCK_SOURCE_LXT CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LXT, 0); #ifdef MCU_INTERFACE_WWDT CLK_EnableModuleClock(WWDT_MODULE); #ifdef WWDT_CLOCK_SOURCE_HCLK_DIV2048 CLK_SetModuleClock(WWDT_MODULE, CLK_CLKSEL2_WWDT_S_HCLK_DIV2048, 0); #ifdef WWDT_CLOCK_SOURCE_LIRC CLK_SetModuleClock(WWDT_MODULE, CLK_CLKSEL2_WWDT_S_LIRC, 0); WDT clock setting WWDT clock setting
SYS_init.c – Set MultiFunction Pins // Init I/O Multi-function // Set multi-function pins for ACMP #ifdef PIN_ACMP0_P_PC6 SYS->GPC_MFP = (SYS->GPC_MFP & ~SYS_GPC_MFP_PC6_Msk) | SYS_GPC_MFP_PC6_ACMP0_P; SYS->ALT_MFP1= (SYS->ALT_MFP1& ~SYS_ALT_MFP_PC6_Msk) | SYS_ALT_MFP1_PC6_ACMP0_P; GPIO_DISABLE_DIGITAL_PATH(PC, BIT6); #endif #ifdef PIN_ACMP0_N_PC7 SYS->GPC_MFP = (SYS->GPC_MFP & ~SYS_GPC_MFP_PC7_Msk) | SYS_GPC_MFP_PC7_ACMP0_N; SYS->ALT_MFP1= (SYS->ALT_MFP1& ~SYS_ALT_MFP_PC7_Msk) | SYS_ALT_MFP1_PC7_ACMP0_N; GPIO_DISABLE_DIGITAL_PATH(PC, BIT7); #ifdef PIN_ACMP1_P_PC14 SYS->GPC_MFP = (SYS->GPC_MFP & ~SYS_GPC_MFP_PC14_Msk) | SYS_GPC_MFP_PC14_ACMP1_P; SYS->ALT_MFP1= (SYS->ALT_MFP1& ~SYS_ALT_MFP_PC14_Msk) | SYS_ALT_MFP1_PC14_ACMP1_P; GPIO_DISABLE_DIGITAL_PATH(PC, BIT14);
MCU_init.h //Define Clock source #define MCU_CLOCK_SOURCE #define MCU_CLOCK_SOURCE_HXT // HXT, LXT, HIRC, LIRC #define MCU_CLOCK_SOURCE_LXT #define MCU_CLOCK_FREQUENCY 50000000 //Hz //Define MCU Interfaces
MCU_init.h with ADC #define MCU_INTERFACE_ADC #define ADC_CLOCK_SOURCE_HXT // HXT, LXT, PLL, HIRC, HCLK #define ADC_CLOCK_DIVIDER 1 #define PIN_ADC_0 #define PIN_ADC_1 #define PIN_ADC_2 #define PIN_ADC_3 #define ADC_CHANNEL_MASK ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_CH_2_MASK | ADC_CH_3_MASK #define ADC_INPUT_MODE ADC_INPUT_MODE_SINGLE_END // SINGLE_END, DIFFERENTIAL #define ADC_OPERATION_MODE ADC_OPERATION_MODE_CONTINUOUS // SINGLE, SINGLE_CYCLE, CONTINUOUS
MCU_init.h with Timer #define MCU_INTERFACE_TMR0 #define TMR0_CLOCK_SOURCE_HIRC // HXT, LXT, HCLK, EXT, LIRC, HIRC #define TMR0_CLOCK_DIVIDER 1 #define TMR0_OPERATING_MODE TIMER_ONESHOT_MODE // ONESHOT, PERIODIC, TOGGLE, CONTINUOUS #define TMR0_OPERATING_FREQ 1 //Hz
MCU_init.h with Timer Capture #define MCU_INTERFACE_TMR2 #define TMR2_CLOCK_SOURCE_HXT // HXT, LXT, HCLK, EXT, LIRC, HIRC #define TMR2_CLOCK_DIVIDER 12 #define TMR2_OPERATING_MODE TIMER_CONTINUOUS_MODE // ONESHOT, PERIODIC, TOGGLE, CONTINUOUS #define TMR2_OPERATING_FREQ 1000000 //Hz #define TMR2_EVENT_DETECTION TIMER_COUNTER_FALLING_EDGE // FALLING, RISING #define TMR2_CAPTURE_MODE TIMER_CAPTURE_FREE_COUNTING_MODE // FREE_COUNTING, COUNTER_RESET #define TMR2_CAPTURE_EDGE TIMER_CAPTURE_FALLING_AND_RISING_EDGE // FALLING, RISING, FALLING_AND_RISING #define PIN_TC2_PB2 // TC0_PB15, TC1_PE5, TC2_PB2, TC3_PB3
MCU_init.h with WDT #define MCU_INTERFACE_WDT #define WDT_CLOCK_SOURCE_LXT // LIRC, HCLK_DIV2048, LXT
MCU_init.h with RTC #define MCU_INTERFACE_RTC
MCU_init.h with ADC #define MCU_INTERFACE_ADC #define ADC_CLOCK_SOURCE_HXT // HXT, LXT, PLL, HIRC, HCLK #define ADC_CLOCK_DIVIDER 1 #define PIN_ADC_0 #define PIN_ADC_1 #define PIN_ADC_2 #define PIN_ADC_3 #define ADC_CHANNEL_MASK ADC_CH_0_MASK | ADC_CH_1_MASK | ADC_CH_2_MASK | ADC_CH_3_MASK #define ADC_INPUT_MODE ADC_INPUT_MODE_SINGLE_END // SINGLE_END, DIFFERENTIAL #define ADC_OPERATION_MODE ADC_OPERATION_MODE_CONTINUOUS // SINGLE, SINGLE_CYCLE, CONTINUOUS
MCU_init.h with PWM //Define MCU Interfaces #define MCU_INTERFACE_PWM0 #define PWM0_CH01_CLOCK_SOURCE_HCLK // HXT, LXT, HCLK, HIRC #define PIN_PWM0_CH0_PA12 #define PIN_PWM0_CH1_PA13 #define PIN_PWM0_CH2_PA14 #define PIN_PWM0_CH3_PA15 different MCU seriers may use a different pin out of the interface
MCU_init.h with PWM #define MCU_INTERFACE_PWM0 #define PWM0_CH01_CLOCK_SOURCE_HCLK // HXT, LXT, HCLK, HIRC #define PIN_PWM0_CH0_PA12 #define PIN_PWM0_CH1_PA13 #define PIN_PWM0_CH2_PA14 #define PIN_PWM0_CH3_PA15 different MCU seriers may use a different pin out of the interface
MCU_init.h with SPI #define MCU_INTERFACE_SPI3 #define SPI_CLOCK_SOURCE_HCLK #define PIN_SPI3_SCLK_PD9 #define PIN_SPI3_MISO0_PD10 #define PIN_SPI3_MOSI0_PD11 //#define PIN_SPI3_MISO1_PD12 //#define PIN_SPI3_MOSI1_PD13 different MCU seriers may use a different pin out of the interface
MCU_init.h with I2C #define MCU_INTERFACE_I2C1 #define I2C1_CLOCK_FREQUENCY 400000 //Hz #define PIN_I2C1_SCL_PA11 #define PIN_I2C1_SDA_PA10 different MCU seriers may use a different pin out of the interface
MCU_init.h with UART #define MCU_INTERFACE_UART1 #define UART_CLOCK_SOURCE_HIRC // HXT, LXT, PLL, HIRC #define UART_CLOCK_DIVIDER 3 #define PIN_UART1_RX_PB4 #define PIN_UART1_TX_PB5 different MCU seriers may use a different pin out of the interface
MCU_init.h with USB #define MCU_INTERFACE_USB #define USB_CLOCK_DIVIDER 1
NuTool Download Download NuTool - PinView 1.28.6378.zip
NuTool – Pinview installation Unzip & run .exe to install
NuTool – PinView (Nu-LB-NUC140)
NuTool – PinView (NuTiny-EVB-Nano102)
NUC140 LQFP-100
Nano102 LQFP-64
M051 QFN-33
M051 LQFP48
Mini51 LQFP48
Mini51/54 TSSOP
Mini58 TSSOP20
Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.