diff --git a/docs/README.md b/docs/README.md index c82a273..828e2f9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,301 @@ # EC-student -# EC HAL Documentation +--- -https://github.com/ykkimhgu/EC-student/blob/main/docs/EC_HAL_Documentation.md \ No newline at end of file +# Embedded Controller - STM32f411 HAL API + +Documentation for HAL functions + +Written by: 000 + +Course: 임베디드컨트롤러 + + + +Program: C/C++ + +IDE/Compiler: Keil uVision 5 + +OS: WIn10 + +MCU: STM32F411RE (Nucleo-64) + + + +--- + +[TOC] + + * [GPIO Digital In/Out](#gpio-digital-inout) + + [Header File](#header-file) + + [GPIO_init\(\)](#gpio-init----) + + [GPIO_mode\(\)](#gpio-mode----) + + [GPIO_write\(\)](#gpio-write----) + + [GPIO_read\(\)](#gpio-read----) + + [GPIO_ospeed\(\)](#gpio-ospeed----) + + [GPIO_otype\(\)](#gpio-otype----) + + [GPIO_pupdr\(\)](#gpio-pupdr----) + +--- + +## GPIO Digital InOut + +### Header File + + `#include "ecGPIO.h"` + + +```c++ +#include "stm32f411xe.h" +#include "ecRCC.h" + +#ifndef __ECGPIO_H +#define __ECGPIO_H + +// MODER +#define INPUT 0x00 +#define OUTPUT 0x01 +#define AF 0x02 +#define ANALOG 0x03 + +// IDR & ODR +#define HIGH 1 +#define LOW 0 + +// OSPEED +#define LOW_SPEED 0x00 +#define MID_SPEED 0x01 +#define FAST_SPEED 0x02 +#define HIGH_SPEED 0x03 + +// OTYPER +#define PUSH_PULL 0 // Push-pull +#define OPEN_DRAIN 1 // Open-Drain + +// PUDR +#define NO_PUPD 0x00 // No pull-up, pull-down +#define PULL_UP 0x01 // Pull-up +#define PULL_DOWN 0x02 // Pull-down +#define RESERVED 0x03 // Reserved + +// PIN +#define LED_PIN 5 +#define BUTTON_PIN 13 + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +void GPIO_init(GPIO_TypeDef *Port, int pin, int mode); +void GPIO_mode(GPIO_TypeDef* Port, int pin, int mode); +void GPIO_write(GPIO_TypeDef *Port, int pin, int output); +int GPIO_read(GPIO_TypeDef *Port, int pin); +void GPIO_ospeed(GPIO_TypeDef* Port, int pin, int speed); +void GPIO_otype(GPIO_TypeDef* Port, int pin, int type); +void GPIO_pupdr(GPIO_TypeDef* Port, int pin, int pupd); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif +``` + + + + +### GPIO_init\(\) + +Initializes GPIO pins with default setting and Enables GPIO Clock. Mode: In/Out/AF/Analog + +```c++ +void GPIO_init(GPIO_TypeDef *Port, int pin, int mode); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH + +* **pin**: pin number (int) 0~15 + +* **mode**: INPUT(0), OUTPUT(1), AF(02), ANALOG (03) + + + +**Example code** + +```c++ +GPIO_init(GPIOA, 5, OUTPUT); +GPIO_init(GPIOC, 13, INPUT); //GPIO_init(GPIOC, 13, 0); +``` + + + +### GPIO_mode\(\) + +Configures GPIO pin modes: In/Out/AF/Analog + +```c++ +void GPIO_init(GPIO_TypeDef *Port, int pin, int mode); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH + +* **pin**: pin number (int) 0~15 + +* **mode**: INPUT (0), OUTPUT (1), AF(02), ANALOG (03) + + + +**Example code** + +```c++ +GPIO_mode(GPIOA, 5, OUTPUT); +``` + + + +### GPIO_write\(\) + +Write the data to GPIO pin: High, Low + +```c++ +write(GPIO_TypeDef *Port, int pin, int output); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH +* **pin**: pin number (int) 0~15 +* **output**: LOW(0), HIGH(1) + + + +**Example code** + +```c++ +GPIO_write(GPIOA, 5, 1); // 1: High +``` + + + +### GPIO_read\(\) + +Read the data from GPIO pin + +```c++ +int GPIO_read(GPIO_TypeDef *Port, int pin); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH +* **pin**: pin number (int) 0~15 + + + +**Example code** + +```c++ +GPIO_read(GPIOC, 13); +``` + + + +### GPIO_ospeed\(\) + +Configures output speed of GPIO pin : Low, Mid, Fast, High + +```c++ +void GPIO_ospeed(GPIO_TypeDef* Port, int pin, int speed); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH +* **pin**: pin number (int) 0~15 +* **speed**: LOW_SPEED(0), MID_SPEED(1), FAST_SPEED(2) , HIGH_SPEED(3) + + + +**Example code** + +```c++ +GPIO_ospeed(GPIOA, 5, 2); // 2: FAST_SPEED +``` + + + +### GPIO_otype\(\) + +Configures output type of GPIO pin: Push-Pull / Open-Drain + +```c++ +void GPIO_otype(GPIO_TypeDef* Port, int pin, int type); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH +* **pin**: pin number (int) 0~15 +* **type**: PUSH_PULL(0), OPEN_DRAIN(1) + + + +**Example code** + +```c++ +GPIO_otype(GPIOA, 5, 0); // 0: Push-Pull +``` + + + +### GPIO_pupdr\(\) + +Configures Pull-up/Pull-down mode of GPIO pin: No Pull-up, Pull-down/ Pull-up/ Pull-down/ Reserved + +```c++ +void GPIO_pupdr(GPIO_TypeDef* Port, int pin, int pupd); +``` + +**Parameters** + +* **Port:** Port Number, GPIOA~GPIOH +* **pin**: pin number (int) 0~15 +* **pupd**: NO_PUPD(0), PULL_UP(1), PULL_DOWN(2), RESERVED(3) + + + +**Example code** + +```c++ +GPIO_pupdr(GPIOA, 5, 0); // 0: No Pull-up, Pull-down +``` + + + + +------ + + + +## Class or Header name + +### Function Name + +```text + +``` + +**Parameters** + +* p1 +* p2 + +**Example code** + +```text + +``` \ No newline at end of file diff --git a/include/README.md b/include/README.md index 59770f8..8855114 100644 --- a/include/README.md +++ b/include/README.md @@ -1 +1,9 @@ -# EC-student \ No newline at end of file +# EC-student + + + +Your own library of STM32F4 go here + +* header and definition files (*.h, *.c) + + \ No newline at end of file diff --git a/tutorial/README.md b/lab-tutorial/README.md similarity index 100% rename from tutorial/README.md rename to lab-tutorial/README.md diff --git a/tutorial/tutorial-student/EC_GPIO_API_student.cpp b/lab-tutorial/lib-student/EC_GPIO_API_student.cpp similarity index 100% rename from tutorial/tutorial-student/EC_GPIO_API_student.cpp rename to lab-tutorial/lib-student/EC_GPIO_API_student.cpp diff --git a/tutorial/tutorial-student/EC_GPIO_API_student.h b/lab-tutorial/lib-student/EC_GPIO_API_student.h similarity index 100% rename from tutorial/tutorial-student/EC_GPIO_API_student.h rename to lab-tutorial/lib-student/EC_GPIO_API_student.h diff --git a/tutorial/tutorial-student/ecADC_student.c b/lab-tutorial/lib-student/ecADC_student.c similarity index 96% rename from tutorial/tutorial-student/ecADC_student.c rename to lab-tutorial/lib-student/ecADC_student.c index f3451e6..e9e5f13 100644 --- a/tutorial/tutorial-student/ecADC_student.c +++ b/lab-tutorial/lib-student/ecADC_student.c @@ -1,204 +1,204 @@ -#include "stm32f411xe.h" -#include "ecSysTick.h" -#include "ecADC_student.h" -#include "ecGPIO.h" -#include "ecTIM.h" -#include -uint32_t result; - -void ADC_init(GPIO_TypeDef *port, int pin, int trigmode){ //mode 0 : SW, 1 : TRGO -// 0. Match Port and Pin for ADC channel - int CHn = ADC_pinmap(port, pin); // ADC Channel <->Port/Pin mapping - -// GPIO configuration --------------------------------------------------------------------- -// 1. Initialize GPIO port and pin as ANALOG, no pull up / pull down - GPIO_init(port, pin, ANALOG); // ANALOG = 3 - GPIO_pupdr(port, pin, EC_NONE); // EC_NONE = 0 - -// ADC configuration --------------------------------------------------------------------- -// 1. Total time of conversion setting - // Enable ADC pheripheral clock - RCC->APB2ENR |= ___________; // Enable the clock of RCC_APB2ENR_ADC1EN - - // Configure ADC clock pre-scaler - ADC->CCR &= ___________; // 0000: PCLK2 divided by 2 (42MHz) - - // Configure ADC resolution - ADC1->CR1 &= ___________; // 00: 12-bit resolution (15cycle+) - - // Configure channel sampling time of conversion. - // Software is allowed to write these bits only when ADSTART=0 and JADSTART=0 !! - // ADC clock cycles @42MHz = 2us - if(CHn < 10) ADC1->SMPR2 |= 4U << ___________; // sampling time conversion : 84 - else ADC1->SMPR1 |= 4U << ___________; - -// 2. Regular / Injection Group - //Regular: SQRx, Injection: JSQx - -// 3. Repetition: Single or Continuous conversion - ADC1->CR2 &= ___________; // default : Single conversion mode - -// 4. Single Channel or Scan mode - // - Single Channel: scan mode, right alignment - ADC1->CR1 |= ___________; // 1: Scan mode enable - ADC1->CR2 &= ___________; // 0: Right alignment - - // Configure the sequence length - ADC1->SQR1 &= ___________; // 0000: 1 conversion in the regular channel conversion sequence - - // Configure the channel sequence - ADC1->SQR3 &= ___________; // SQ1 clear bits - ADC1->SQR3 |= (CHn & ___________); // Choose the channel to convert firstly - - -// 5. Interrupt Enable - // Enable EOC(conversion) interrupt. - ADC1->CR1 &= ___________; // Interrupt reset - ADC1->CR1 |= ___________; // Interrupt enable - - // Enable ADC_IRQn - NVIC_SetPriority(___________); // Set Priority to 2 - NVIC_EnableIRQ(___________); // Enable interrupt form ACD1 peripheral - - -/* -------------------------------------------------------------------------------------*/ -// HW TRIGGER MODE -/* -------------------------------------------------------------------------------------*/ - - // TRGO Initialize : TIM3, 1msec, RISE edge - if(trigmode==TRGO) ADC_TRGO(TIM3, 1, RISE); - -} - -void ADC_TRGO(TIM_TypeDef* TIMx, int msec, int edge){ - // set timer - int timer = 0; - if(TIMx==TIM2) timer=2; - else if(TIMx==TIM3) timer=3; - - // Single conversion mode (disable continuous conversion) - ADC1->CR2 &= ___________; // Discontinuous conversion mode - ADC1->CR2 |= ___________; // Enable EOCS - - -// HW Trigger configuration ------------------------------------------------------------- - -// 1. TIMx Trigger Output Config - // Enable TIMx Clock - TIM_init(TIMx, msec); - TIMx->CR1 &= ___________; //counter disable - - // Set PSC, ARR - // TIM_period_ms(TIMx, msec); - - // Master Mode Selection MMS[2:0]: Trigger output (TRGO) - TIMx->CR2 &= ___________; // reset MMS - TIMx->CR2 |= ___________; //100: Compare - OC1REF signal is used as trigger output (TRGO) - - // Output Compare Mode - TIMx->CCMR1 &= ~(7<<4); // OC1M : output compare 1 Mode - TIMx->CCMR1 |= 6<<4; // OC1M = 110 for compare 1 Mode ch1 - - // OC1 signal - TIMx->CCER |= ___________; // CC1E Capture enabled - TIMx->CCR1 = (TIMx->ARR)/2; // duty ratio 50% - - // Enable TIMx - TIMx->CR1 |= ___________; //counter enable - -// 2. ADC HW Trigger Config. - // Select Trigger Source - ADC1->CR2 &= ~ADC_CR2_EXTSEL; // reset EXTSEL - ADC1->CR2 |= (timer*2+2)<<24; // TIMx TRGO event (ADC : TIM2, TIM3 TRGO) - - //Select Trigger Polarity - ADC1->CR2 &= ~ADC_CR2_EXTEN; // reset EXTEN, default - if(edge==RISE) ADC1->CR2 |= ADC_CR2_EXTEN_0; // trigger detection rising edge - else if(edge==FALL) ADC1->CR2 |= ADC_CR2_EXTEN_1; // trigger detection falling edge - else if(edge==BOTH) ADC1->CR2 |= ADC_CR2_EXTEN_Msk; // trigger detection both edge - -} - -void ADC_continue(int contmode){ - if(contmode==CONT){ - // Repetition: Continuous conversion - ADC1->CR2 |= ___________; // Enable Continuous conversion mode - ADC1->CR1 &= ~ADC_CR1_SCAN; // 0: Scan mode disable - } - else //if(contmode==SINGLE) - { - // Repetition: Single conversion - ADC1->CR2 &= ~ADC_CR2_CONT; // Disable Continuous conversion mode - ADC1->CR1 |= ADC_CR1_SCAN; // 1: Scan mode enable - } -} - -void ADC_sequence(int length, int *seq){ - - ADC1->SQR1 &= ~(0xF<<20); // reset length of conversions in the regular channel - ADC1->SQR1 |= (length-1)<<20; // conversions in the regular channel conversion sequence - - for(int i = 0; iSQR3 &= ~(0x1F<SQR3 |= seq[i]<SQR2 &= ___________; // SQn clear bits - ADC1->SQR2 |= ___________; // Choose the channel to convert sequence - } - else{ - ADC1->SQR1 &= ~(0x1F<<(i-12)*5); // SQn clear bits - ADC1->SQR1 |= seq[i]<<(i-12)*5; // Choose the channel to convert sequence - } - } -} - -void ADC_start(void){ - // Enable ADON, SW Trigger------------------------------------------------------------------------------- - ADC1->CR2 |= ___________; - ADC1->CR2 |= ___________; -} - -uint32_t is_ADC_EOC(void){ - return ADC1->SR & ___________; -} - -uint32_t is_ADC_OVR(void){ - return ADC1->SR & ___________; -} - -void clear_ADC_OVR(void){ - ADC1->SR &= ___________; -} - -uint32_t ADC_read(){ - return ADC1->___________; -} - -uint32_t ADC_pinmap(GPIO_TypeDef *Port, int Pin){ - if(Port == GPIOA){ - if(Pin == 0) return 0; - else if(Pin == 1) return 1; - else if(Pin == 4) return 4; - else if(Pin == 5) return 5; - else if(Pin == 6) return 6; - else if(Pin == 7) return 7; - else while(1); - } - else if(Port == GPIOB){ - if(Pin == 0) return 8; - else if(Pin == 1) return 9; - else while(1); - } - else if(Port == GPIOC){ - if(Pin == 0) return 10; - else if(Pin == 1) return 11; - else if(Pin == 2) return 12; - else if(Pin == 3) return 13; - else if(Pin == 4) return 14; - else if(Pin == 5) return 15; - else while(1); - } -} - +#include "stm32f411xe.h" +#include "ecSysTick.h" +#include "ecADC_student.h" +#include "ecGPIO.h" +#include "ecTIM.h" +#include +uint32_t result; + +void ADC_init(GPIO_TypeDef *port, int pin, int trigmode){ //mode 0 : SW, 1 : TRGO +// 0. Match Port and Pin for ADC channel + int CHn = ADC_pinmap(port, pin); // ADC Channel <->Port/Pin mapping + +// GPIO configuration --------------------------------------------------------------------- +// 1. Initialize GPIO port and pin as ANALOG, no pull up / pull down + GPIO_init(port, pin, ANALOG); // ANALOG = 3 + GPIO_pupdr(port, pin, EC_NONE); // EC_NONE = 0 + +// ADC configuration --------------------------------------------------------------------- +// 1. Total time of conversion setting + // Enable ADC pheripheral clock + RCC->APB2ENR |= ___________; // Enable the clock of RCC_APB2ENR_ADC1EN + + // Configure ADC clock pre-scaler + ADC->CCR &= ___________; // 0000: PCLK2 divided by 2 (42MHz) + + // Configure ADC resolution + ADC1->CR1 &= ___________; // 00: 12-bit resolution (15cycle+) + + // Configure channel sampling time of conversion. + // Software is allowed to write these bits only when ADSTART=0 and JADSTART=0 !! + // ADC clock cycles @42MHz = 2us + if(CHn < 10) ADC1->SMPR2 |= 4U << ___________; // sampling time conversion : 84 + else ADC1->SMPR1 |= 4U << ___________; + +// 2. Regular / Injection Group + //Regular: SQRx, Injection: JSQx + +// 3. Repetition: Single or Continuous conversion + ADC1->CR2 &= ___________; // default : Single conversion mode + +// 4. Single Channel or Scan mode + // - Single Channel: scan mode, right alignment + ADC1->CR1 |= ___________; // 1: Scan mode enable + ADC1->CR2 &= ___________; // 0: Right alignment + + // Configure the sequence length + ADC1->SQR1 &= ___________; // 0000: 1 conversion in the regular channel conversion sequence + + // Configure the channel sequence + ADC1->SQR3 &= ___________; // SQ1 clear bits + ADC1->SQR3 |= (CHn & ___________); // Choose the channel to convert firstly + + +// 5. Interrupt Enable + // Enable EOC(conversion) interrupt. + ADC1->CR1 &= ___________; // Interrupt reset + ADC1->CR1 |= ___________; // Interrupt enable + + // Enable ADC_IRQn + NVIC_SetPriority(___________); // Set Priority to 2 + NVIC_EnableIRQ(___________); // Enable interrupt form ACD1 peripheral + + +/* -------------------------------------------------------------------------------------*/ +// HW TRIGGER MODE +/* -------------------------------------------------------------------------------------*/ + + // TRGO Initialize : TIM3, 1msec, RISE edge + if(trigmode==TRGO) ADC_TRGO(TIM3, 1, RISE); + +} + +void ADC_TRGO(TIM_TypeDef* TIMx, int msec, int edge){ + // set timer + int timer = 0; + if(TIMx==TIM2) timer=2; + else if(TIMx==TIM3) timer=3; + + // Single conversion mode (disable continuous conversion) + ADC1->CR2 &= ___________; // Discontinuous conversion mode + ADC1->CR2 |= ___________; // Enable EOCS + + +// HW Trigger configuration ------------------------------------------------------------- + +// 1. TIMx Trigger Output Config + // Enable TIMx Clock + TIM_init(TIMx, msec); + TIMx->CR1 &= ___________; //counter disable + + // Set PSC, ARR + // TIM_period_ms(TIMx, msec); + + // Master Mode Selection MMS[2:0]: Trigger output (TRGO) + TIMx->CR2 &= ___________; // reset MMS + TIMx->CR2 |= ___________; //100: Compare - OC1REF signal is used as trigger output (TRGO) + + // Output Compare Mode + TIMx->CCMR1 &= ~(7<<4); // OC1M : output compare 1 Mode + TIMx->CCMR1 |= 6<<4; // OC1M = 110 for compare 1 Mode ch1 + + // OC1 signal + TIMx->CCER |= ___________; // CC1E Capture enabled + TIMx->CCR1 = (TIMx->ARR)/2; // duty ratio 50% + + // Enable TIMx + TIMx->CR1 |= ___________; //counter enable + +// 2. ADC HW Trigger Config. + // Select Trigger Source + ADC1->CR2 &= ~ADC_CR2_EXTSEL; // reset EXTSEL + ADC1->CR2 |= (timer*2+2)<<24; // TIMx TRGO event (ADC : TIM2, TIM3 TRGO) + + //Select Trigger Polarity + ADC1->CR2 &= ~ADC_CR2_EXTEN; // reset EXTEN, default + if(edge==RISE) ADC1->CR2 |= ADC_CR2_EXTEN_0; // trigger detection rising edge + else if(edge==FALL) ADC1->CR2 |= ADC_CR2_EXTEN_1; // trigger detection falling edge + else if(edge==BOTH) ADC1->CR2 |= ADC_CR2_EXTEN_Msk; // trigger detection both edge + +} + +void ADC_continue(int contmode){ + if(contmode==CONT){ + // Repetition: Continuous conversion + ADC1->CR2 |= ___________; // Enable Continuous conversion mode + ADC1->CR1 &= ~ADC_CR1_SCAN; // 0: Scan mode disable + } + else //if(contmode==SINGLE) + { + // Repetition: Single conversion + ADC1->CR2 &= ~ADC_CR2_CONT; // Disable Continuous conversion mode + ADC1->CR1 |= ADC_CR1_SCAN; // 1: Scan mode enable + } +} + +void ADC_sequence(int length, int *seq){ + + ADC1->SQR1 &= ~(0xF<<20); // reset length of conversions in the regular channel + ADC1->SQR1 |= (length-1)<<20; // conversions in the regular channel conversion sequence + + for(int i = 0; iSQR3 &= ~(0x1F<SQR3 |= seq[i]<SQR2 &= ___________; // SQn clear bits + ADC1->SQR2 |= ___________; // Choose the channel to convert sequence + } + else{ + ADC1->SQR1 &= ~(0x1F<<(i-12)*5); // SQn clear bits + ADC1->SQR1 |= seq[i]<<(i-12)*5; // Choose the channel to convert sequence + } + } +} + +void ADC_start(void){ + // Enable ADON, SW Trigger------------------------------------------------------------------------------- + ADC1->CR2 |= ___________; + ADC1->CR2 |= ___________; +} + +uint32_t is_ADC_EOC(void){ + return ADC1->SR & ___________; +} + +uint32_t is_ADC_OVR(void){ + return ADC1->SR & ___________; +} + +void clear_ADC_OVR(void){ + ADC1->SR &= ___________; +} + +uint32_t ADC_read(){ + return ADC1->___________; +} + +uint32_t ADC_pinmap(GPIO_TypeDef *Port, int Pin){ + if(Port == GPIOA){ + if(Pin == 0) return 0; + else if(Pin == 1) return 1; + else if(Pin == 4) return 4; + else if(Pin == 5) return 5; + else if(Pin == 6) return 6; + else if(Pin == 7) return 7; + else while(1); + } + else if(Port == GPIOB){ + if(Pin == 0) return 8; + else if(Pin == 1) return 9; + else while(1); + } + else if(Port == GPIOC){ + if(Pin == 0) return 10; + else if(Pin == 1) return 11; + else if(Pin == 2) return 12; + else if(Pin == 3) return 13; + else if(Pin == 4) return 14; + else if(Pin == 5) return 15; + else while(1); + } +} + diff --git a/tutorial/tutorial-student/ecADC_student.h b/lab-tutorial/lib-student/ecADC_student.h similarity index 95% rename from tutorial/tutorial-student/ecADC_student.h rename to lab-tutorial/lib-student/ecADC_student.h index 6b22cea..c6e00c6 100644 --- a/tutorial/tutorial-student/ecADC_student.h +++ b/lab-tutorial/lib-student/ecADC_student.h @@ -1,39 +1,39 @@ -#ifndef __MY_ADC_H -#define __MY_ADC_H -#include "stm32f411xe.h" - -// ADC trigmode -#define SW 0 -#define TRGO 1 - -// ADC contmode -#define CONT 0 -#define SINGLE 1 - -// Edge Type -#define RISE 1 -#define FALL 2 -#define BOTH 3 - -#define _DEFAULT 0 - -// ADC setting -// Exercise- with Tutorial -void ADC_init(GPIO_TypeDef *port, int pin, int trigmode); // trigmode : SW , TRGO - -// Exercise- with LAB -void ADC_continue(int contmode); // contmode : CONT, SINGLE / Operate both ADC,JADC -void ADC_TRGO(TIM_TypeDef* TIMx, int msec, int edge); -void ADC_sequence(int length, int *seq); - -// Exercise- with Tutorial -void ADC_start(void); -uint32_t is_ADC_EOC(void); -uint32_t ADC_read(void); -uint32_t ADC_pinmap(GPIO_TypeDef *port, int pin); - -// Exercise- with LAB -uint32_t is_ADC_OVR(void); -void clear_ADC_OVR(void); - -#endif +#ifndef __MY_ADC_H +#define __MY_ADC_H +#include "stm32f411xe.h" + +// ADC trigmode +#define SW 0 +#define TRGO 1 + +// ADC contmode +#define CONT 0 +#define SINGLE 1 + +// Edge Type +#define RISE 1 +#define FALL 2 +#define BOTH 3 + +#define _DEFAULT 0 + +// ADC setting +// Exercise- with Tutorial +void ADC_init(GPIO_TypeDef *port, int pin, int trigmode); // trigmode : SW , TRGO + +// Exercise- with LAB +void ADC_continue(int contmode); // contmode : CONT, SINGLE / Operate both ADC,JADC +void ADC_TRGO(TIM_TypeDef* TIMx, int msec, int edge); +void ADC_sequence(int length, int *seq); + +// Exercise- with Tutorial +void ADC_start(void); +uint32_t is_ADC_EOC(void); +uint32_t ADC_read(void); +uint32_t ADC_pinmap(GPIO_TypeDef *port, int pin); + +// Exercise- with LAB +uint32_t is_ADC_OVR(void); +void clear_ADC_OVR(void); + +#endif diff --git a/tutorial/tutorial-student/ecGPIO.c b/lab-tutorial/lib-student/ecGPIO.c similarity index 100% rename from tutorial/tutorial-student/ecGPIO.c rename to lab-tutorial/lib-student/ecGPIO.c diff --git a/tutorial/tutorial-student/ecGPIO.h b/lab-tutorial/lib-student/ecGPIO.h similarity index 100% rename from tutorial/tutorial-student/ecGPIO.h rename to lab-tutorial/lib-student/ecGPIO.h diff --git a/tutorial/tutorial-student/ecIC_student.c b/lab-tutorial/lib-student/ecIC_student.c similarity index 97% rename from tutorial/tutorial-student/ecIC_student.c rename to lab-tutorial/lib-student/ecIC_student.c index f685d32..27a7767 100644 --- a/tutorial/tutorial-student/ecIC_student.c +++ b/lab-tutorial/lib-student/ecIC_student.c @@ -1,199 +1,199 @@ -#include "ecTIM.h" -#include "ecGPIO.h" -#include "math.h" - -/* Input Capture */ - -void ICAP_init(IC_t *ICx, GPIO_TypeDef *port, int pin){ -// 0. Match Input Capture Port and Pin for TIMx - ICx->port = port; - ICx->pin = pin; - ICAP_pinmap(ICx); // Port, Pin --(mapping)--> TIMx, Channel - - TIM_TypeDef *TIMx = ICx->timer; - int TIn = ICx->ch; - int ICn=TIn; - ICx->ICnum=ICn; // (default) TIx=ICx - -// GPIO configuration --------------------------------------------------------------------- -// 1. Initialize GPIO port and pin as AF - ________________________; // GPIO init as AF=2 - GPIO_ospeed(port, pin, 3); // speed VHIGH=3 - -// 2. Configure GPIO AFR by Pin num. - if(TIMx == TIM1 || TIMx == TIM2) port->AFR[pin >> 3] |= 0x01 << (4*(pin % 8)); // TIM1~2 - else if // for TIM3~ - ________________________; - - -// TIMER configuration --------------------------------------------------------------------- -// 1. Initialize Timer - TIM_init(TIMx, 1); -// 2. Initialize Timer Interrpt - TIM_INT_init(TIMx, 1); // TIMx Interrupt initialize -// 3. Modify ARR Maxium for 1MHz - TIMx->PSC = 84-1; // Timer counter clock: 1MHz(1us) for PLL - TIMx->ARR = 0xFFFF; // Set auto reload register to maximum (count up to 65535) -// 4. Disable Counter during configuration - TIMx->CR1 &= ~TIM_CR1_CEN; // Disable Counter during configuration - - - -// Input Capture configuration --------------------------------------------------------------------- -// 1. Select Timer channel(TIx) for Input Capture channel(ICx) - // Default Setting - TIMx->CCMR1 |= TIM_CCMR1_CC1S_0; //01<<0 CC1S TI1=IC1 - ________________________; //01<<8 CC2s TI2=IC2 - ________________________; //01<<0 CC3s TI3=IC3 - ________________________; //01<<8 CC4s TI4=IC4 - - -// 2. Filter Duration (use default) - -// 3. IC Prescaler (use default) - - -// 4. Activation Edge: CCyNP/CCyP - TIMx->CCER ___________________; // CCy(Rising) for ICn - - -// 5. Enable CCy Capture, Capture/Compare interrupt - TIMx->CCER ___________________; // CCn(ICn) Capture Enable - -// 6. Enable Interrupt of CC(CCyIE), Update (UIE) - TIMx->DIER ___________________; // Capture/Compare Interrupt Enable for ICn - TIMx->DIER |= TIM_DIER_UIE; // Update Interrupt enable - -// 7. Enable Counter - TIMx->CR1 |= TIM_CR1_CEN; // Counter enable -} - - - - -// Configure Selecting TIx-ICy and Edge Type -void ICAP_setup(IC_t *ICx, int ICn, int edge_type){ - TIM_TypeDef *TIMx = ICx->timer; // TIMx - int CHn = ICx->ch; // Timer Channel CHn - ICx->ICnum=ICn; - -// Disable CC. Disable CCInterrupt for ICn. - TIMx->CCER ___________________; // Capture Disable - TIMx->DIER ___________________; // CCn Interrupt Disable - - -// Configure IC number(user selected) with given IC pin(TIMx_CHn) - switch(ICn){ - case 1: - TIMx->CCMR1 &= ~TIM_CCMR1_CC1S; //reset CC1S - if (ICn==CHn) TIMx->CCMR1 |= TIM_CCMR1_CC1S_0; //01<<0 CC1S Tx_Ch1=IC1 - else ___________________; //10<<0 CC1S Tx_Ch2=IC1 - break; - case 2: - TIMx->CCMR1 ___________________; //reset CC2S - if (ICn==CHn) TIMx->CCMR1 ___________________; //01<<0 CC2S Tx_Ch2=IC2 - else ___________________; //10<<0 CC2S Tx_Ch1=IC2 - break; - case 3: - TIMx->CCMR2 &= ~TIM_CCMR2_CC3S; //reset CC3S - if (ICn==CHn) TIMx->CCMR2 ___________________; //01<<0 CC3S Tx_Ch3=IC3 - else TIMx->CCMR2 ___________________; //10<<0 CC3S Tx_Ch4=IC3 - break; - case 4: - TIMx->CCMR2 ___________________; //reset CC4S - if (ICn==CHn) ___________________; //01<<0 CC4S Tx_Ch4=IC4 - else TIMx->CCMR2 ___________________; //10<<0 CC4S Tx_Ch3=IC4 - break; - default: break; - } - - -// Configure Activation Edge direction - TIMx->CCER ___________________; // Clear CCnNP/CCnP bits for ICn - switch(edge_type){ - case RISE: TIMx->CCER ___________________; break; //rising: 00 - case FALL: TIMx->CCER ___________________; break; //falling: 01 - case BOTH: TIMx->CCER ___________________; break; //both: 11 - } - -// Enable CC. Enable CC Interrupt. - TIMx->CCER |= 1 << (4*(ICn - 1)); // Capture Enable - TIMx->DIER |= 1 << ICn; // CCn Interrupt enabled -} - - - -// Time span for one counter step -void ICAP_counter_us(IC_t *ICx, int usec){ - TIM_TypeDef *TIMx = ICx->timer; - TIMx->PSC = 84*usec-1; // Timer counter clock: 1us * usec - TIMx->ARR = 0xFFFF; // Set auto reload register to maximum (count up to 65535) -} - - - - -uint32_t is_pending_TIM(TIM_TypeDef *TIMx){ - return ((TIMx->SR & TIM_SR_UIF) == ___________________; -} - -void clear_pending_TIM(TIM_TypeDef *TIMx){ - TIMx->SR ___________________; -} - -uint32_t is_CCIF(TIM_TypeDef *TIMx, uint32_t ccNum){ - return (TIMx->SR ___________________; -} - -void clear_CCIF(TIM_TypeDef *TIMx, uint32_t ccNum){ - TIMx->SR ___________________; -} - - - -//DO NOT MODIFY THIS -void ICAP_pinmap(IC_t *timer_pin){ - GPIO_TypeDef *port = timer_pin->port; - int pin = timer_pin->pin; - - if(port == GPIOA) { - switch(pin){ - case 0 : timer_pin->timer = TIM2; timer_pin->ch = 1; break; - case 1 : timer_pin->timer = TIM2; timer_pin->ch = 2; break; - case 5 : timer_pin->timer = TIM2; timer_pin->ch = 1; break; - case 6 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; - //case 7: timer_pin->timer = TIM1; timer_pin->ch = 1N; break; - case 8 : timer_pin->timer = TIM1; timer_pin->ch = 1; break; - case 9 : timer_pin->timer = TIM1; timer_pin->ch = 2; break; - case 10: timer_pin->timer = TIM1; timer_pin->ch = 3; break; - case 15: timer_pin->timer = TIM2; timer_pin->ch = 1; break; - default: break; - } - } - else if(port == GPIOB) { - switch(pin){ - //case 0: timer_pin->timer = TIM1; timer_pin->ch = 2N; break; - //case 1: timer_pin->timer = TIM1; timer_pin->ch = 3N; break; - case 3 : timer_pin->timer = TIM2; timer_pin->ch = 2; break; - case 4 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; - case 5 : timer_pin->timer = TIM3; timer_pin->ch = 2; break; - case 6 : timer_pin->timer = TIM4; timer_pin->ch = 1; break; - case 7 : timer_pin->timer = TIM4; timer_pin->ch = 2; break; - case 8 : timer_pin->timer = TIM4; timer_pin->ch = 3; break; - case 9 : timer_pin->timer = TIM4; timer_pin->ch = 3; break; - case 10: timer_pin->timer = TIM2; timer_pin->ch = 3; break; - - default: break; - } - } - else if(port == GPIOC) { - switch(pin){ - case 6 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; - case 7 : timer_pin->timer = TIM3; timer_pin->ch = 2; break; - case 8 : timer_pin->timer = TIM3; timer_pin->ch = 3; break; - case 9 : timer_pin->timer = TIM3; timer_pin->ch = 4; break; - - default: break; - } - } -} +#include "ecTIM.h" +#include "ecGPIO.h" +#include "math.h" + +/* Input Capture */ + +void ICAP_init(IC_t *ICx, GPIO_TypeDef *port, int pin){ +// 0. Match Input Capture Port and Pin for TIMx + ICx->port = port; + ICx->pin = pin; + ICAP_pinmap(ICx); // Port, Pin --(mapping)--> TIMx, Channel + + TIM_TypeDef *TIMx = ICx->timer; + int TIn = ICx->ch; + int ICn=TIn; + ICx->ICnum=ICn; // (default) TIx=ICx + +// GPIO configuration --------------------------------------------------------------------- +// 1. Initialize GPIO port and pin as AF + ________________________; // GPIO init as AF=2 + GPIO_ospeed(port, pin, 3); // speed VHIGH=3 + +// 2. Configure GPIO AFR by Pin num. + if(TIMx == TIM1 || TIMx == TIM2) port->AFR[pin >> 3] |= 0x01 << (4*(pin % 8)); // TIM1~2 + else if // for TIM3~ + ________________________; + + +// TIMER configuration --------------------------------------------------------------------- +// 1. Initialize Timer + TIM_init(TIMx, 1); +// 2. Initialize Timer Interrpt + TIM_INT_init(TIMx, 1); // TIMx Interrupt initialize +// 3. Modify ARR Maxium for 1MHz + TIMx->PSC = 84-1; // Timer counter clock: 1MHz(1us) for PLL + TIMx->ARR = 0xFFFF; // Set auto reload register to maximum (count up to 65535) +// 4. Disable Counter during configuration + TIMx->CR1 &= ~TIM_CR1_CEN; // Disable Counter during configuration + + + +// Input Capture configuration --------------------------------------------------------------------- +// 1. Select Timer channel(TIx) for Input Capture channel(ICx) + // Default Setting + TIMx->CCMR1 |= TIM_CCMR1_CC1S_0; //01<<0 CC1S TI1=IC1 + ________________________; //01<<8 CC2s TI2=IC2 + ________________________; //01<<0 CC3s TI3=IC3 + ________________________; //01<<8 CC4s TI4=IC4 + + +// 2. Filter Duration (use default) + +// 3. IC Prescaler (use default) + + +// 4. Activation Edge: CCyNP/CCyP + TIMx->CCER ___________________; // CCy(Rising) for ICn + + +// 5. Enable CCy Capture, Capture/Compare interrupt + TIMx->CCER ___________________; // CCn(ICn) Capture Enable + +// 6. Enable Interrupt of CC(CCyIE), Update (UIE) + TIMx->DIER ___________________; // Capture/Compare Interrupt Enable for ICn + TIMx->DIER |= TIM_DIER_UIE; // Update Interrupt enable + +// 7. Enable Counter + TIMx->CR1 |= TIM_CR1_CEN; // Counter enable +} + + + + +// Configure Selecting TIx-ICy and Edge Type +void ICAP_setup(IC_t *ICx, int ICn, int edge_type){ + TIM_TypeDef *TIMx = ICx->timer; // TIMx + int CHn = ICx->ch; // Timer Channel CHn + ICx->ICnum=ICn; + +// Disable CC. Disable CCInterrupt for ICn. + TIMx->CCER ___________________; // Capture Disable + TIMx->DIER ___________________; // CCn Interrupt Disable + + +// Configure IC number(user selected) with given IC pin(TIMx_CHn) + switch(ICn){ + case 1: + TIMx->CCMR1 &= ~TIM_CCMR1_CC1S; //reset CC1S + if (ICn==CHn) TIMx->CCMR1 |= TIM_CCMR1_CC1S_0; //01<<0 CC1S Tx_Ch1=IC1 + else ___________________; //10<<0 CC1S Tx_Ch2=IC1 + break; + case 2: + TIMx->CCMR1 ___________________; //reset CC2S + if (ICn==CHn) TIMx->CCMR1 ___________________; //01<<0 CC2S Tx_Ch2=IC2 + else ___________________; //10<<0 CC2S Tx_Ch1=IC2 + break; + case 3: + TIMx->CCMR2 &= ~TIM_CCMR2_CC3S; //reset CC3S + if (ICn==CHn) TIMx->CCMR2 ___________________; //01<<0 CC3S Tx_Ch3=IC3 + else TIMx->CCMR2 ___________________; //10<<0 CC3S Tx_Ch4=IC3 + break; + case 4: + TIMx->CCMR2 ___________________; //reset CC4S + if (ICn==CHn) ___________________; //01<<0 CC4S Tx_Ch4=IC4 + else TIMx->CCMR2 ___________________; //10<<0 CC4S Tx_Ch3=IC4 + break; + default: break; + } + + +// Configure Activation Edge direction + TIMx->CCER ___________________; // Clear CCnNP/CCnP bits for ICn + switch(edge_type){ + case RISE: TIMx->CCER ___________________; break; //rising: 00 + case FALL: TIMx->CCER ___________________; break; //falling: 01 + case BOTH: TIMx->CCER ___________________; break; //both: 11 + } + +// Enable CC. Enable CC Interrupt. + TIMx->CCER |= 1 << (4*(ICn - 1)); // Capture Enable + TIMx->DIER |= 1 << ICn; // CCn Interrupt enabled +} + + + +// Time span for one counter step +void ICAP_counter_us(IC_t *ICx, int usec){ + TIM_TypeDef *TIMx = ICx->timer; + TIMx->PSC = 84*usec-1; // Timer counter clock: 1us * usec + TIMx->ARR = 0xFFFF; // Set auto reload register to maximum (count up to 65535) +} + + + + +uint32_t is_pending_TIM(TIM_TypeDef *TIMx){ + return ((TIMx->SR & TIM_SR_UIF) == ___________________; +} + +void clear_pending_TIM(TIM_TypeDef *TIMx){ + TIMx->SR ___________________; +} + +uint32_t is_CCIF(TIM_TypeDef *TIMx, uint32_t ccNum){ + return (TIMx->SR ___________________; +} + +void clear_CCIF(TIM_TypeDef *TIMx, uint32_t ccNum){ + TIMx->SR ___________________; +} + + + +//DO NOT MODIFY THIS +void ICAP_pinmap(IC_t *timer_pin){ + GPIO_TypeDef *port = timer_pin->port; + int pin = timer_pin->pin; + + if(port == GPIOA) { + switch(pin){ + case 0 : timer_pin->timer = TIM2; timer_pin->ch = 1; break; + case 1 : timer_pin->timer = TIM2; timer_pin->ch = 2; break; + case 5 : timer_pin->timer = TIM2; timer_pin->ch = 1; break; + case 6 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; + //case 7: timer_pin->timer = TIM1; timer_pin->ch = 1N; break; + case 8 : timer_pin->timer = TIM1; timer_pin->ch = 1; break; + case 9 : timer_pin->timer = TIM1; timer_pin->ch = 2; break; + case 10: timer_pin->timer = TIM1; timer_pin->ch = 3; break; + case 15: timer_pin->timer = TIM2; timer_pin->ch = 1; break; + default: break; + } + } + else if(port == GPIOB) { + switch(pin){ + //case 0: timer_pin->timer = TIM1; timer_pin->ch = 2N; break; + //case 1: timer_pin->timer = TIM1; timer_pin->ch = 3N; break; + case 3 : timer_pin->timer = TIM2; timer_pin->ch = 2; break; + case 4 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; + case 5 : timer_pin->timer = TIM3; timer_pin->ch = 2; break; + case 6 : timer_pin->timer = TIM4; timer_pin->ch = 1; break; + case 7 : timer_pin->timer = TIM4; timer_pin->ch = 2; break; + case 8 : timer_pin->timer = TIM4; timer_pin->ch = 3; break; + case 9 : timer_pin->timer = TIM4; timer_pin->ch = 3; break; + case 10: timer_pin->timer = TIM2; timer_pin->ch = 3; break; + + default: break; + } + } + else if(port == GPIOC) { + switch(pin){ + case 6 : timer_pin->timer = TIM3; timer_pin->ch = 1; break; + case 7 : timer_pin->timer = TIM3; timer_pin->ch = 2; break; + case 8 : timer_pin->timer = TIM3; timer_pin->ch = 3; break; + case 9 : timer_pin->timer = TIM3; timer_pin->ch = 4; break; + + default: break; + } + } +} diff --git a/tutorial/tutorial-student/ecIC_student.h b/lab-tutorial/lib-student/ecIC_student.h similarity index 94% rename from tutorial/tutorial-student/ecIC_student.h rename to lab-tutorial/lib-student/ecIC_student.h index bdb2177..249395d 100644 --- a/tutorial/tutorial-student/ecIC_student.h +++ b/lab-tutorial/lib-student/ecIC_student.h @@ -1,42 +1,42 @@ -#ifndef __EC_TIM_H -#define __EC_TIM_H -#include "stm32f411xe.h" - -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - - -/* Input Capture*/ - -// Edge Type -#define RISE 0 -#define FALL 1 -#define BOTH 2 - -//Input Capture - -typedef struct{ - GPIO_TypeDef *port; - int pin; - TIM_TypeDef *timer; - int ch; //int Timer Channel - int ICnum; //int IC number -} IC_t; - - - -void ICAP_init(IC_t *ICx, GPIO_TypeDef *port, int pin); -void ICAP_setup(IC_t *ICx, int IC_number, int edge_type); -void ICAP_counter_us(IC_t *ICx, int usec); - -void ICAP_pinmap(IC_t *timer_pin); - -uint32_t is_pending_TIM(TIM_TypeDef *TIMx); -void clear_pending_TIM(TIM_TypeDef *TIMx); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +#ifndef __EC_TIM_H +#define __EC_TIM_H +#include "stm32f411xe.h" + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + + +/* Input Capture*/ + +// Edge Type +#define RISE 0 +#define FALL 1 +#define BOTH 2 + +//Input Capture + +typedef struct{ + GPIO_TypeDef *port; + int pin; + TIM_TypeDef *timer; + int ch; //int Timer Channel + int ICnum; //int IC number +} IC_t; + + + +void ICAP_init(IC_t *ICx, GPIO_TypeDef *port, int pin); +void ICAP_setup(IC_t *ICx, int IC_number, int edge_type); +void ICAP_counter_us(IC_t *ICx, int usec); + +void ICAP_pinmap(IC_t *timer_pin); + +uint32_t is_pending_TIM(TIM_TypeDef *TIMx); +void clear_pending_TIM(TIM_TypeDef *TIMx); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/tutorial/tutorial-student/ecPWM_student.c b/lab-tutorial/lib-student/ecPWM_student.c similarity index 97% rename from tutorial/tutorial-student/ecPWM_student.c rename to lab-tutorial/lib-student/ecPWM_student.c index 8bb71c9..83c4870 100644 --- a/tutorial/tutorial-student/ecPWM_student.c +++ b/lab-tutorial/lib-student/ecPWM_student.c @@ -1,185 +1,185 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: EC_HAL_for_student_exercise -* -****************************************************************************** -*/ - - -#include "ecPWM_student.h" - -/* PWM Configuration */ - -void PWM_init(PWM_t *pwm, GPIO_TypeDef *port, int pin){ -// 0. Match Output Port and Pin for TIMx - pwm->port = port; - pwm->pin = pin; - PWM_pinmap(pwm); - TIM_TypeDef *TIMx = pwm->timer; - int CHn = pwm->ch; - -// 1. Initialize GPIO port and pin as AF - // YOUR CODE GOES HERE - GPIO_init(port, pin, __); // AF=2 - GPIO_ospeed(port, pin,__); // speed VHIGH=3 - - -// 2. Configure GPIO AFR by Pin num. - // AFR[0] for pin: 0~7, AFR[1] for pin 8~15 - // AFR=1 for TIM1,TIM2 AFR=2 for TIM3 etc - - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - - -// 3. Initialize Timer - TIM_init(TIMx, 1); // with default msec=1 value. - -// 3-2. Direction of Counter - //YOUR CODE GOES HERE - TIMx->CR1 ________________; // Counting direction: 0 = up-counting, 1 = down-counting - - -// 4. Configure Timer Output mode as PWM - uint32_t ccVal=TIMx->ARR/2; // default value CC=ARR/2 - if(CHn == 1){ - TIMx->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear ouput compare mode bits for channel 1 - TIMx->CCMR1 |= ___________; // OC1M = 110 for PWM Mode 1 output on ch1. #define TIM_CCMR1_OC1M_1 (0x2UL << TIM_CCMR1_OC1M_Pos) - TIMx->CCMR1 |= TIM_CCMR1_OC1PE; // Output 1 preload enable (make CCR1 value changable) - TIMx->CCR1 = ccVal; // Output Compare Register for channel 1 (default duty ratio = 50%) - TIMx->CCER &= ~TIM_CCER_CC1P; // select output polarity: active high - TIMx->CCER |= _____________; // Enable output for ch1 - } - else if(CHn == 2){ - TIMx->CCMR1 &= ~TIM_CCMR1_OC2M; // Clear ouput compare mode bits for channel 2 - // YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch2 - // YOUR CODE GOES HERE // Output 1 preload enable (make CCR2 value changable) - // YOUR CODE GOES HERE // Output Compare Register for channel 2 (default duty ratio = 50%) - // YOUR CODE GOES HERE // select output polarity: active high - // YOUR CODE GOES HERE // Enable output for ch2 - } - else if(CHn == 3){ - TIMx->CCMR2 &= ~TIM_CCMR2_OC3M; // Clear ouput compare mode bits for channel 3 - // YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch3 - // YOUR CODE GOES HERE // Output 1 preload enable (make CCR3 value changable) - // YOUR CODE GOES HERE // Output Compare Register for channel 3 (default duty ratio = 50%) - // YOUR CODE GOES HERE // select output polarity: active high - // YOUR CODE GOES HERE // Enable output for ch3 - } - else if(CHn == 4){ - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - } - - - -// 5. Enable Timer Counter - if(TIMx == TIM1) TIMx->BDTR |= TIM_BDTR_MOE; // Main output enable (MOE): 0 = Disable, 1 = Enable - TIMx->CR1 |= TIM_CR1_CEN; // Enable counter -} - - -void PWM_period_ms(PWM_t *PWM_pin, uint32_t msec){ - TIM_TypeDef *TIMx = PWM_pin->timer; - TIM_period_ms(___, _____); //YOUR CODE GOES HERE -} - -void PWM_period_us(PWM_t *PWM_pin, uint32_t usec){ - TIM_TypeDef *TIMx = PWM_pin->timer; - TIM_period_us(___, _____); //YOUR CODE GOES HERE -} - - -void PWM_pulsewidth_ms(PWM_t *pwm, float pulse_width_ms){ - TIM_TypeDef *TIMx = pwm->timer; - int CHn = pwm->ch; - uint32_t fsys = 0; - uint32_t psc=pwm->timer->PSC; - - // Check System CLK: PLL or HSI - if((RCC->CFGR & (3<<0)) == 2) { fsys = 84000; } // for msec 84MHz/1000 - else if((RCC->CFGR & (3<<0)) == 0) { fsys = 16000; } - - //YOUR CODE GOES HERE - float fclk = _______________ // fclk=fsys/(psc+1); - uint32_t ccval = ____________ // width_ms *fclk - 1; - - //YOUR CODE GOES HERE - switch(CHn){ - case 1: TIMx->CCR1 = ccval; break; - // REPEAT for CHn=2, 3, 4 - // REPEAT for CHn=2, 3, 4 - // REPEAT for CHn=2, 3, 4 - default: break; - } -} - - -void PWM_duty(PWM_t *pwm, float duty) { // duty=0 to 1 - float ccval = ___________________; // (ARR+1)*dutyRatio - 1 - int CHn = pwm->ch; - - //YOUR CODE GOES HERE - switch(CHn){ - case 1: TIMx->CCR1 = ccval; break; - // REPEAT for CHn=2, 3, 4 - // REPEAT for CHn=2, 3, 4 - // REPEAT for CHn=2, 3, 4 - default: break; - } -} - - -// DO NOT MODIFY HERE -void PWM_pinmap(PWM_t *PWM_pin){ - GPIO_TypeDef *port = PWM_pin->port; - int pin = PWM_pin->pin; - - if(port == GPIOA) { - switch(pin){ - case 0 : PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; - case 1 : PWM_pin->timer = TIM2; PWM_pin->ch = 2; break; - case 5 : PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; - case 6 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; - //case 7: PWM_pin->timer = TIM1; PWM_pin->ch = 1N; break; - case 8 : PWM_pin->timer = TIM1; PWM_pin->ch = 1; break; - case 9 : PWM_pin->timer = TIM1; PWM_pin->ch = 2; break; - case 10: PWM_pin->timer = TIM1; PWM_pin->ch = 3; break; - case 15: PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; - default: break; - } - } - else if(port == GPIOB) { - switch(pin){ - //case 0: PWM_pin->timer = TIM1; PWM_pin->ch = 2N; break; - //case 1: PWM_pin->timer = TIM1; PWM_pin->ch = 3N; break; - case 3 : PWM_pin->timer = TIM2; PWM_pin->ch = 2; break; - case 4 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; - case 5 : PWM_pin->timer = TIM3; PWM_pin->ch = 2; break; - case 6 : PWM_pin->timer = TIM4; PWM_pin->ch = 1; break; - case 7 : PWM_pin->timer = TIM4; PWM_pin->ch = 2; break; - case 8 : PWM_pin->timer = TIM4; PWM_pin->ch = 3; break; - case 9 : PWM_pin->timer = TIM4; PWM_pin->ch = 3; break; - case 10: PWM_pin->timer = TIM2; PWM_pin->ch = 3; break; - - default: break; - } - } - else if(port == GPIOC) { - switch(pin){ - case 6 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; - case 7 : PWM_pin->timer = TIM3; PWM_pin->ch = 2; break; - case 8 : PWM_pin->timer = TIM3; PWM_pin->ch = 3; break; - case 9 : PWM_pin->timer = TIM3; PWM_pin->ch = 4; break; - - default: break; - } - } - // TIM5 needs to be added, if used. -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: EC_HAL_for_student_exercise +* +****************************************************************************** +*/ + + +#include "ecPWM_student.h" + +/* PWM Configuration */ + +void PWM_init(PWM_t *pwm, GPIO_TypeDef *port, int pin){ +// 0. Match Output Port and Pin for TIMx + pwm->port = port; + pwm->pin = pin; + PWM_pinmap(pwm); + TIM_TypeDef *TIMx = pwm->timer; + int CHn = pwm->ch; + +// 1. Initialize GPIO port and pin as AF + // YOUR CODE GOES HERE + GPIO_init(port, pin, __); // AF=2 + GPIO_ospeed(port, pin,__); // speed VHIGH=3 + + +// 2. Configure GPIO AFR by Pin num. + // AFR[0] for pin: 0~7, AFR[1] for pin 8~15 + // AFR=1 for TIM1,TIM2 AFR=2 for TIM3 etc + + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + + +// 3. Initialize Timer + TIM_init(TIMx, 1); // with default msec=1 value. + +// 3-2. Direction of Counter + //YOUR CODE GOES HERE + TIMx->CR1 ________________; // Counting direction: 0 = up-counting, 1 = down-counting + + +// 4. Configure Timer Output mode as PWM + uint32_t ccVal=TIMx->ARR/2; // default value CC=ARR/2 + if(CHn == 1){ + TIMx->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear ouput compare mode bits for channel 1 + TIMx->CCMR1 |= ___________; // OC1M = 110 for PWM Mode 1 output on ch1. #define TIM_CCMR1_OC1M_1 (0x2UL << TIM_CCMR1_OC1M_Pos) + TIMx->CCMR1 |= TIM_CCMR1_OC1PE; // Output 1 preload enable (make CCR1 value changable) + TIMx->CCR1 = ccVal; // Output Compare Register for channel 1 (default duty ratio = 50%) + TIMx->CCER &= ~TIM_CCER_CC1P; // select output polarity: active high + TIMx->CCER |= _____________; // Enable output for ch1 + } + else if(CHn == 2){ + TIMx->CCMR1 &= ~TIM_CCMR1_OC2M; // Clear ouput compare mode bits for channel 2 + // YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch2 + // YOUR CODE GOES HERE // Output 1 preload enable (make CCR2 value changable) + // YOUR CODE GOES HERE // Output Compare Register for channel 2 (default duty ratio = 50%) + // YOUR CODE GOES HERE // select output polarity: active high + // YOUR CODE GOES HERE // Enable output for ch2 + } + else if(CHn == 3){ + TIMx->CCMR2 &= ~TIM_CCMR2_OC3M; // Clear ouput compare mode bits for channel 3 + // YOUR CODE GOES HERE // OC1M = 110 for PWM Mode 1 output on ch3 + // YOUR CODE GOES HERE // Output 1 preload enable (make CCR3 value changable) + // YOUR CODE GOES HERE // Output Compare Register for channel 3 (default duty ratio = 50%) + // YOUR CODE GOES HERE // select output polarity: active high + // YOUR CODE GOES HERE // Enable output for ch3 + } + else if(CHn == 4){ + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + } + + + +// 5. Enable Timer Counter + if(TIMx == TIM1) TIMx->BDTR |= TIM_BDTR_MOE; // Main output enable (MOE): 0 = Disable, 1 = Enable + TIMx->CR1 |= TIM_CR1_CEN; // Enable counter +} + + +void PWM_period_ms(PWM_t *PWM_pin, uint32_t msec){ + TIM_TypeDef *TIMx = PWM_pin->timer; + TIM_period_ms(___, _____); //YOUR CODE GOES HERE +} + +void PWM_period_us(PWM_t *PWM_pin, uint32_t usec){ + TIM_TypeDef *TIMx = PWM_pin->timer; + TIM_period_us(___, _____); //YOUR CODE GOES HERE +} + + +void PWM_pulsewidth_ms(PWM_t *pwm, float pulse_width_ms){ + TIM_TypeDef *TIMx = pwm->timer; + int CHn = pwm->ch; + uint32_t fsys = 0; + uint32_t psc=pwm->timer->PSC; + + // Check System CLK: PLL or HSI + if((RCC->CFGR & (3<<0)) == 2) { fsys = 84000; } // for msec 84MHz/1000 + else if((RCC->CFGR & (3<<0)) == 0) { fsys = 16000; } + + //YOUR CODE GOES HERE + float fclk = _______________ // fclk=fsys/(psc+1); + uint32_t ccval = ____________ // width_ms *fclk - 1; + + //YOUR CODE GOES HERE + switch(CHn){ + case 1: TIMx->CCR1 = ccval; break; + // REPEAT for CHn=2, 3, 4 + // REPEAT for CHn=2, 3, 4 + // REPEAT for CHn=2, 3, 4 + default: break; + } +} + + +void PWM_duty(PWM_t *pwm, float duty) { // duty=0 to 1 + float ccval = ___________________; // (ARR+1)*dutyRatio - 1 + int CHn = pwm->ch; + + //YOUR CODE GOES HERE + switch(CHn){ + case 1: TIMx->CCR1 = ccval; break; + // REPEAT for CHn=2, 3, 4 + // REPEAT for CHn=2, 3, 4 + // REPEAT for CHn=2, 3, 4 + default: break; + } +} + + +// DO NOT MODIFY HERE +void PWM_pinmap(PWM_t *PWM_pin){ + GPIO_TypeDef *port = PWM_pin->port; + int pin = PWM_pin->pin; + + if(port == GPIOA) { + switch(pin){ + case 0 : PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; + case 1 : PWM_pin->timer = TIM2; PWM_pin->ch = 2; break; + case 5 : PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; + case 6 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; + //case 7: PWM_pin->timer = TIM1; PWM_pin->ch = 1N; break; + case 8 : PWM_pin->timer = TIM1; PWM_pin->ch = 1; break; + case 9 : PWM_pin->timer = TIM1; PWM_pin->ch = 2; break; + case 10: PWM_pin->timer = TIM1; PWM_pin->ch = 3; break; + case 15: PWM_pin->timer = TIM2; PWM_pin->ch = 1; break; + default: break; + } + } + else if(port == GPIOB) { + switch(pin){ + //case 0: PWM_pin->timer = TIM1; PWM_pin->ch = 2N; break; + //case 1: PWM_pin->timer = TIM1; PWM_pin->ch = 3N; break; + case 3 : PWM_pin->timer = TIM2; PWM_pin->ch = 2; break; + case 4 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; + case 5 : PWM_pin->timer = TIM3; PWM_pin->ch = 2; break; + case 6 : PWM_pin->timer = TIM4; PWM_pin->ch = 1; break; + case 7 : PWM_pin->timer = TIM4; PWM_pin->ch = 2; break; + case 8 : PWM_pin->timer = TIM4; PWM_pin->ch = 3; break; + case 9 : PWM_pin->timer = TIM4; PWM_pin->ch = 3; break; + case 10: PWM_pin->timer = TIM2; PWM_pin->ch = 3; break; + + default: break; + } + } + else if(port == GPIOC) { + switch(pin){ + case 6 : PWM_pin->timer = TIM3; PWM_pin->ch = 1; break; + case 7 : PWM_pin->timer = TIM3; PWM_pin->ch = 2; break; + case 8 : PWM_pin->timer = TIM3; PWM_pin->ch = 3; break; + case 9 : PWM_pin->timer = TIM3; PWM_pin->ch = 4; break; + + default: break; + } + } + // TIM5 needs to be added, if used. +} diff --git a/tutorial/tutorial-student/ecPWM_student.h b/lab-tutorial/lib-student/ecPWM_student.h similarity index 95% rename from tutorial/tutorial-student/ecPWM_student.h rename to lab-tutorial/lib-student/ecPWM_student.h index e2c7f73..bb0932c 100644 --- a/tutorial/tutorial-student/ecPWM_student.h +++ b/lab-tutorial/lib-student/ecPWM_student.h @@ -1,44 +1,44 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: EC_HAL_for_student_exercise -* -****************************************************************************** -*/ - -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecTIM_student.h" // change to ecTIM.h - -#ifndef __EC_PWM_H -#define __EC_PWM_H - -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - -typedef struct{ - GPIO_TypeDef *port; - int pin; - TIM_TypeDef *timer; - int ch; -} PWM_t; - - -/* PWM Configuration */ -void PWM_init(PWM_t *pwm, GPIO_TypeDef *port, int pin); -void PWM_period_ms(PWM_t *pwm, uint32_t msec); -void PWM_period_us(PWM_t *PWM_pin, uint32_t usec); - -void PWM_pulsewidth_ms(PWM_t *pwm, float pulse_width_ms); -void PWM_duty(PWM_t *pwm, float duty); -void PWM_pinmap(PWM_t *PWM_pin); - - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: EC_HAL_for_student_exercise +* +****************************************************************************** +*/ + +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecTIM_student.h" // change to ecTIM.h + +#ifndef __EC_PWM_H +#define __EC_PWM_H + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +typedef struct{ + GPIO_TypeDef *port; + int pin; + TIM_TypeDef *timer; + int ch; +} PWM_t; + + +/* PWM Configuration */ +void PWM_init(PWM_t *pwm, GPIO_TypeDef *port, int pin); +void PWM_period_ms(PWM_t *pwm, uint32_t msec); +void PWM_period_us(PWM_t *PWM_pin, uint32_t usec); + +void PWM_pulsewidth_ms(PWM_t *pwm, float pulse_width_ms); +void PWM_duty(PWM_t *pwm, float duty); +void PWM_pinmap(PWM_t *PWM_pin); + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/tutorial/tutorial-student/ecRCC.c b/lab-tutorial/lib-student/ecRCC.c similarity index 100% rename from tutorial/tutorial-student/ecRCC.c rename to lab-tutorial/lib-student/ecRCC.c diff --git a/tutorial/tutorial-student/ecRCC.h b/lab-tutorial/lib-student/ecRCC.h similarity index 100% rename from tutorial/tutorial-student/ecRCC.h rename to lab-tutorial/lib-student/ecRCC.h diff --git a/tutorial/tutorial-student/ecStepper_student.c b/lab-tutorial/lib-student/ecStepper_student.c similarity index 95% rename from tutorial/tutorial-student/ecStepper_student.c rename to lab-tutorial/lib-student/ecStepper_student.c index f619d7f..b3916cb 100644 --- a/tutorial/tutorial-student/ecStepper_student.c +++ b/lab-tutorial/lib-student/ecStepper_student.c @@ -1,142 +1,142 @@ -#include "stm32f4xx.h" -#include "ecStepper_student.h" - -//State number -#define S0 0 -#define S1 1 -#define S2 2 -#define S3 3 -#define S4 4 -#define S5 5 -#define S6 6 -#define S7 7 - - -// Stepper Motor function -uint32_t direction = 1; -uint32_t step_delay = 100; -uint32_t step_per_rev = 64; - - -// Stepper Motor variable -volatile Stepper_t myStepper; - - -//FULL stepping sequence - FSM -typedef struct { - uint8_t out; - uint32_t next[4]; -} State_full_t; - -State_full_t FSM_full[4] = { - {0b1001,{S1,S2}}, - // YOUR CODE - // YOUR CODE - // YOUR CODE -}; - -//HALF stepping sequence -typedef struct { - uint8_t out; - uint32_t next[8]; -} State_half_t; - -State_half_t FSM_half[8] = { - {0b1001,{S1,S2}}, - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE -}; - - - -void Stepper_init(GPIO_TypeDef* port1, int pin1, GPIO_TypeDef* port2, int pin2, GPIO_TypeDef* port3, int pin3, GPIO_TypeDef* port4, int pin4){ - -// GPIO Digital Out Initiation - myStepper.port1 = port1; - myStepper.pin1 = pin1; - // Repeat for port2,pin2~port4,pin4 - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - - - -// GPIO Digital Out Initiation - // No pull-up Pull-down , Push-Pull, Fast - // Port1,Pin1 ~ Port4,Pin4 - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - - -} - -void Stepper_pinOut (uint32_t state, int mode){ - - if (mode ==FULL){ // FULL mode - GPIO_write(myStepper.port1, myStepper.pin1, // YOUR CODE_____); - // Repeat for port2,pin2~port4,pin4 - // YOUR CODE - // YOUR CODE - // YOUR CODE - - } - else if (mode ==HALF){ // HALF mode - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE - } -} - - -void Stepper_setSpeed (long whatSpeed){ // rppm - step_delay = _________//YOUR CODE // Convert rpm to milli sec -} - - -void Stepper_step(int steps, int direction,int mode){ - int step_number = 0; - myStepper._step_num = steps; - int state_number = 0; - int max_step = 3; - if (mode == HALF) max_step = 7; - - - for(;myStepper._step_num>0;myStepper._step_num--){ // run for step size - // YOUR CODE // delay (step_delay); - - if(direction) step_number++; // + direction step number++ - // YOUR CODE // - direction step number-- - - // YOUR CODE // step_number must be 0 to max_step - step_number=_____________// YOUR CODE - - - if (mode == FULL) - state_numer=___________// YOUR CODE // state_number = 0 to 3 for FULL step mode - else if (mode == HALF) - state_numer=___________// YOUR CODE // state_number = 0 to 7 for HALF step mode - - Stepper_pinOut(state_number, mode); - } -} - - -void Stepper_stop (void){ - - myStepper._step_num = 0; - // All pins(Port1~4, Pin1~4) set as DigitalOut '0' - // YOUR CODE - // YOUR CODE - // YOUR CODE - // YOUR CODE -} - +#include "stm32f4xx.h" +#include "ecStepper_student.h" + +//State number +#define S0 0 +#define S1 1 +#define S2 2 +#define S3 3 +#define S4 4 +#define S5 5 +#define S6 6 +#define S7 7 + + +// Stepper Motor function +uint32_t direction = 1; +uint32_t step_delay = 100; +uint32_t step_per_rev = 64; + + +// Stepper Motor variable +volatile Stepper_t myStepper; + + +//FULL stepping sequence - FSM +typedef struct { + uint8_t out; + uint32_t next[4]; +} State_full_t; + +State_full_t FSM_full[4] = { + {0b1001,{S1,S2}}, + // YOUR CODE + // YOUR CODE + // YOUR CODE +}; + +//HALF stepping sequence +typedef struct { + uint8_t out; + uint32_t next[8]; +} State_half_t; + +State_half_t FSM_half[8] = { + {0b1001,{S1,S2}}, + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE +}; + + + +void Stepper_init(GPIO_TypeDef* port1, int pin1, GPIO_TypeDef* port2, int pin2, GPIO_TypeDef* port3, int pin3, GPIO_TypeDef* port4, int pin4){ + +// GPIO Digital Out Initiation + myStepper.port1 = port1; + myStepper.pin1 = pin1; + // Repeat for port2,pin2~port4,pin4 + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + + + +// GPIO Digital Out Initiation + // No pull-up Pull-down , Push-Pull, Fast + // Port1,Pin1 ~ Port4,Pin4 + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + + +} + +void Stepper_pinOut (uint32_t state, int mode){ + + if (mode ==FULL){ // FULL mode + GPIO_write(myStepper.port1, myStepper.pin1, // YOUR CODE_____); + // Repeat for port2,pin2~port4,pin4 + // YOUR CODE + // YOUR CODE + // YOUR CODE + + } + else if (mode ==HALF){ // HALF mode + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE + } +} + + +void Stepper_setSpeed (long whatSpeed){ // rppm + step_delay = _________//YOUR CODE // Convert rpm to milli sec +} + + +void Stepper_step(int steps, int direction,int mode){ + int step_number = 0; + myStepper._step_num = steps; + int state_number = 0; + int max_step = 3; + if (mode == HALF) max_step = 7; + + + for(;myStepper._step_num>0;myStepper._step_num--){ // run for step size + // YOUR CODE // delay (step_delay); + + if(direction) step_number++; // + direction step number++ + // YOUR CODE // - direction step number-- + + // YOUR CODE // step_number must be 0 to max_step + step_number=_____________// YOUR CODE + + + if (mode == FULL) + state_numer=___________// YOUR CODE // state_number = 0 to 3 for FULL step mode + else if (mode == HALF) + state_numer=___________// YOUR CODE // state_number = 0 to 7 for HALF step mode + + Stepper_pinOut(state_number, mode); + } +} + + +void Stepper_stop (void){ + + myStepper._step_num = 0; + // All pins(Port1~4, Pin1~4) set as DigitalOut '0' + // YOUR CODE + // YOUR CODE + // YOUR CODE + // YOUR CODE +} + diff --git a/tutorial/tutorial-student/ecStepper_student.h b/lab-tutorial/lib-student/ecStepper_student.h similarity index 95% rename from tutorial/tutorial-student/ecStepper_student.h rename to lab-tutorial/lib-student/ecStepper_student.h index 33c5fe3..614a306 100644 --- a/tutorial/tutorial-student/ecStepper_student.h +++ b/lab-tutorial/lib-student/ecStepper_student.h @@ -1,42 +1,42 @@ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecSysTick.h" - -#ifndef __EC_STEPPER_H -#define __EC_STEPPER_H - -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - -//State mode -#define HALF 0 -#define FULL 1 - -/* Stepper Motor */ -//stepper motor function - -typedef struct{ - GPIO_TypeDef *port1; - int pin1; - GPIO_TypeDef *port2; - int pin2; - GPIO_TypeDef *port3; - int pin3; - GPIO_TypeDef *port4; - int pin4; - int _step_num; -} Stepper_t; - - -void Stepper_init(GPIO_TypeDef* port1, int pin1, GPIO_TypeDef* port2, int pin2, GPIO_TypeDef* port3, int pin3, GPIO_TypeDef* port4, int pin4); -void Stepper_setSpeed (long whatSpeed); -void Stepper_step(int steps, int direction, int mode); -void Stepper_run (int direction, int mode); -void Stepper_stop (void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecSysTick.h" + +#ifndef __EC_STEPPER_H +#define __EC_STEPPER_H + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +//State mode +#define HALF 0 +#define FULL 1 + +/* Stepper Motor */ +//stepper motor function + +typedef struct{ + GPIO_TypeDef *port1; + int pin1; + GPIO_TypeDef *port2; + int pin2; + GPIO_TypeDef *port3; + int pin3; + GPIO_TypeDef *port4; + int pin4; + int _step_num; +} Stepper_t; + + +void Stepper_init(GPIO_TypeDef* port1, int pin1, GPIO_TypeDef* port2, int pin2, GPIO_TypeDef* port3, int pin3, GPIO_TypeDef* port4, int pin4); +void Stepper_setSpeed (long whatSpeed); +void Stepper_step(int steps, int direction, int mode); +void Stepper_run (int direction, int mode); +void Stepper_stop (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/tutorial/tutorial-student/ecSysTick.c b/lab-tutorial/lib-student/ecSysTick.c similarity index 95% rename from tutorial/tutorial-student/ecSysTick.c rename to lab-tutorial/lib-student/ecSysTick.c index 9fadb4a..de35673 100644 --- a/tutorial/tutorial-student/ecSysTick.c +++ b/lab-tutorial/lib-student/ecSysTick.c @@ -1,79 +1,79 @@ -#include "ecSysTick.h" - - - -#define MCU_CLK_PLL 84000000 -#define MCU_CLK_HSI 16000000 - -volatile uint32_t msTicks; - -//EC_SYSTEM_CLK - -void SysTick_init(void){ - // SysTick Control and Status Register - SysTick->CTRL = 0; // Disable SysTick IRQ and SysTick Counter - - // Select processor clock - // 1 = processor clock; 0 = external clock - SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; - - // uint32_t MCU_CLK=EC_SYSTEM_CLK - // SysTick Reload Value Register - SysTick->LOAD = 84000000 / 1000 - 1; // 1ms, for HSI PLL = 84MHz. - - // SysTick Current Value Register - SysTick->VAL = 0; - - // Enables SysTick exception request - // 1 = counting down to zero asserts the SysTick exception request - SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; - - // Enable SysTick IRQ and SysTick Timer - SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; - - NVIC_SetPriority(SysTick_IRQn, 16); // Set Priority to 1 - NVIC_EnableIRQ(SysTick_IRQn); // Enable interrupt in NVIC -} - - - -void SysTick_Handler(void){ - SysTick_counter(); -} - -void SysTick_counter(){ - msTicks++; -} - - -void delay_ms (uint32_t mesc){ - uint32_t curTicks; - - curTicks = msTicks; - while ((msTicks - curTicks) < mesc); - - msTicks = 0; -} - -//void delay_ms(uint32_t msec){ -// uint32_t now=SysTick_val(); -// if (msec>5000) msec=5000; -// if (msec<1) msec=1; -// while ((now - SysTick_val()) < msec); -//} - - -void SysTick_reset(void) -{ - // SysTick Current Value Register - SysTick->VAL = 0; -} - -uint32_t SysTick_val(void) { - return SysTick->VAL; -} - -//void SysTick_counter(){ -// msTicks++; -// if(msTicks%1000 == 0) count++; -//} +#include "ecSysTick.h" + + + +#define MCU_CLK_PLL 84000000 +#define MCU_CLK_HSI 16000000 + +volatile uint32_t msTicks; + +//EC_SYSTEM_CLK + +void SysTick_init(void){ + // SysTick Control and Status Register + SysTick->CTRL = 0; // Disable SysTick IRQ and SysTick Counter + + // Select processor clock + // 1 = processor clock; 0 = external clock + SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; + + // uint32_t MCU_CLK=EC_SYSTEM_CLK + // SysTick Reload Value Register + SysTick->LOAD = 84000000 / 1000 - 1; // 1ms, for HSI PLL = 84MHz. + + // SysTick Current Value Register + SysTick->VAL = 0; + + // Enables SysTick exception request + // 1 = counting down to zero asserts the SysTick exception request + SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; + + // Enable SysTick IRQ and SysTick Timer + SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; + + NVIC_SetPriority(SysTick_IRQn, 16); // Set Priority to 1 + NVIC_EnableIRQ(SysTick_IRQn); // Enable interrupt in NVIC +} + + + +void SysTick_Handler(void){ + SysTick_counter(); +} + +void SysTick_counter(){ + msTicks++; +} + + +void delay_ms (uint32_t mesc){ + uint32_t curTicks; + + curTicks = msTicks; + while ((msTicks - curTicks) < mesc); + + msTicks = 0; +} + +//void delay_ms(uint32_t msec){ +// uint32_t now=SysTick_val(); +// if (msec>5000) msec=5000; +// if (msec<1) msec=1; +// while ((now - SysTick_val()) < msec); +//} + + +void SysTick_reset(void) +{ + // SysTick Current Value Register + SysTick->VAL = 0; +} + +uint32_t SysTick_val(void) { + return SysTick->VAL; +} + +//void SysTick_counter(){ +// msTicks++; +// if(msTicks%1000 == 0) count++; +//} diff --git a/tutorial/tutorial-student/ecSysTick.h b/lab-tutorial/lib-student/ecSysTick.h similarity index 94% rename from tutorial/tutorial-student/ecSysTick.h rename to lab-tutorial/lib-student/ecSysTick.h index 596dc52..54de72a 100644 --- a/tutorial/tutorial-student/ecSysTick.h +++ b/lab-tutorial/lib-student/ecSysTick.h @@ -1,23 +1,23 @@ -#ifndef __EC_SYSTICK_H -#define __EC_SYSTICK_H - -#include "stm32f4xx.h" -#include "ecRCC.h" -#include - -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - -void SysTick_init(void); -void SysTick_Handler(void); -void SysTick_counter(); -void delay_ms(uint32_t msec); -void SysTick_reset(void); -uint32_t SysTick_val(void); - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - +#ifndef __EC_SYSTICK_H +#define __EC_SYSTICK_H + +#include "stm32f4xx.h" +#include "ecRCC.h" +#include + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + +void SysTick_init(void); +void SysTick_Handler(void); +void SysTick_counter(); +void delay_ms(uint32_t msec); +void SysTick_reset(void); +uint32_t SysTick_val(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + #endif \ No newline at end of file diff --git a/tutorial/tutorial-student/ecTIM_student.c b/lab-tutorial/lib-student/ecTIM_student.c similarity index 95% rename from tutorial/tutorial-student/ecTIM_student.c rename to lab-tutorial/lib-student/ecTIM_student.c index b4230ab..f3e176a 100644 --- a/tutorial/tutorial-student/ecTIM_student.c +++ b/lab-tutorial/lib-student/ecTIM_student.c @@ -1,100 +1,100 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: EC_HAL_for_student_exercise -* -****************************************************************************** -*/ - - -#include "ecTIM2.h" -#include "ecGPIO.h" - -/* Timer Configuration */ - -void TIM_init(TIM_TypeDef* timerx, uint32_t msec){ - -// 1. Enable Timer CLOCK - if(timerx ==TIM1) RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; - else if(timerx ==TIM2) RCC->APB1ENR |= __________________; - else if(timerx ==TIM3) __________________________________; - // repeat for TIM4, TIM5, TIM9, TIM11 - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - - -// 2. Set CNT period - TIM_period_ms(timerx,msec); - - -// 3. CNT Direction - timerx->CR1 _________________; // Upcounter - -// 4. Enable Timer Counter - timerx->CR1 |= TIM_CR1_CEN; -} - - -// Q. Which combination of PSC and ARR for msec unit? -// Q. What are the possible range (in sec ?) -void TIM_period_us(TIM_TypeDef *TIMx, uint32_t usec){ - // Period usec = 1 to 1000 - - // 1us(1MHz, ARR=1) to 65msec (ARR=0xFFFF) - uint32_t prescaler = _____; - uint16_t ARRval= (84/(prescaler)*usec); // 84MHz/1000000 us - - TIMx->PSC = ______________; - TIMx->ARR = ARRval-1; -} - - - -void TIM_period_ms(TIM_TypeDef* TIMx, uint32_t msec){ - // Period msec = 1 to 6000 - - // 0.1ms(10kHz, ARR=1) to 6.5sec (ARR=0xFFFF) - uint32_t prescaler = 8400; - uint16_t ARRval=______________; // 84MHz/1000ms - - TIMx->PSC = prescaler-1; - TIMx->ARR = ___________; -} - - -// Update Event Interrupt -void TIM_INT_init(TIM_TypeDef* timerx, uint32_t msec){ -// 1. Initialize Timer - TIM_init(timerx,msec); - -// 2. Enable Update Interrupt - uint32_t IRQn_reg =0; - if(timerx ==TIM1) IRQn_reg = TIM1_UP_TIM10_IRQn; - else if(timerx ==TIM2) IRQn_reg = ____________; - // repeat for TIM3, TIM4, TIM5, TIM9, TIM11 - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - - - NVIC_EnableIRQ(IRQn_reg); - NVIC_SetPriority(IRQn_reg,2); -} - - - -void TIM_INT_enable(TIM_TypeDef* timerx){ - timerx->DIER _____________________; // Enable Timer Update Interrupt -} - -void TIM_INT_disable(TIM_TypeDef* timerx){ - timerx->DIER &= ________________; // Disable Timer Update Interrupt -} - -uint32_t is_UIF(TIM_TypeDef *TIMx){ - return TIMx->SR & _____________; -} - -void clear_UIF(TIM_TypeDef *TIMx){ - TIMx->SR &= ~_________________; -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: EC_HAL_for_student_exercise +* +****************************************************************************** +*/ + + +#include "ecTIM2.h" +#include "ecGPIO.h" + +/* Timer Configuration */ + +void TIM_init(TIM_TypeDef* timerx, uint32_t msec){ + +// 1. Enable Timer CLOCK + if(timerx ==TIM1) RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; + else if(timerx ==TIM2) RCC->APB1ENR |= __________________; + else if(timerx ==TIM3) __________________________________; + // repeat for TIM4, TIM5, TIM9, TIM11 + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + + +// 2. Set CNT period + TIM_period_ms(timerx,msec); + + +// 3. CNT Direction + timerx->CR1 _________________; // Upcounter + +// 4. Enable Timer Counter + timerx->CR1 |= TIM_CR1_CEN; +} + + +// Q. Which combination of PSC and ARR for msec unit? +// Q. What are the possible range (in sec ?) +void TIM_period_us(TIM_TypeDef *TIMx, uint32_t usec){ + // Period usec = 1 to 1000 + + // 1us(1MHz, ARR=1) to 65msec (ARR=0xFFFF) + uint32_t prescaler = _____; + uint16_t ARRval= (84/(prescaler)*usec); // 84MHz/1000000 us + + TIMx->PSC = ______________; + TIMx->ARR = ARRval-1; +} + + + +void TIM_period_ms(TIM_TypeDef* TIMx, uint32_t msec){ + // Period msec = 1 to 6000 + + // 0.1ms(10kHz, ARR=1) to 6.5sec (ARR=0xFFFF) + uint32_t prescaler = 8400; + uint16_t ARRval=______________; // 84MHz/1000ms + + TIMx->PSC = prescaler-1; + TIMx->ARR = ___________; +} + + +// Update Event Interrupt +void TIM_INT_init(TIM_TypeDef* timerx, uint32_t msec){ +// 1. Initialize Timer + TIM_init(timerx,msec); + +// 2. Enable Update Interrupt + uint32_t IRQn_reg =0; + if(timerx ==TIM1) IRQn_reg = TIM1_UP_TIM10_IRQn; + else if(timerx ==TIM2) IRQn_reg = ____________; + // repeat for TIM3, TIM4, TIM5, TIM9, TIM11 + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + + + NVIC_EnableIRQ(IRQn_reg); + NVIC_SetPriority(IRQn_reg,2); +} + + + +void TIM_INT_enable(TIM_TypeDef* timerx){ + timerx->DIER _____________________; // Enable Timer Update Interrupt +} + +void TIM_INT_disable(TIM_TypeDef* timerx){ + timerx->DIER &= ________________; // Disable Timer Update Interrupt +} + +uint32_t is_UIF(TIM_TypeDef *TIMx){ + return TIMx->SR & _____________; +} + +void clear_UIF(TIM_TypeDef *TIMx){ + TIMx->SR &= ~_________________; +} diff --git a/tutorial/tutorial-student/ecTIM_student.h b/lab-tutorial/lib-student/ecTIM_student.h similarity index 95% rename from tutorial/tutorial-student/ecTIM_student.h rename to lab-tutorial/lib-student/ecTIM_student.h index 7715e0b..9f3b6e0 100644 --- a/tutorial/tutorial-student/ecTIM_student.h +++ b/lab-tutorial/lib-student/ecTIM_student.h @@ -1,36 +1,36 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: EC_HAL_for_student_exercise -* -****************************************************************************** -*/ - -#ifndef __EC_TIM_H -#define __EC_TIM_H -#include "stm32f411xe.h" - -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - - -/* Timer Configuration */ -void TIM_init(TIM_TypeDef *timerx, uint32_t msec); -void TIM_period_us(TIM_TypeDef* timx, uint32_t usec); -void TIM_period_ms(TIM_TypeDef* timx, uint32_t msec); - -void TIM_INT_init(TIM_TypeDef* timerx, uint32_t msec); -void TIM_INT_enable(TIM_TypeDef* timx); -void TIM_INT_disable(TIM_TypeDef* timx); - -uint32_t is_UIF(TIM_TypeDef *TIMx); -void clear_UIF(TIM_TypeDef *TIMx); - - -#ifdef __cplusplus -} -#endif /* __cplusplus */ - -#endif +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: EC_HAL_for_student_exercise +* +****************************************************************************** +*/ + +#ifndef __EC_TIM_H +#define __EC_TIM_H +#include "stm32f411xe.h" + +#ifdef __cplusplus + extern "C" { +#endif /* __cplusplus */ + + +/* Timer Configuration */ +void TIM_init(TIM_TypeDef *timerx, uint32_t msec); +void TIM_period_us(TIM_TypeDef* timx, uint32_t usec); +void TIM_period_ms(TIM_TypeDef* timx, uint32_t msec); + +void TIM_INT_init(TIM_TypeDef* timerx, uint32_t msec); +void TIM_INT_enable(TIM_TypeDef* timx); +void TIM_INT_disable(TIM_TypeDef* timx); + +uint32_t is_UIF(TIM_TypeDef *TIMx); +void clear_UIF(TIM_TypeDef *TIMx); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif diff --git a/tutorial/tutorial-student/ecUART_student.c b/lab-tutorial/lib-student/ecUART_student.c similarity index 97% rename from tutorial/tutorial-student/ecUART_student.c rename to lab-tutorial/lib-student/ecUART_student.c index b1ae16c..7477a89 100644 --- a/tutorial/tutorial-student/ecUART_student.c +++ b/lab-tutorial/lib-student/ecUART_student.c @@ -1,265 +1,265 @@ -#include "ecUART_student.h" -#include - - - - -// ********************** DO NOT MODIFY HERE *************************** -// -// Implement a dummy __FILE struct, which is called with the FILE structure. -//#ifndef __stdio_h -struct __FILE { - //int dummy; - int handle; - -}; - -FILE __stdout; -FILE __stdin; -//#endif - -// Retarget printf() to USART2 -int fputc(int ch, FILE *f) { - uint8_t c; - c = ch & 0x00FF; - USART_write(USART2, (uint8_t *)&c, 1); - return(ch); -} - -// Retarget getchar()/scanf() to USART2 -int fgetc(FILE *f) { - uint8_t rxByte; - rxByte = USART_getc(USART2); - return rxByte; -} - -void UART2_init(){ - // Enable the clock of USART2 - RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // Enable USART 2 clock (APB1 clock: AHB clock / 2 = 42MHz) - - // Enable the peripheral clock of GPIO Port - RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; - - // ********************** USART 2 *************************** - // PA2 = USART2_TX - // PA3 = USART2_RX - // Alternate function(AF7), High Speed, Push pull, Pull up - // ********************************************************** - int TX_pin = 2; - - GPIOA->MODER &= ~(0xF << (2*TX_pin)); // Clear bits - GPIOA->MODER |= 0xA << (2*TX_pin); // Alternate Function(10) - GPIOA->AFR[0] |= 0x77<< (4*TX_pin); // AF7 - USART2 - GPIOA->OSPEEDR |= 0xF<<(2*TX_pin); // High speed (11) - GPIOA->PUPDR &= ~(0xF<<(2*TX_pin)); - GPIOA->PUPDR |= 0x5<<(2*TX_pin); // Pull-up (01) - GPIOA->OTYPER &= ~(0x3<CR1 &= ~USART_CR1_UE; // Disable USART - - // Configure word length to 8 bit - USARTx->CR1 &= ~USART_CR1_M; // M: 0 = 8 data bits, 1 start bit - USARTx->CR1 &= ~USART_CR1_PCE; // No parrity bit - USARTx->CR2 &= ~USART_CR2_STOP; // 1 stop bit - - // Configure oversampling mode (to reduce RF noise) - USARTx->CR1 &= ~USART_CR1_OVER8; // 0 = oversampling by 16 - - // CSet Baudrate to 9600 using APB frequency (42MHz) - // If oversampling by 16, Tx/Rx baud = f_CK / (16*USARTDIV), - // If oversampling by 8, Tx/Rx baud = f_CK / (8*USARTDIV) - // USARTDIV = 42MHz/(16*9600) = 237.4375 - - //USARTx->BRR = 42000000/ baud_rate; - float Hz = 42000000; - - float USARTDIV = (float)Hz/16/9600; - uint32_t MNT = (uint32_t)USARTDIV; - uint32_t FRC = round((USARTDIV - MNT) * 16); - if (FRC > 15) { - MNT += 1; - FRC = 0; - } - USARTx->BRR |= (MNT << 4) | FRC; - - USARTx->CR1 |= (USART_CR1_RE | USART_CR1_TE); // Transmitter and Receiver enable - USARTx->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR; - USARTx->CR1 |= USART_CR1_UE; // USART enable - - USARTx->CR1 |= USART_CR1_RXNEIE; // Enable Read Interrupt - NVIC_SetPriority(USART2_IRQn, 1); // Set Priority to 1 - NVIC_EnableIRQ(USART2_IRQn); // Enable interrupt of USART2 peripheral - -} - -void USART_write(USART_TypeDef * USARTx, uint8_t *buffer, uint32_t nBytes) { - // TXE is set by hardware when the content of the TDR - // register has been transferred into the shift register. - int i; - for (i = 0; i < nBytes; i++) { - // wait until TXE (TX empty) bit is set - while (!(USARTx->SR & USART_SR_TXE)); - // Writing USART_DR automatically clears the TXE flag - USARTx->DR = buffer[i] & 0xFF; - USART_delay(300); - } - // wait until TC bit is set - while (!(USARTx->SR & USART_SR_TC)); - // TC bit clear - USARTx->SR &= ~USART_SR_TC; - -} - -void USART_delay(uint32_t us) { - uint32_t time = 100*us/7; - while(--time); -} - -// ********************************************************** - - - - -// ********************** EXERCISE*************************** -// -void USART_begin(USART_TypeDef* USARTx, GPIO_TypeDef* GPIO_TX, int pinTX, GPIO_TypeDef* GPIO_RX, int pinRX, int baud){ -//1. GPIO Pin for TX and RX - // Enable GPIO peripheral clock - // Alternative Function mode selection for Pin_y in GPIOx - GPIO_init(GPIO_TX,pinTX,AF); // GPIO mode setting : AF - GPIO_init(GPIO_RX,pinRX,AF); // GPIO mode setting : AF - - // Set Alternative Function Register for USARTx. - // AF7 - USART1,2 AF8 - USART6 - if (USARTx==USART6){ - // USART_TX GPIO AFR - if(pinTX<8) GPIO_TX->AFR[0] |= 8<< (4*pinTX); - else ________________________________________; - // USART_RX GPIO AFR - if(pinRX<8) _______________________________________; - else _______________________________________; - } - else{ //USART1,USART2 - // USART_TX GPIO AFR - if(pinTX<8) _______________________________________; - else _______________________________________; - // USART_RX GPIO AFR - if(pinRX<8) _______________________________________; - else _______________________________________; - } - // No pull up, No pull down - GPIO_pupdr(GPIO_TX, pinTX, EC_NONE); - GPIO_pupdr(GPIO_RX, pinRX, EC_NONE); - - -//2. USARTx (x=2,1,6) configuration - // Enable USART peripheral clock - if (USARTx==USART1) - _______________________________________; // Enable USART 1 clock (APB2 clock: AHB clock = 84MHz) - else if(USARTx==USART2) - RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // Enable USART 2 clock (APB1 clock: AHB clock/2 = 42MHz) - else - _______________________________________; // Enable USART 6 clock (APB2 clock: AHB clock = 84MHz) - - // Disable USARTx. - USARTx->CR1 &= ~USART_CR1_UE; // USART disable - - // No Parity / 8-bit word length / Oversampling x16 - USARTx->CR1 _______________________________________; // No parrity bit - USARTx->CR1 _______________________________________; // M: 0 = 8 data bits, 1 start bit - USARTx->CR1 _______________________________________; // 0 = oversampling by 16 (to reduce RF noise) - // Configure Stop bit - USARTx->CR2 &= ~USART_CR2_STOP; // 1 stop bit - - // CSet Baudrate to 9600 using APB frequency (42MHz) - // If oversampling by 16, Tx/Rx baud = f_CK / (16*USARTDIV), - // If oversampling by 8, Tx/Rx baud = f_CK / (8*USARTDIV) - // USARTDIV = 42MHz/(16*9600) = 237.4375 - - // Configure Baud-rate - float Hz=84000000; // if(USARTx==USART1 || USARTx==USART6) - if(USARTx==USART2) Hz=42000000; - - - float USARTDIV = _______________________________________; - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - // YOUR CODE GOES HERE - USARTx->BRR |= _______________________________________; - - // Enable TX, RX, and USARTx - USARTx->CR1 _______________________________________; // Transmitter and Receiver enable - USARTx->CR1 _______________________________________; // USART enable - - -// 3. Read USARTx Data (Interrupt) - // Set the priority and enable interrupt - USARTx->CR1 _______________________________________; // Received Data Ready to be Read Interrupt - if (USARTx==USART1){ - _______________________________________; // Set Priority to 1 - _______________________________________; // Enable interrupt of USART2 peripheral - } - else if (USARTx==USART2){ - NVIC_SetPriority(USART2_IRQn, 1); // Set Priority to 1 - NVIC_EnableIRQ(USART2_IRQn); // Enable interrupt of USART2 peripheral - } - else { // if(USARTx==USART6) - NVIC_SetPriority(USART6_IRQn, 1); // Set Priority to 1 - NVIC_EnableIRQ(USART6_IRQn); // Enable interrupt of USART2 peripheral - } - USARTx->CR1 _______________________________________; // USART enable -} - - -void USART_init(USART_TypeDef* USARTx, int baud){ -// ********************************************************** -// Default Tx,Rx GPIO, pin configuration -// PA_2 = USART2_TX, PA_3 = USART2_RX -// PB_6 = USART1_TX (default), PB_3 = USART1_RX (default) -// PA_11 = USART6_TX (default), PA_12 = USART6_RX (default) -// ********************************************************** - -// 1. GPIO Pin for TX and RX - GPIO_TypeDef* GPIO_TX; - GPIO_TypeDef* GPIO_RX; - int pinTX = 0, pinRX =0; - - if (USARTx==USART1) { - GPIO_TX = GPIOB; - GPIO_RX = GPIOB; - pinTX = 6; - pinRX = 3; - } - if (USARTx==USART2) { - GPIO_TX = GPIOA; - GPIO_RX = GPIOA; - pinTX = 2; - pinRX = 3; - } - if (USARTx==USART6) { - GPIO_TX = GPIOA; - GPIO_RX = GPIOA; - pinTX = 11; - pinRX = 12; - } - // if for other USART input? - - // USART_begin() - USART_begin(USARTx, GPIO_TX, pinTX, GPIO_RX, pinRX, baud); -} - - -uint8_t USART_getc(USART_TypeDef * USARTx){ - // Wait until RXNE (RX not empty) bit is set by HW -->Read to read - while (_______________________________________); - // Reading USART_DR automatically clears the RXNE flag - return ((uint8_t)(USARTx->DR & 0xFF)); -} - -uint32_t is_USART_RXNE(USART_TypeDef * USARTx){ - return (USARTx->SR & USART_SR_RXNE); -} - +#include "ecUART_student.h" +#include + + + + +// ********************** DO NOT MODIFY HERE *************************** +// +// Implement a dummy __FILE struct, which is called with the FILE structure. +//#ifndef __stdio_h +struct __FILE { + //int dummy; + int handle; + +}; + +FILE __stdout; +FILE __stdin; +//#endif + +// Retarget printf() to USART2 +int fputc(int ch, FILE *f) { + uint8_t c; + c = ch & 0x00FF; + USART_write(USART2, (uint8_t *)&c, 1); + return(ch); +} + +// Retarget getchar()/scanf() to USART2 +int fgetc(FILE *f) { + uint8_t rxByte; + rxByte = USART_getc(USART2); + return rxByte; +} + +void UART2_init(){ + // Enable the clock of USART2 + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // Enable USART 2 clock (APB1 clock: AHB clock / 2 = 42MHz) + + // Enable the peripheral clock of GPIO Port + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; + + // ********************** USART 2 *************************** + // PA2 = USART2_TX + // PA3 = USART2_RX + // Alternate function(AF7), High Speed, Push pull, Pull up + // ********************************************************** + int TX_pin = 2; + + GPIOA->MODER &= ~(0xF << (2*TX_pin)); // Clear bits + GPIOA->MODER |= 0xA << (2*TX_pin); // Alternate Function(10) + GPIOA->AFR[0] |= 0x77<< (4*TX_pin); // AF7 - USART2 + GPIOA->OSPEEDR |= 0xF<<(2*TX_pin); // High speed (11) + GPIOA->PUPDR &= ~(0xF<<(2*TX_pin)); + GPIOA->PUPDR |= 0x5<<(2*TX_pin); // Pull-up (01) + GPIOA->OTYPER &= ~(0x3<CR1 &= ~USART_CR1_UE; // Disable USART + + // Configure word length to 8 bit + USARTx->CR1 &= ~USART_CR1_M; // M: 0 = 8 data bits, 1 start bit + USARTx->CR1 &= ~USART_CR1_PCE; // No parrity bit + USARTx->CR2 &= ~USART_CR2_STOP; // 1 stop bit + + // Configure oversampling mode (to reduce RF noise) + USARTx->CR1 &= ~USART_CR1_OVER8; // 0 = oversampling by 16 + + // CSet Baudrate to 9600 using APB frequency (42MHz) + // If oversampling by 16, Tx/Rx baud = f_CK / (16*USARTDIV), + // If oversampling by 8, Tx/Rx baud = f_CK / (8*USARTDIV) + // USARTDIV = 42MHz/(16*9600) = 237.4375 + + //USARTx->BRR = 42000000/ baud_rate; + float Hz = 42000000; + + float USARTDIV = (float)Hz/16/9600; + uint32_t MNT = (uint32_t)USARTDIV; + uint32_t FRC = round((USARTDIV - MNT) * 16); + if (FRC > 15) { + MNT += 1; + FRC = 0; + } + USARTx->BRR |= (MNT << 4) | FRC; + + USARTx->CR1 |= (USART_CR1_RE | USART_CR1_TE); // Transmitter and Receiver enable + USARTx->CR3 |= USART_CR3_DMAT | USART_CR3_DMAR; + USARTx->CR1 |= USART_CR1_UE; // USART enable + + USARTx->CR1 |= USART_CR1_RXNEIE; // Enable Read Interrupt + NVIC_SetPriority(USART2_IRQn, 1); // Set Priority to 1 + NVIC_EnableIRQ(USART2_IRQn); // Enable interrupt of USART2 peripheral + +} + +void USART_write(USART_TypeDef * USARTx, uint8_t *buffer, uint32_t nBytes) { + // TXE is set by hardware when the content of the TDR + // register has been transferred into the shift register. + int i; + for (i = 0; i < nBytes; i++) { + // wait until TXE (TX empty) bit is set + while (!(USARTx->SR & USART_SR_TXE)); + // Writing USART_DR automatically clears the TXE flag + USARTx->DR = buffer[i] & 0xFF; + USART_delay(300); + } + // wait until TC bit is set + while (!(USARTx->SR & USART_SR_TC)); + // TC bit clear + USARTx->SR &= ~USART_SR_TC; + +} + +void USART_delay(uint32_t us) { + uint32_t time = 100*us/7; + while(--time); +} + +// ********************************************************** + + + + +// ********************** EXERCISE*************************** +// +void USART_begin(USART_TypeDef* USARTx, GPIO_TypeDef* GPIO_TX, int pinTX, GPIO_TypeDef* GPIO_RX, int pinRX, int baud){ +//1. GPIO Pin for TX and RX + // Enable GPIO peripheral clock + // Alternative Function mode selection for Pin_y in GPIOx + GPIO_init(GPIO_TX,pinTX,AF); // GPIO mode setting : AF + GPIO_init(GPIO_RX,pinRX,AF); // GPIO mode setting : AF + + // Set Alternative Function Register for USARTx. + // AF7 - USART1,2 AF8 - USART6 + if (USARTx==USART6){ + // USART_TX GPIO AFR + if(pinTX<8) GPIO_TX->AFR[0] |= 8<< (4*pinTX); + else ________________________________________; + // USART_RX GPIO AFR + if(pinRX<8) _______________________________________; + else _______________________________________; + } + else{ //USART1,USART2 + // USART_TX GPIO AFR + if(pinTX<8) _______________________________________; + else _______________________________________; + // USART_RX GPIO AFR + if(pinRX<8) _______________________________________; + else _______________________________________; + } + // No pull up, No pull down + GPIO_pupdr(GPIO_TX, pinTX, EC_NONE); + GPIO_pupdr(GPIO_RX, pinRX, EC_NONE); + + +//2. USARTx (x=2,1,6) configuration + // Enable USART peripheral clock + if (USARTx==USART1) + _______________________________________; // Enable USART 1 clock (APB2 clock: AHB clock = 84MHz) + else if(USARTx==USART2) + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // Enable USART 2 clock (APB1 clock: AHB clock/2 = 42MHz) + else + _______________________________________; // Enable USART 6 clock (APB2 clock: AHB clock = 84MHz) + + // Disable USARTx. + USARTx->CR1 &= ~USART_CR1_UE; // USART disable + + // No Parity / 8-bit word length / Oversampling x16 + USARTx->CR1 _______________________________________; // No parrity bit + USARTx->CR1 _______________________________________; // M: 0 = 8 data bits, 1 start bit + USARTx->CR1 _______________________________________; // 0 = oversampling by 16 (to reduce RF noise) + // Configure Stop bit + USARTx->CR2 &= ~USART_CR2_STOP; // 1 stop bit + + // CSet Baudrate to 9600 using APB frequency (42MHz) + // If oversampling by 16, Tx/Rx baud = f_CK / (16*USARTDIV), + // If oversampling by 8, Tx/Rx baud = f_CK / (8*USARTDIV) + // USARTDIV = 42MHz/(16*9600) = 237.4375 + + // Configure Baud-rate + float Hz=84000000; // if(USARTx==USART1 || USARTx==USART6) + if(USARTx==USART2) Hz=42000000; + + + float USARTDIV = _______________________________________; + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + // YOUR CODE GOES HERE + USARTx->BRR |= _______________________________________; + + // Enable TX, RX, and USARTx + USARTx->CR1 _______________________________________; // Transmitter and Receiver enable + USARTx->CR1 _______________________________________; // USART enable + + +// 3. Read USARTx Data (Interrupt) + // Set the priority and enable interrupt + USARTx->CR1 _______________________________________; // Received Data Ready to be Read Interrupt + if (USARTx==USART1){ + _______________________________________; // Set Priority to 1 + _______________________________________; // Enable interrupt of USART2 peripheral + } + else if (USARTx==USART2){ + NVIC_SetPriority(USART2_IRQn, 1); // Set Priority to 1 + NVIC_EnableIRQ(USART2_IRQn); // Enable interrupt of USART2 peripheral + } + else { // if(USARTx==USART6) + NVIC_SetPriority(USART6_IRQn, 1); // Set Priority to 1 + NVIC_EnableIRQ(USART6_IRQn); // Enable interrupt of USART2 peripheral + } + USARTx->CR1 _______________________________________; // USART enable +} + + +void USART_init(USART_TypeDef* USARTx, int baud){ +// ********************************************************** +// Default Tx,Rx GPIO, pin configuration +// PA_2 = USART2_TX, PA_3 = USART2_RX +// PB_6 = USART1_TX (default), PB_3 = USART1_RX (default) +// PA_11 = USART6_TX (default), PA_12 = USART6_RX (default) +// ********************************************************** + +// 1. GPIO Pin for TX and RX + GPIO_TypeDef* GPIO_TX; + GPIO_TypeDef* GPIO_RX; + int pinTX = 0, pinRX =0; + + if (USARTx==USART1) { + GPIO_TX = GPIOB; + GPIO_RX = GPIOB; + pinTX = 6; + pinRX = 3; + } + if (USARTx==USART2) { + GPIO_TX = GPIOA; + GPIO_RX = GPIOA; + pinTX = 2; + pinRX = 3; + } + if (USARTx==USART6) { + GPIO_TX = GPIOA; + GPIO_RX = GPIOA; + pinTX = 11; + pinRX = 12; + } + // if for other USART input? + + // USART_begin() + USART_begin(USARTx, GPIO_TX, pinTX, GPIO_RX, pinRX, baud); +} + + +uint8_t USART_getc(USART_TypeDef * USARTx){ + // Wait until RXNE (RX not empty) bit is set by HW -->Read to read + while (_______________________________________); + // Reading USART_DR automatically clears the RXNE flag + return ((uint8_t)(USARTx->DR & 0xFF)); +} + +uint32_t is_USART_RXNE(USART_TypeDef * USARTx){ + return (USARTx->SR & USART_SR_RXNE); +} + diff --git a/tutorial/tutorial-student/ecUART_student.h b/lab-tutorial/lib-student/ecUART_student.h similarity index 96% rename from tutorial/tutorial-student/ecUART_student.h rename to lab-tutorial/lib-student/ecUART_student.h index 39e41d1..555cc7b 100644 --- a/tutorial/tutorial-student/ecUART_student.h +++ b/lab-tutorial/lib-student/ecUART_student.h @@ -1,53 +1,53 @@ -#ifndef __EC_USART_H -#define __EC_USART_H - -#include "stm32f411xe.h" -#include -#include "ecGPIO.h" -#include "ecRCC.h" - -#define POL 0 -#define INT 1 - -// You can modify this -#define BAUD_9600 0 -#define BAUD_19200 1 -#define BAUD_57600 2 -#define BAUD_115200 3 -#define BAUD_921600 4 - - -// ********************** USART 2 (USB) *************************** - // PA_2 = USART2_TX - // PA_3 = USART2_RX - // Alternate function(AF7), High Speed, Push pull, Pull up - // APB1 -// ********************************************************** - -// ********************** USART 1 *************************** - // PB_6 = USART1_TX (default) // PA_9 (option) - // PB_3 = USART1_RX (default) // PA_10 (option) - // APB2 -// ********************************************************** - -// ********************** USART 6 *************************** - // PA_11 = USART6_TX (default) // PC_6 (option) - // PA_12 = USART6_RX (default) // PC_7 (option) - // APB2 -// ********************************************************** - - -/* Given to Students */ -void UART2_init(); -void USART_write(USART_TypeDef* USARTx, uint8_t* buffer, uint32_t nBytes); -void USART_delay(uint32_t us); - -/* Exercise*/ -void USART_begin(USART_TypeDef* USARTx, GPIO_TypeDef* GPIO_TX, int pinTX, GPIO_TypeDef* GPIO_RX, int pinRX, int baud); -void USART_init(USART_TypeDef* USARTx, int baud); // default pins. -uint8_t USART_getc(USART_TypeDef * USARTx); -uint32_t is_USART_RXNE(USART_TypeDef * USARTx); - - - +#ifndef __EC_USART_H +#define __EC_USART_H + +#include "stm32f411xe.h" +#include +#include "ecGPIO.h" +#include "ecRCC.h" + +#define POL 0 +#define INT 1 + +// You can modify this +#define BAUD_9600 0 +#define BAUD_19200 1 +#define BAUD_57600 2 +#define BAUD_115200 3 +#define BAUD_921600 4 + + +// ********************** USART 2 (USB) *************************** + // PA_2 = USART2_TX + // PA_3 = USART2_RX + // Alternate function(AF7), High Speed, Push pull, Pull up + // APB1 +// ********************************************************** + +// ********************** USART 1 *************************** + // PB_6 = USART1_TX (default) // PA_9 (option) + // PB_3 = USART1_RX (default) // PA_10 (option) + // APB2 +// ********************************************************** + +// ********************** USART 6 *************************** + // PA_11 = USART6_TX (default) // PC_6 (option) + // PA_12 = USART6_RX (default) // PC_7 (option) + // APB2 +// ********************************************************** + + +/* Given to Students */ +void UART2_init(); +void USART_write(USART_TypeDef* USARTx, uint8_t* buffer, uint32_t nBytes); +void USART_delay(uint32_t us); + +/* Exercise*/ +void USART_begin(USART_TypeDef* USARTx, GPIO_TypeDef* GPIO_TX, int pinTX, GPIO_TypeDef* GPIO_RX, int pinRX, int baud); +void USART_init(USART_TypeDef* USARTx, int baud); // default pins. +uint8_t USART_getc(USART_TypeDef * USARTx); +uint32_t is_USART_RXNE(USART_TypeDef * USARTx); + + + #endif \ No newline at end of file diff --git a/tutorial/exercise/EC_bitwise_exercise.c b/lab-tutorial/tutorial-c-program/EC_bitwise_exercise.c similarity index 100% rename from tutorial/exercise/EC_bitwise_exercise.c rename to lab-tutorial/tutorial-c-program/EC_bitwise_exercise.c diff --git a/tutorial/exercise/EC_final_exercise.c b/lab-tutorial/tutorial-c-program/EC_final_exercise.c similarity index 100% rename from tutorial/exercise/EC_final_exercise.c rename to lab-tutorial/tutorial-c-program/EC_final_exercise.c diff --git a/tutorial/exercise/EC_pointer_exercise_2.c b/lab-tutorial/tutorial-c-program/EC_pointer_exercise_2.c similarity index 100% rename from tutorial/exercise/EC_pointer_exercise_2.c rename to lab-tutorial/tutorial-c-program/EC_pointer_exercise_2.c diff --git a/tutorial/exercise/EC_string_exercise.c b/lab-tutorial/tutorial-c-program/EC_string_exercise.c similarity index 100% rename from tutorial/exercise/EC_string_exercise.c rename to lab-tutorial/tutorial-c-program/EC_string_exercise.c diff --git a/tutorial/exercise/EC_structure_exercise.c b/lab-tutorial/tutorial-c-program/EC_structure_exercise.c similarity index 100% rename from tutorial/exercise/EC_structure_exercise.c rename to lab-tutorial/tutorial-c-program/EC_structure_exercise.c diff --git a/tutorial/exercise/readme.md b/lab-tutorial/tutorial-c-program/readme.md similarity index 100% rename from tutorial/exercise/readme.md rename to lab-tutorial/tutorial-c-program/readme.md diff --git a/tutorial/exercise/solution/readme b/lab-tutorial/tutorial-c-program/solution/readme similarity index 100% rename from tutorial/exercise/solution/readme rename to lab-tutorial/tutorial-c-program/solution/readme diff --git a/tutorial/tutorial-student/EC_PLX_DAQ_demo.c b/lab-tutorial/tutorial-student/EC_PLX_DAQ_demo.c similarity index 95% rename from tutorial/tutorial-student/EC_PLX_DAQ_demo.c rename to lab-tutorial/tutorial-student/EC_PLX_DAQ_demo.c index 3a2e3a5..b39911a 100644 --- a/tutorial/tutorial-student/EC_PLX_DAQ_demo.c +++ b/lab-tutorial/tutorial-student/EC_PLX_DAQ_demo.c @@ -1,81 +1,81 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: USART communication -* -****************************************************************************** -*/ - -#include "stm32f4xx.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecUART.h" -#include "ecSysTick.h" -#include "String.h" - - -// USART2 : MCU to PC via usb - -uint8_t buf1[4]; -uint8_t buf2[4]; -uint8_t buf3[4]; -uint8_t buf4[4]; - - - -uint32_t reflect=0, sound=0, lux=0, dist = 0; -int flag = 0; -int i =0; - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - printf("Hello Nucleo\r\n"); - - //USART1 excel_DAQ initialize - USART_write(USART2,(unsigned char*) "CLEARSHEET\r\n",12); - USART_write(USART2,(unsigned char*) "LABEL,Date,Time,Timer,Sound,Light,Dist\r\n",40); - - // Inifinite Loop ---------------------------------------------------------- - while (1){ - - lux = GPIO_read(GPIOA,0); - sound++; - dist++; - - sprintf(buf1, "%d", sound); - sprintf(buf2, "%d", lux); - sprintf(buf3, "%d", dist); - - //USART1 Trasnmit sensor value to server - - USART_write(USART2,(unsigned char*) "DATA,DATE,TIME,TIMER,",21); // transmit char to USART6 - USART_write(USART2,&buf1,4); - USART_write(USART2,(unsigned char*) ",",1); // transmit char to USART6 - USART_write(USART2,&buf2,4); - USART_write(USART2,(unsigned char*) ",",1); // transmit char to USART6 - USART_write(USART2,&buf3,4); - USART_write(USART2,(unsigned char*) ",AUTOSCROLL_20\r\n",16); // transmit char to USART6 - - - delay_ms(1000); - } -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); - SysTick_init(); - - // USART congfiguration - USART_init(USART2, 9600); - - // GPIO configuration - LED_init(); - GPIO_init(GPIOA,0,INPUT); -} - +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: USART communication +* +****************************************************************************** +*/ + +#include "stm32f4xx.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecUART.h" +#include "ecSysTick.h" +#include "String.h" + + +// USART2 : MCU to PC via usb + +uint8_t buf1[4]; +uint8_t buf2[4]; +uint8_t buf3[4]; +uint8_t buf4[4]; + + + +uint32_t reflect=0, sound=0, lux=0, dist = 0; +int flag = 0; +int i =0; + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + printf("Hello Nucleo\r\n"); + + //USART1 excel_DAQ initialize + USART_write(USART2,(unsigned char*) "CLEARSHEET\r\n",12); + USART_write(USART2,(unsigned char*) "LABEL,Date,Time,Timer,Sound,Light,Dist\r\n",40); + + // Inifinite Loop ---------------------------------------------------------- + while (1){ + + lux = GPIO_read(GPIOA,0); + sound++; + dist++; + + sprintf(buf1, "%d", sound); + sprintf(buf2, "%d", lux); + sprintf(buf3, "%d", dist); + + //USART1 Trasnmit sensor value to server + + USART_write(USART2,(unsigned char*) "DATA,DATE,TIME,TIMER,",21); // transmit char to USART6 + USART_write(USART2,&buf1,4); + USART_write(USART2,(unsigned char*) ",",1); // transmit char to USART6 + USART_write(USART2,&buf2,4); + USART_write(USART2,(unsigned char*) ",",1); // transmit char to USART6 + USART_write(USART2,&buf3,4); + USART_write(USART2,(unsigned char*) ",AUTOSCROLL_20\r\n",16); // transmit char to USART6 + + + delay_ms(1000); + } +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); + SysTick_init(); + + // USART congfiguration + USART_init(USART2, 9600); + + // GPIO configuration + LED_init(); + GPIO_init(GPIOA,0,INPUT); +} + diff --git a/tutorial/tutorial-student/EC_zigbee_demo.c b/lab-tutorial/tutorial-student/EC_zigbee_demo.c similarity index 95% rename from tutorial/tutorial-student/EC_zigbee_demo.c rename to lab-tutorial/tutorial-student/EC_zigbee_demo.c index d682cd9..e85a5bd 100644 --- a/tutorial/tutorial-student/EC_zigbee_demo.c +++ b/lab-tutorial/tutorial-student/EC_zigbee_demo.c @@ -1,99 +1,99 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: USART communication -* -****************************************************************************** -*/ - -#include "stm32f4xx.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecUART.h" -#include "ecSysTick.h" -#include "String.h" - -// USART2 : MCU to PC via usb -// USART1 : MCU to MCU2 via zigbee - -uint8_t mcu2Data = 0; -uint8_t pcData = 0; -int indx =0; -int maxBuf=10; -uint8_t buffer[100]={0,}; -int bReceive=0; -int ledOn = 0; -int endChar = 13; - -int i =0; - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - printf("Hello Nucleo\r\n"); - - // Inifinite Loop ---------------------------------------------------------- - while (1){ - - if(bReceive==1 && buffer[0]=='L'){ - printf("buffer : %s\r\n",buffer); - if (buffer[1]=='0') ledOn = 0; - else if (buffer[1]=='1') ledOn = 1; - else printf("ERROR : Wrong command\r\n"); - bReceive = 0; - } - - GPIO_write(GPIOA,5,ledOn); - delay_ms(1000); - } -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); - SysTick_init(); - - // USART congfiguration - USART_init(USART2, 9600); - USART_begin(USART1, GPIOA,9,GPIOA,10, 9600); // PA9 - RXD , PA10 - TXD - - // GPIO configuration - LED_init(); -} - - -void USART1_IRQHandler(){ //USART1 INT - if(is_USART_RXNE(USART1)){ - mcu2Data = USART_getc(USART1); - //printf("%c",pcData); // echo to sender(pc) - if(mcu2Data==endChar) { - bReceive=1; - indx = 0; - } - else{ - if(indx>maxBuf){ - indx =0; - memset(buffer, 0, sizeof(char) * maxBuf); - printf("ERROR : Too long string\r\n"); - } - buffer[indx] = mcu2Data; - indx++; - } - } -} - -void USART2_IRQHandler(){ //USART2 INT - if(is_USART_RXNE(USART2)){ - pcData = USART_getc(USART2); - USART_write(USART1,&pcData,1); // transmit char to USART1 - printf("%c",pcData); // echo to sender(pc) - - if(pcData==endChar){ - printf("\r\n"); // to change line on PC display - } - } -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: USART communication +* +****************************************************************************** +*/ + +#include "stm32f4xx.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecUART.h" +#include "ecSysTick.h" +#include "String.h" + +// USART2 : MCU to PC via usb +// USART1 : MCU to MCU2 via zigbee + +uint8_t mcu2Data = 0; +uint8_t pcData = 0; +int indx =0; +int maxBuf=10; +uint8_t buffer[100]={0,}; +int bReceive=0; +int ledOn = 0; +int endChar = 13; + +int i =0; + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + printf("Hello Nucleo\r\n"); + + // Inifinite Loop ---------------------------------------------------------- + while (1){ + + if(bReceive==1 && buffer[0]=='L'){ + printf("buffer : %s\r\n",buffer); + if (buffer[1]=='0') ledOn = 0; + else if (buffer[1]=='1') ledOn = 1; + else printf("ERROR : Wrong command\r\n"); + bReceive = 0; + } + + GPIO_write(GPIOA,5,ledOn); + delay_ms(1000); + } +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); + SysTick_init(); + + // USART congfiguration + USART_init(USART2, 9600); + USART_begin(USART1, GPIOA,9,GPIOA,10, 9600); // PA9 - RXD , PA10 - TXD + + // GPIO configuration + LED_init(); +} + + +void USART1_IRQHandler(){ //USART1 INT + if(is_USART_RXNE(USART1)){ + mcu2Data = USART_getc(USART1); + //printf("%c",pcData); // echo to sender(pc) + if(mcu2Data==endChar) { + bReceive=1; + indx = 0; + } + else{ + if(indx>maxBuf){ + indx =0; + memset(buffer, 0, sizeof(char) * maxBuf); + printf("ERROR : Too long string\r\n"); + } + buffer[indx] = mcu2Data; + indx++; + } + } +} + +void USART2_IRQHandler(){ //USART2 INT + if(is_USART_RXNE(USART2)){ + pcData = USART_getc(USART2); + USART_write(USART1,&pcData,1); // transmit char to USART1 + printf("%c",pcData); // echo to sender(pc) + + if(pcData==endChar){ + printf("\r\n"); // to change line on PC display + } + } +} diff --git a/tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c b/lab-tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c similarity index 97% rename from tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c rename to lab-tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c index 48ec62d..46b1022 100644 --- a/tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c +++ b/lab-tutorial/tutorial-student/LAB_TIMER_Inputcap_UltraSonic_student.c @@ -1,80 +1,80 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ - - -#include "stm32f411xe.h" -#include "math.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" -#include "ecPWM.h" -#include "ecUART_student.h" -#include "ecSysTIck.h" - -uint32_t ovf_cnt = 0; -float distance = 0; -float timeInterval = 0; -float timeSt = 0; -float timeEnd= 0; - -void setup(void); - -int main(void){ - - setup(); - - - while(1){ - distance = (float) timeInterval/58*10; // Ultrasonic speed[m/s] * echo pulse duration[us] - printf("%f [mm]\r\n",distance); - delay_ms(500); - } -} - -void TIM2_IRQHandler(void){ - if(is_UIF(TIM2)){ // Update interrupt - ________________ // overflow count - clear_UIF(TIM2); // clear update interrupt flag - } - if(is_CCIF(TIM2,3)){ // TIM2_Ch3 (IC3) Capture Flag. Rising Edge Detect - timeSt = ________________; // Capture TimeStart from CC3 - clear_CCIF(TIM2,3); // clear capture/compare interrupt flag - } - else if(________________ ){ // TIM2_Ch3 (IC4) Capture Flag. Falling Edge Detect - timeEnd = ________________; // Capture TimeEnd from CC4 - timeInterval = ________________; // Total time of echo pulse - ovf_cnt = 0; // overflow reset - clear_CCIF(TIM2,4); // clear capture/compare interrupt flag - } -} - -void setup(){ - - RCC_PLL_init(); - SysTick_init(); - UART2_init(); - -// PWM configuration --------------------------------------------------------------------- - PWM_t trig; // PWM1 for trig - ________________ // PWM init as PA_6: Ultrasonic trig pulse - PWM_period_us(&trig,50000); // PWM of 50ms period. Use period_us() - PWM_pulsewidth_us(&trig,10); // PWM pulse width of 10us - -// Input Capture configuration ----------------------------------------------------------------------- - IC_t echo; // Input Capture for echo - ________________ // ICAP init as PB10 as input caputre - ICAP_counter_us(&echo, 10); // ICAP counter step time as 10us - ICAP_setup(&echo, 3, RISE); // TIM2_CH3 as IC3 , rising edge detect - ________________ // TIM2_CH3 as IC4 , falling edge detect - -// Enable TIMx interrupt ----------------------------------------------------------------------- - ________________ // TIM2 Interrupt Enable - +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ + + +#include "stm32f411xe.h" +#include "math.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" +#include "ecPWM.h" +#include "ecUART_student.h" +#include "ecSysTIck.h" + +uint32_t ovf_cnt = 0; +float distance = 0; +float timeInterval = 0; +float timeSt = 0; +float timeEnd= 0; + +void setup(void); + +int main(void){ + + setup(); + + + while(1){ + distance = (float) timeInterval/58*10; // Ultrasonic speed[m/s] * echo pulse duration[us] + printf("%f [mm]\r\n",distance); + delay_ms(500); + } +} + +void TIM2_IRQHandler(void){ + if(is_UIF(TIM2)){ // Update interrupt + ________________ // overflow count + clear_UIF(TIM2); // clear update interrupt flag + } + if(is_CCIF(TIM2,3)){ // TIM2_Ch3 (IC3) Capture Flag. Rising Edge Detect + timeSt = ________________; // Capture TimeStart from CC3 + clear_CCIF(TIM2,3); // clear capture/compare interrupt flag + } + else if(________________ ){ // TIM2_Ch3 (IC4) Capture Flag. Falling Edge Detect + timeEnd = ________________; // Capture TimeEnd from CC4 + timeInterval = ________________; // Total time of echo pulse + ovf_cnt = 0; // overflow reset + clear_CCIF(TIM2,4); // clear capture/compare interrupt flag + } +} + +void setup(){ + + RCC_PLL_init(); + SysTick_init(); + UART2_init(); + +// PWM configuration --------------------------------------------------------------------- + PWM_t trig; // PWM1 for trig + ________________ // PWM init as PA_6: Ultrasonic trig pulse + PWM_period_us(&trig,50000); // PWM of 50ms period. Use period_us() + PWM_pulsewidth_us(&trig,10); // PWM pulse width of 10us + +// Input Capture configuration ----------------------------------------------------------------------- + IC_t echo; // Input Capture for echo + ________________ // ICAP init as PB10 as input caputre + ICAP_counter_us(&echo, 10); // ICAP counter step time as 10us + ICAP_setup(&echo, 3, RISE); // TIM2_CH3 as IC3 , rising edge detect + ________________ // TIM2_CH3 as IC4 , falling edge detect + +// Enable TIMx interrupt ----------------------------------------------------------------------- + ________________ // TIM2 Interrupt Enable + } \ No newline at end of file diff --git a/tutorial/tutorial-student/LAB_USART_LED.c b/lab-tutorial/tutorial-student/LAB_USART_LED.c similarity index 95% rename from tutorial/tutorial-student/LAB_USART_LED.c rename to lab-tutorial/tutorial-student/LAB_USART_LED.c index f300656..d727fa2 100644 --- a/tutorial/tutorial-student/LAB_USART_LED.c +++ b/lab-tutorial/tutorial-student/LAB_USART_LED.c @@ -1,95 +1,95 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: USART communication -* -****************************************************************************** -*/ - -#include "stm32f4xx.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecUART.h" -#include "ecSysTick.h" -#include "String.h" - -// USART2 : MCU to PC via usb -// USART1 : MCU to MCU2 - -uint8_t mcu2Data = 0; -uint8_t pcData = 0; -int indx =0; -int maxBuf=10; -uint8_t buffer[100]={0,}; -int bReceive=0; -int ledOn = 0; -int endChar = 13; - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - printf("Hello Nucleo\r\n"); - - // Inifinite Loop ---------------------------------------------------------- - while (1){ - - if(bReceive==1 && buffer[0]=='L'){ - printf("buffer : %s\r\n",buffer); - if (buffer[1]=='0') ledOn = 0; - else if (buffer[1]=='1') ledOn = 1; - else printf("ERROR : Wrong command\r\n"); - bReceive = 0; - } - - GPIO_write(GPIOA,5,ledOn); - delay_ms(500); - } -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); - SysTick_init(); - - // USART congfiguration - USART_init(USART2, 38400); - USART_begin(USART1, GPIOA,9,GPIOA,10, 9600); // PA9 - RXD , PA10 - TXD - - // GPIO configuration - LED_init(); -} - -void USART1_IRQHandler(){ //USART1 INT - if(is_USART_RXNE(USART1)){ - mcu2Data = USART_getc(USART1); - if(mcu2Data==endChar) { - bReceive=1; - indx = 0; - } - else{ - if(indx>maxBuf){ - indx =0; - memset(buffer, 0, sizeof(char) * maxBuf); - printf("ERROR : Too long string\r\n"); - } - buffer[indx] = mcu2Data; - indx++; - } - } -} - -void USART2_IRQHandler(){ //USART2 INT - if(is_USART_RXNE(USART2)){ - pcData = USART_getc(USART2); - USART_write(USART1,&pcData,1); // transmit char to USART1 - printf("%c",pcData); // echo to sender(pc) - - if(pcData==endChar){ - printf("\r\n"); // to change line on PC display - } - } -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: USART communication +* +****************************************************************************** +*/ + +#include "stm32f4xx.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecUART.h" +#include "ecSysTick.h" +#include "String.h" + +// USART2 : MCU to PC via usb +// USART1 : MCU to MCU2 + +uint8_t mcu2Data = 0; +uint8_t pcData = 0; +int indx =0; +int maxBuf=10; +uint8_t buffer[100]={0,}; +int bReceive=0; +int ledOn = 0; +int endChar = 13; + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + printf("Hello Nucleo\r\n"); + + // Inifinite Loop ---------------------------------------------------------- + while (1){ + + if(bReceive==1 && buffer[0]=='L'){ + printf("buffer : %s\r\n",buffer); + if (buffer[1]=='0') ledOn = 0; + else if (buffer[1]=='1') ledOn = 1; + else printf("ERROR : Wrong command\r\n"); + bReceive = 0; + } + + GPIO_write(GPIOA,5,ledOn); + delay_ms(500); + } +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); + SysTick_init(); + + // USART congfiguration + USART_init(USART2, 38400); + USART_begin(USART1, GPIOA,9,GPIOA,10, 9600); // PA9 - RXD , PA10 - TXD + + // GPIO configuration + LED_init(); +} + +void USART1_IRQHandler(){ //USART1 INT + if(is_USART_RXNE(USART1)){ + mcu2Data = USART_getc(USART1); + if(mcu2Data==endChar) { + bReceive=1; + indx = 0; + } + else{ + if(indx>maxBuf){ + indx =0; + memset(buffer, 0, sizeof(char) * maxBuf); + printf("ERROR : Too long string\r\n"); + } + buffer[indx] = mcu2Data; + indx++; + } + } +} + +void USART2_IRQHandler(){ //USART2 INT + if(is_USART_RXNE(USART2)){ + pcData = USART_getc(USART2); + USART_write(USART1,&pcData,1); // transmit char to USART1 + printf("%c",pcData); // echo to sender(pc) + + if(pcData==endChar){ + printf("\r\n"); // to change line on PC display + } + } +} diff --git a/tutorial/tutorial-student/TU_ADC_HAL_student.c b/lab-tutorial/tutorial-student/TU_ADC_HAL_student.c similarity index 95% rename from tutorial/tutorial-student/TU_ADC_HAL_student.c rename to lab-tutorial/tutorial-student/TU_ADC_HAL_student.c index 6501026..9c81e1e 100644 --- a/tutorial/tutorial-student/TU_ADC_HAL_student.c +++ b/lab-tutorial/tutorial-student/TU_ADC_HAL_student.c @@ -1,48 +1,48 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" -#include "ecSysTick.h" -#include "ecUART_student.h" -#include "ecADC_student.h" - -//IR parameter// -float result_v =0; - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - - - // Inifinite Loop ---------------------------------------------------------- - while(1); -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - UART2_init(); - ADC_init(GPIOA,1,SW); - ADC_start(ADC1); -} - - -void ADC_IRQHandler(void){ - if(is_ADC_EOC(ADC1)){ //after finishing sequence - result_v =ADC_read(); - printf("voltage = %.3f\r\n",result_v*3.3/4095); - - } -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" +#include "ecSysTick.h" +#include "ecUART_student.h" +#include "ecADC_student.h" + +//IR parameter// +float result_v =0; + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + + + // Inifinite Loop ---------------------------------------------------------- + while(1); +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + UART2_init(); + ADC_init(GPIOA,1,SW); + ADC_start(ADC1); +} + + +void ADC_IRQHandler(void){ + if(is_ADC_EOC(ADC1)){ //after finishing sequence + result_v =ADC_read(); + printf("voltage = %.3f\r\n",result_v*3.3/4095); + + } +} diff --git a/tutorial/tutorial-student/TU_ADC_Interrupt_student.c b/lab-tutorial/tutorial-student/TU_ADC_Interrupt_student.c similarity index 96% rename from tutorial/tutorial-student/TU_ADC_Interrupt_student.c rename to lab-tutorial/tutorial-student/TU_ADC_Interrupt_student.c index 894eacd..8ea9a76 100644 --- a/tutorial/tutorial-student/TU_ADC_Interrupt_student.c +++ b/lab-tutorial/tutorial-student/TU_ADC_Interrupt_student.c @@ -1,96 +1,96 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" -#include "ecSysTick.h" -#include "ecUART_student.h" -float result_v =0; -void setup(void); - -int main(void) { - setup(); -// GPIO configuration --------------------------------------------------------------------- -// 1. Initialize GPIO port and pin as ANALOG, no pull up / pull down - GPIO_init(GPIOA, 1, ANALOG); // ANALOG = 3 - GPIO_pupdr(GPIOA, 1, EC_NONE); // EC_NONE = 0 - -// ADC configuration --------------------------------------------------------------------- -// 1. Total time of conversion setting - // Enable ADC pheripheral clock - RCC->APB2ENR |= // Enable the clock of RCC_APB2ENR_ADC1EN - - // Configure ADC clock pre-scaler - ADC->CCR &= // 0000: PCLK2 divided by 2 (42MHz) - - // Configure ADC resolution - ADC1->CR1 &= // 00: 12-bit resolution (15cycle+) - - // Configure channel sampling time of conversion. - // Software is allowed to write these bits only when ADSTART=0 and JADSTART=0 !! - // ADC clock cycles @42MHz = 2us - ADC1->SMPR2 &= // Sampling Time: clear bits - ADC1->SMPR2 |= // Sampling Time: 84cycles/42Mhz)=2us - -// 2. Regular / Injection Group - //Regular: SQRx, Injection: JSQx - -// 3. Repetition: Single or Continuous conversion - ADC1->CR2 |= // Enable Continuous conversion mode - -// 4. Single Channel or Scan mode - // - Single Channel: scan mode, right alignment - ADC1->CR1 |= // 1: Scan mode enable - ADC1->CR2 &= // 0: Right alignment - - // Configure the sequence length - ADC1->SQR1 &= // 0000: 1 conversion in the regular channel conversion sequence - - // Configure the channel sequence - ADC1->SQR3 &= // Choose CH of 1st Conversion: clear bits - ADC1->SQR3 |= // Choose CH of 1st Conversion: Select CH (0~15) - -// 5. Interrupt Enable - // Enable EOC(conversion) interrupt. - ADC1->CR1 &= // Interrupt reset - ADC1->CR1 |= // Interrupt enable - - // Enable ADC_IRQn - NVIC_SetPriority(); // Set Priority to 2 - NVIC_EnableIRQ(); // Enable interrupt form ACD1 peripheral - -// 6. Enable ADC - ADC1->CR2 |= ADC_CR2_ADON; - -// ADC SW configuration --------------------------------------------------------------------- - ADC1->CR2 |= ADC_CR2_SWSTART; - - /* Infinite Loop -----------------------------------------------------------------------*/ - while(1); -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - UART2_init(); - GPIO_init(GPIOA, 1, EC_ANG); // GPIOA pin1 enable - GPIO_pupdr(GPIOA, 1, EC_NONE); - SysTick_init(); -} - -void ADC_IRQHandler(void){ - if(is_ADC_EOC()){ - result_v = ADC_read(); - printf("voltage = %.3f\r\n",result_v*3.3/4095); - } -} - +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" +#include "ecSysTick.h" +#include "ecUART_student.h" +float result_v =0; +void setup(void); + +int main(void) { + setup(); +// GPIO configuration --------------------------------------------------------------------- +// 1. Initialize GPIO port and pin as ANALOG, no pull up / pull down + GPIO_init(GPIOA, 1, ANALOG); // ANALOG = 3 + GPIO_pupdr(GPIOA, 1, EC_NONE); // EC_NONE = 0 + +// ADC configuration --------------------------------------------------------------------- +// 1. Total time of conversion setting + // Enable ADC pheripheral clock + RCC->APB2ENR |= // Enable the clock of RCC_APB2ENR_ADC1EN + + // Configure ADC clock pre-scaler + ADC->CCR &= // 0000: PCLK2 divided by 2 (42MHz) + + // Configure ADC resolution + ADC1->CR1 &= // 00: 12-bit resolution (15cycle+) + + // Configure channel sampling time of conversion. + // Software is allowed to write these bits only when ADSTART=0 and JADSTART=0 !! + // ADC clock cycles @42MHz = 2us + ADC1->SMPR2 &= // Sampling Time: clear bits + ADC1->SMPR2 |= // Sampling Time: 84cycles/42Mhz)=2us + +// 2. Regular / Injection Group + //Regular: SQRx, Injection: JSQx + +// 3. Repetition: Single or Continuous conversion + ADC1->CR2 |= // Enable Continuous conversion mode + +// 4. Single Channel or Scan mode + // - Single Channel: scan mode, right alignment + ADC1->CR1 |= // 1: Scan mode enable + ADC1->CR2 &= // 0: Right alignment + + // Configure the sequence length + ADC1->SQR1 &= // 0000: 1 conversion in the regular channel conversion sequence + + // Configure the channel sequence + ADC1->SQR3 &= // Choose CH of 1st Conversion: clear bits + ADC1->SQR3 |= // Choose CH of 1st Conversion: Select CH (0~15) + +// 5. Interrupt Enable + // Enable EOC(conversion) interrupt. + ADC1->CR1 &= // Interrupt reset + ADC1->CR1 |= // Interrupt enable + + // Enable ADC_IRQn + NVIC_SetPriority(); // Set Priority to 2 + NVIC_EnableIRQ(); // Enable interrupt form ACD1 peripheral + +// 6. Enable ADC + ADC1->CR2 |= ADC_CR2_ADON; + +// ADC SW configuration --------------------------------------------------------------------- + ADC1->CR2 |= ADC_CR2_SWSTART; + + /* Infinite Loop -----------------------------------------------------------------------*/ + while(1); +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + UART2_init(); + GPIO_init(GPIOA, 1, EC_ANG); // GPIOA pin1 enable + GPIO_pupdr(GPIOA, 1, EC_NONE); + SysTick_init(); +} + +void ADC_IRQHandler(void){ + if(is_ADC_EOC()){ + result_v = ADC_read(); + printf("voltage = %.3f\r\n",result_v*3.3/4095); + } +} + diff --git a/tutorial/tutorial-student/TU_ADC_TRGO_student.c b/lab-tutorial/tutorial-student/TU_ADC_TRGO_student.c similarity index 96% rename from tutorial/tutorial-student/TU_ADC_TRGO_student.c rename to lab-tutorial/tutorial-student/TU_ADC_TRGO_student.c index 3d58833..49b8fbf 100644 --- a/tutorial/tutorial-student/TU_ADC_TRGO_student.c +++ b/lab-tutorial/tutorial-student/TU_ADC_TRGO_student.c @@ -1,94 +1,94 @@ - - -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" -#include "ecSysTick.h" -#include "ecUART_student.h" -float result_v =0; -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - - /* ADC_initiation --------------------------------------------------------------------*/ - // 1) Enable the clock of ADC - // 2) ... - // 3) ... - -// HW Trigger configuration ------------------------------------------------------------- -// 1. TIMx Trigger Output Config - // Enable TIMx Clock - TIM_TypeDef *timx = TIM3; // Default TRGO : TIM3 - int msec = 1; // Default msec : 1 msec - TIM_init(timx, msec); // TIM3 init (use user defined HAL) - timx->CR1 &= ~1; // counter disable - - // Set PSC, ARR - TIM_period_ms(timx, msec); - - // Master Mode Selection MMS[2:0]: Trigger output (TRGO) - timx->CR2 &= // reset MMS - timx->CR2 |= //100: Compare - OC1REF signal is used as trigger output (TRGO) - - // Output Compare Mode - timx->CCMR1 &= // OC1M : output compare 1 Mode - timx->CCMR1 |= // OC1M = 110 for compare 1 Mode ch1 - - // OC1 signal - timx->CCER |= // CC1E Capture enabled - timx->CCR1 = // CCR set - - // Enable TIMx - timx->CR1 |= 1; //counter enable - -// 2. HW Trigger - // Select Trigger Source - ADC1->CR2 &= // reset EXTSEL - ADC1->CR2 |= // TIMx TRGO event (ADC : TIM2, TIM3 TRGO) default : TIM3 - - //Select Trigger Polarity - ADC1->CR2 &= // reset EXTEN, default - ADC1->CR2 |= // trigger detection rising edge - - // Enable ADC - ADC1->CR2 |= ADC_CR2_ADON; - - // ADC Start (SW, once) - ADC1->CR2 |= ADC_CR2_SWSTART; - - /* Inifinite Loop --------------------------------------------------------------------*/ - while(1); -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - UART2_init(); - GPIO_init(GPIOA, 1, EC_ANG); // calls RCC_GPIOA_enable() - GPIO_pupdr(GPIOA, 1, EC_NONE); -} - -void ADC_IRQHandler(void){ - if((ADC1->SR & ADC_SR_OVR) == ADC_SR_OVR){ - ADC1->SR &= ~ADC_SR_OVR; - } - - if(is_ADC_EOC()){ - result_v = ADC_read(); - printf("voltage = %.3f\r\n",result_v*3.3/4095); - - } + + +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" +#include "ecSysTick.h" +#include "ecUART_student.h" +float result_v =0; +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + + /* ADC_initiation --------------------------------------------------------------------*/ + // 1) Enable the clock of ADC + // 2) ... + // 3) ... + +// HW Trigger configuration ------------------------------------------------------------- +// 1. TIMx Trigger Output Config + // Enable TIMx Clock + TIM_TypeDef *timx = TIM3; // Default TRGO : TIM3 + int msec = 1; // Default msec : 1 msec + TIM_init(timx, msec); // TIM3 init (use user defined HAL) + timx->CR1 &= ~1; // counter disable + + // Set PSC, ARR + TIM_period_ms(timx, msec); + + // Master Mode Selection MMS[2:0]: Trigger output (TRGO) + timx->CR2 &= // reset MMS + timx->CR2 |= //100: Compare - OC1REF signal is used as trigger output (TRGO) + + // Output Compare Mode + timx->CCMR1 &= // OC1M : output compare 1 Mode + timx->CCMR1 |= // OC1M = 110 for compare 1 Mode ch1 + + // OC1 signal + timx->CCER |= // CC1E Capture enabled + timx->CCR1 = // CCR set + + // Enable TIMx + timx->CR1 |= 1; //counter enable + +// 2. HW Trigger + // Select Trigger Source + ADC1->CR2 &= // reset EXTSEL + ADC1->CR2 |= // TIMx TRGO event (ADC : TIM2, TIM3 TRGO) default : TIM3 + + //Select Trigger Polarity + ADC1->CR2 &= // reset EXTEN, default + ADC1->CR2 |= // trigger detection rising edge + + // Enable ADC + ADC1->CR2 |= ADC_CR2_ADON; + + // ADC Start (SW, once) + ADC1->CR2 |= ADC_CR2_SWSTART; + + /* Inifinite Loop --------------------------------------------------------------------*/ + while(1); +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + UART2_init(); + GPIO_init(GPIOA, 1, EC_ANG); // calls RCC_GPIOA_enable() + GPIO_pupdr(GPIOA, 1, EC_NONE); +} + +void ADC_IRQHandler(void){ + if((ADC1->SR & ADC_SR_OVR) == ADC_SR_OVR){ + ADC1->SR &= ~ADC_SR_OVR; + } + + if(is_ADC_EOC()){ + result_v = ADC_read(); + printf("voltage = %.3f\r\n",result_v*3.3/4095); + + } } \ No newline at end of file diff --git a/tutorial/tutorial-student/TU_EXTI_student.c b/lab-tutorial/tutorial-student/TU_EXTI_student.c similarity index 95% rename from tutorial/tutorial-student/TU_EXTI_student.c rename to lab-tutorial/tutorial-student/TU_EXTI_student.c index 5f10627..52622a4 100644 --- a/tutorial/tutorial-student/TU_EXTI_student.c +++ b/lab-tutorial/tutorial-student/TU_EXTI_student.c @@ -1,70 +1,70 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ - - - - -#include "ecRCC.h" -#include "ecGPIO.h" - -#define LED_PIN 5 -#define BUTTON_PIN 13 - -void setup(void); - -int main(void) { - - // System CLOCK, GPIO Initialiization ---------------------------------------- - setup(); - - - // EXTI Initialiization ------------------------------------------------------ - - // SYSCFG peripheral clock enable - RCC->APB2ENR |= __________________ - - // Connect External Line to the GPIO - // Button: PC_13 -> EXTICR3(EXTI13) - SYSCFG->EXTICR[____] &= ~SYSCFG_EXTICR4_EXTI13; - SYSCFG->EXTICR[____] |= ______________________; - - // Falling trigger enable (Button: pull-up) - EXTI->FTSR |= 1UL << __________; - - // Unmask (Enable) EXT interrupt - EXTI->IMR |= 1UL << ___________; - - // Interrupt IRQn, Priority - NVIC_SetPriority(EXTI15_10_IRQn, 0); // Set EXTI priority as 0 - NVIC_EnableIRQ(EXTI15_10_IRQn); // Enable EXTI - - - while (1); -} - - -void EXTI15_10_IRQHandler(void) { - if ((EXTI->PR & EXTI_PR_PR13) == _________) { - LED_toggle(); - EXTI->PR |= EXTI_PR_PR13; // cleared by writing '1' - } -} - - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - // Initialize GPIOA_5 for Output - GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() - // Initialize GPIOC_13 for Input Button - GPIO_init(GPIOC, BUTTON_PIN, INPUT); // calls RCC_GPIOC_enable() -} - +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ + + + + +#include "ecRCC.h" +#include "ecGPIO.h" + +#define LED_PIN 5 +#define BUTTON_PIN 13 + +void setup(void); + +int main(void) { + + // System CLOCK, GPIO Initialiization ---------------------------------------- + setup(); + + + // EXTI Initialiization ------------------------------------------------------ + + // SYSCFG peripheral clock enable + RCC->APB2ENR |= __________________ + + // Connect External Line to the GPIO + // Button: PC_13 -> EXTICR3(EXTI13) + SYSCFG->EXTICR[____] &= ~SYSCFG_EXTICR4_EXTI13; + SYSCFG->EXTICR[____] |= ______________________; + + // Falling trigger enable (Button: pull-up) + EXTI->FTSR |= 1UL << __________; + + // Unmask (Enable) EXT interrupt + EXTI->IMR |= 1UL << ___________; + + // Interrupt IRQn, Priority + NVIC_SetPriority(EXTI15_10_IRQn, 0); // Set EXTI priority as 0 + NVIC_EnableIRQ(EXTI15_10_IRQn); // Enable EXTI + + + while (1); +} + + +void EXTI15_10_IRQHandler(void) { + if ((EXTI->PR & EXTI_PR_PR13) == _________) { + LED_toggle(); + EXTI->PR |= EXTI_PR_PR13; // cleared by writing '1' + } +} + + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + // Initialize GPIOA_5 for Output + GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() + // Initialize GPIOC_13 for Input Button + GPIO_init(GPIOC, BUTTON_PIN, INPUT); // calls RCC_GPIOC_enable() +} + diff --git a/tutorial/tutorial-student/TU_SysTick_student.c b/lab-tutorial/tutorial-student/TU_SysTick_student.c similarity index 95% rename from tutorial/tutorial-student/TU_SysTick_student.c rename to lab-tutorial/tutorial-student/TU_SysTick_student.c index a8c48d7..c8af7a8 100644 --- a/tutorial/tutorial-student/TU_SysTick_student.c +++ b/lab-tutorial/tutorial-student/TU_SysTick_student.c @@ -1,66 +1,66 @@ -#include "stm32f411xe.h" -#include "ecRCC.h" -#include "ecGPIO.h" - -#define MCU_CLK_PLL 84000000 -#define MCU_CLK_HSI 16000000 - -volatile uint32_t msTick = 0; -volatile uint32_t curTicks; - -void setup(void); - -int main(void) { - -// System CLOCK, GPIO Initialiization ---------------------------------------- - setup(); - -// SysTick Initialiization ------------------------------------------------------ - // SysTick Control and Status Register - SysTick->CTRL = 0; // Disable SysTick IRQ and SysTick Counter - - // Select processor clock - // 1 = processor clock; 0 = external clock - SysTick->CTRL |= __________ - - // uint32_t MCU_CLK=EC_SYSTEM_CLK - // SysTick Reload Value Register - SysTick->LOAD =__________ // 1ms - - // Clear SysTick Current Value - SysTick->VAL = 0; - - // Enables SysTick exception request - // 1 = counting down to zero asserts the SysTick exception request - SysTick->CTRL |= __________ - - // Enable SysTick IRQ and SysTick Timer - SysTick->CTRL |= __________ - - NVIC_SetPriority(SysTick_IRQn, 16); // Set Priority to 1 - NVIC_EnableIRQ(SysTick_IRQn); // Enable interrupt in NVIC - - -// While loop ------------------------------------------------------ - msTick = 0; - - while(1){ - curTicks = msTicks; - while ((msTicks - curTicks) < 1000); - msTicks = 0; - LED_toggle(); - } -} - - -void SysTick_Handler(void){ - msTicks++; -} - -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() -} - - +#include "stm32f411xe.h" +#include "ecRCC.h" +#include "ecGPIO.h" + +#define MCU_CLK_PLL 84000000 +#define MCU_CLK_HSI 16000000 + +volatile uint32_t msTick = 0; +volatile uint32_t curTicks; + +void setup(void); + +int main(void) { + +// System CLOCK, GPIO Initialiization ---------------------------------------- + setup(); + +// SysTick Initialiization ------------------------------------------------------ + // SysTick Control and Status Register + SysTick->CTRL = 0; // Disable SysTick IRQ and SysTick Counter + + // Select processor clock + // 1 = processor clock; 0 = external clock + SysTick->CTRL |= __________ + + // uint32_t MCU_CLK=EC_SYSTEM_CLK + // SysTick Reload Value Register + SysTick->LOAD =__________ // 1ms + + // Clear SysTick Current Value + SysTick->VAL = 0; + + // Enables SysTick exception request + // 1 = counting down to zero asserts the SysTick exception request + SysTick->CTRL |= __________ + + // Enable SysTick IRQ and SysTick Timer + SysTick->CTRL |= __________ + + NVIC_SetPriority(SysTick_IRQn, 16); // Set Priority to 1 + NVIC_EnableIRQ(SysTick_IRQn); // Enable interrupt in NVIC + + +// While loop ------------------------------------------------------ + msTick = 0; + + while(1){ + curTicks = msTicks; + while ((msTicks - curTicks) < 1000); + msTicks = 0; + LED_toggle(); + } +} + + +void SysTick_Handler(void){ + msTicks++; +} + +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() +} + + diff --git a/tutorial/tutorial-student/TU_TIMER_Interrupt_student.c b/lab-tutorial/tutorial-student/TU_TIMER_Interrupt_student.c similarity index 96% rename from tutorial/tutorial-student/TU_TIMER_Interrupt_student.c rename to lab-tutorial/tutorial-student/TU_TIMER_Interrupt_student.c index aafec60..6e71794 100644 --- a/tutorial/tutorial-student/TU_TIMER_Interrupt_student.c +++ b/lab-tutorial/tutorial-student/TU_TIMER_Interrupt_student.c @@ -1,53 +1,53 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" - -uint32_t _count=0; - -#define LED_PIN 5 - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - setup(); - TIM_TypeDef* timerx; - timerx = TIM2; - RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; - - timerx->PSC = // Timer counter clock: 1MHz(1us) - timerx->ARR = // Set auto reload register to maximum (count up to 65535) - timerx->DIER |= // Enable Interrupt - timerx->CR1 |= // Enable counter - - NVIC_EnableIRQ(); // TIM2_IRQHandler Enable - NVIC_SetPriority( ,); // TIM2_IRQHandler Set priority as 2 - - // Inifinite Loop ---------------------------------------------------------- - while(1){} -} - -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() -} - -void TIM2_IRQHandler(void){ - if((TIM2->SR & TIM_SR_UIF) == ){ // update interrupt flag - //Create the code to toggle LED by 1000ms - TIM2->SR &= // clear by writing 0 - } - -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" + +uint32_t _count=0; + +#define LED_PIN 5 + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + setup(); + TIM_TypeDef* timerx; + timerx = TIM2; + RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; + + timerx->PSC = // Timer counter clock: 1MHz(1us) + timerx->ARR = // Set auto reload register to maximum (count up to 65535) + timerx->DIER |= // Enable Interrupt + timerx->CR1 |= // Enable counter + + NVIC_EnableIRQ(); // TIM2_IRQHandler Enable + NVIC_SetPriority( ,); // TIM2_IRQHandler Set priority as 2 + + // Inifinite Loop ---------------------------------------------------------- + while(1){} +} + +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + GPIO_init(GPIOA, LED_PIN, OUTPUT); // calls RCC_GPIOA_enable() +} + +void TIM2_IRQHandler(void){ + if((TIM2->SR & TIM_SR_UIF) == ){ // update interrupt flag + //Create the code to toggle LED by 1000ms + TIM2->SR &= // clear by writing 0 + } + +} diff --git a/tutorial/tutorial-student/TU_TIMER_PWM_student.c b/lab-tutorial/tutorial-student/TU_TIMER_PWM_student.c similarity index 97% rename from tutorial/tutorial-student/TU_TIMER_PWM_student.c rename to lab-tutorial/tutorial-student/TU_TIMER_PWM_student.c index 190aca1..fbe1ffc 100644 --- a/tutorial/tutorial-student/TU_TIMER_PWM_student.c +++ b/lab-tutorial/tutorial-student/TU_TIMER_PWM_student.c @@ -1,68 +1,68 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ -#include "stm32f411xe.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecPWM.h" -#include "ecSysTick.h" -#include "ecEXTI.h" - -#define LED_PIN 5 - -void setup(void); - -int main(void) { - // Initialiization -------------------------------------------------------- - RCC_PLL_init(); // System Clock = 84MHz - SysTick_init(); // for delay_ms() - GPIO_init(GPIOA, LED_PIN, EC_ALTE); // GPIOA 5 ALTERNATE function - GPIO_ospeed(GPIOA, LED_PIN, EC_HIGH); // GPIOA 5 HIGH SPEED - - // TEMP: TIMER Register Initialiization -------------------------------------------------------- - TIM_TypeDef *TIMx; - TIMx = TIM2; - - // GPIO: ALTERNATIVE function setting - GPIOA->AFR[0] = // AF1 at PA5 = TIM2_CH1 (p.150) - - // TIMER: PWM setting - RCC->APB1ENR |= // Enable TIMER clock - - TIMx->CR1 &= // Direction Up-count - - TIMx->PSC = // f_cnt = 10kHz - - TIMx->ARR = // Auto-reload: Upcounting (0...ARR). - - TIMx->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear ouput compare mode bits for channel 1 - TIMx->CCMR1 |= // OC1M = 110 for PWM Mode 1 output on ch1 - TIMx->CCMR1 |= TIM_CCMR1_OC1PE; // Output 1 preload enable (make CCR1 value changable) - - TIMx->CCER &= ~TIM_CCER_CC1P; // select output polarity: active high - TIMx->CCER |= // Enable output for ch1 - TIMx->CCR1 = // Output Compare Register for channel 1 - - TIMx->CR1 |= TIM_CR1_CEN; // Enable counter - - - // Inifinite Loop ---------------------------------------------------------- - while(1){ - //Create the code to change the brightness of LED as 10kHZ (use "delay(100)") - } - } -// Initialiization -void setup(void) -{ - RCC_PLL_init(); // System Clock = 84MHz - SysTick_init(); // for delay_ms() -} - - - +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ +#include "stm32f411xe.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecPWM.h" +#include "ecSysTick.h" +#include "ecEXTI.h" + +#define LED_PIN 5 + +void setup(void); + +int main(void) { + // Initialiization -------------------------------------------------------- + RCC_PLL_init(); // System Clock = 84MHz + SysTick_init(); // for delay_ms() + GPIO_init(GPIOA, LED_PIN, EC_ALTE); // GPIOA 5 ALTERNATE function + GPIO_ospeed(GPIOA, LED_PIN, EC_HIGH); // GPIOA 5 HIGH SPEED + + // TEMP: TIMER Register Initialiization -------------------------------------------------------- + TIM_TypeDef *TIMx; + TIMx = TIM2; + + // GPIO: ALTERNATIVE function setting + GPIOA->AFR[0] = // AF1 at PA5 = TIM2_CH1 (p.150) + + // TIMER: PWM setting + RCC->APB1ENR |= // Enable TIMER clock + + TIMx->CR1 &= // Direction Up-count + + TIMx->PSC = // f_cnt = 10kHz + + TIMx->ARR = // Auto-reload: Upcounting (0...ARR). + + TIMx->CCMR1 &= ~TIM_CCMR1_OC1M; // Clear ouput compare mode bits for channel 1 + TIMx->CCMR1 |= // OC1M = 110 for PWM Mode 1 output on ch1 + TIMx->CCMR1 |= TIM_CCMR1_OC1PE; // Output 1 preload enable (make CCR1 value changable) + + TIMx->CCER &= ~TIM_CCER_CC1P; // select output polarity: active high + TIMx->CCER |= // Enable output for ch1 + TIMx->CCR1 = // Output Compare Register for channel 1 + + TIMx->CR1 |= TIM_CR1_CEN; // Enable counter + + + // Inifinite Loop ---------------------------------------------------------- + while(1){ + //Create the code to change the brightness of LED as 10kHZ (use "delay(100)") + } + } +// Initialiization +void setup(void) +{ + RCC_PLL_init(); // System Clock = 84MHz + SysTick_init(); // for delay_ms() +} + + + diff --git a/tutorial/tutorial-student/TU_TIMER_inputcap_student.c b/lab-tutorial/tutorial-student/TU_TIMER_inputcap_student.c similarity index 97% rename from tutorial/tutorial-student/TU_TIMER_inputcap_student.c rename to lab-tutorial/tutorial-student/TU_TIMER_inputcap_student.c index 2a0764e..1372c25 100644 --- a/tutorial/tutorial-student/TU_TIMER_inputcap_student.c +++ b/lab-tutorial/tutorial-student/TU_TIMER_inputcap_student.c @@ -1,66 +1,66 @@ -/** -****************************************************************************** -* @author SSSLAB -* @Mod 2021-8-12 by YKKIM -* @brief Embedded Controller: Tutorial ___ -* - _________________________________ -* -****************************************************************************** -*/ - - -#include "stm32f411xe.h" -#include "math.h" -#include "ecGPIO.h" -#include "ecRCC.h" -#include "ecTIM.h" -#include "ecUART_student.h" - -uint32_t ovf_cnt = 0; -uint32_t ccr2 = 0; -float period = 0; - -int main(void){ - - RCC_PLL_init(); - UART2_init(); - - // GPIO configuration --------------------------------------------------------------------- - GPIO_init(GPIOA, 5, AF); // PB10: Alternate Function Mode - // TIM configuration ----------------------------------------------------------------------- - GPIOA->AFR[] |= //AF1(TIM2) - RCC->APB1ENR |= //TIM2 Clock enabled - - TIM2->PSC = // Timer counter clock: 1M`Hz(1us) - TIM2->ARR = // Set auto reload register to maximum (count up to 65535) - TIM2->CR1 & // CR1 DIR enabled - TIM2->CCMR1 |= // Capture/Compare Selection: CC1 is mapped on TI1 - TIM2->CCMR1 &= // Clear IC1F - TIM2->CCMR1 |= // Set filter N=4 - TIM2->CCER &= // Clear CCER - TIM2->CCER &= // Capture rising edge - TIM2->CCER |= // Capture enabled - TIM2->DIER |= // Interrupt enabled - TIM2->DIER = // Update interrupt enable - TIM2->CR1 = // Counter enable - - - NVIC_SetPriority(, 2); // Set the priority of TIM2 interrupt request - NVIC_EnableIRQ(); // TIM2 interrupt request enable - - while(1); -} - -void TIM2_IRQHandler(void){ - if(TIM2->SR & TIM_SR_UIF){ // Update interrupt - TIM2->SR &= // clear update interrupt flag - //User code to handle overflow - // ... - } - if((TIM2->SR & TIM_SR_CC1IF) != 0){ - // User code to calculate the period of 1Hz pulse - //... - //printf("%f[sec]\r\n", period); // print out the period on TeraTerm - TIM2->SR &= // clear capture/compare interrupt flag ( it is also cleared by reading TIM2_CCR1) - } -} +/** +****************************************************************************** +* @author SSSLAB +* @Mod 2021-8-12 by YKKIM +* @brief Embedded Controller: Tutorial ___ +* - _________________________________ +* +****************************************************************************** +*/ + + +#include "stm32f411xe.h" +#include "math.h" +#include "ecGPIO.h" +#include "ecRCC.h" +#include "ecTIM.h" +#include "ecUART_student.h" + +uint32_t ovf_cnt = 0; +uint32_t ccr2 = 0; +float period = 0; + +int main(void){ + + RCC_PLL_init(); + UART2_init(); + + // GPIO configuration --------------------------------------------------------------------- + GPIO_init(GPIOA, 5, AF); // PB10: Alternate Function Mode + // TIM configuration ----------------------------------------------------------------------- + GPIOA->AFR[] |= //AF1(TIM2) + RCC->APB1ENR |= //TIM2 Clock enabled + + TIM2->PSC = // Timer counter clock: 1M`Hz(1us) + TIM2->ARR = // Set auto reload register to maximum (count up to 65535) + TIM2->CR1 & // CR1 DIR enabled + TIM2->CCMR1 |= // Capture/Compare Selection: CC1 is mapped on TI1 + TIM2->CCMR1 &= // Clear IC1F + TIM2->CCMR1 |= // Set filter N=4 + TIM2->CCER &= // Clear CCER + TIM2->CCER &= // Capture rising edge + TIM2->CCER |= // Capture enabled + TIM2->DIER |= // Interrupt enabled + TIM2->DIER = // Update interrupt enable + TIM2->CR1 = // Counter enable + + + NVIC_SetPriority(, 2); // Set the priority of TIM2 interrupt request + NVIC_EnableIRQ(); // TIM2 interrupt request enable + + while(1); +} + +void TIM2_IRQHandler(void){ + if(TIM2->SR & TIM_SR_UIF){ // Update interrupt + TIM2->SR &= // clear update interrupt flag + //User code to handle overflow + // ... + } + if((TIM2->SR & TIM_SR_CC1IF) != 0){ + // User code to calculate the period of 1Hz pulse + //... + //printf("%f[sec]\r\n", period); // print out the period on TeraTerm + TIM2->SR &= // clear capture/compare interrupt flag ( it is also cleared by reading TIM2_CCR1) + } +} diff --git a/tutorial/tutorial-student/TU_uVision_Debugging.c b/lab-tutorial/tutorial-student/TU_uVision_Debugging.c similarity index 100% rename from tutorial/tutorial-student/TU_uVision_Debugging.c rename to lab-tutorial/tutorial-student/TU_uVision_Debugging.c diff --git a/tutorial/tutorial-student/readme.md b/lab-tutorial/tutorial-student/readme.md similarity index 100% rename from tutorial/tutorial-student/readme.md rename to lab-tutorial/tutorial-student/readme.md diff --git a/src/README.md b/src/README.md index ef2b7f3..3a867b5 100644 --- a/src/README.md +++ b/src/README.md @@ -1,3 +1,8 @@ # EC-student -assignment src files goes here \ No newline at end of file +Your assignment, tutorial, lab, project source files go here + + + +Please make it private repository for your code must not be accessible by other students. +