Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2019 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include <math.h>
- #include "stm32f1xx.h"
- #include "stm32f1xx_hal.h"
- #include "sd_hal_mpu6050.h"
- #include <stdarg.h>
- #include <string.h>
- #include <stdlib.h>
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- I2C_HandleTypeDef hi2c1;
- I2C_HandleTypeDef hi2c2;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- SD_MPU6050 mpu1;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_I2C2_Init(void);
- static void MX_I2C1_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- #define Bufor_size 200
- #define pocz_ramka '#'
- #define kon_ramka '*'
- #define ESC '!'
- #define ESC_START '('
- #define ESC_STOP ')'
- uint8_t czy_pocz=0, czy_specjal=0, czy_kon=0;
- char BUF_RX[Bufor_size];
- char BUF_TX[Bufor_size];
- char com[Bufor_size];
- char bf[Bufor_size];
- volatile uint8_t BusyRX = 0;
- volatile uint8_t EmptyRX = 0;
- volatile uint8_t BusyTX = 0;
- volatile uint8_t EmptyTX = 0;
- volatile uint8_t Busytab = 0;
- volatile uint8_t Emptytab = 0;
- int x,y;
- uint16_t wielkosc_ram;
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart){
- if(huart==&huart2){
- if(EmptyTX!=BusyTX){
- uint8_t tmp=BUF_TX[BusyTX];
- BusyTX++;
- if(BusyTX>=Bufor_size){
- BusyTX=0;
- }
- HAL_UART_Transmit_IT(&huart2,&tmp,1);
- }
- }
- }
- void send (char* format,...){
- char tmp_rs[128];
- int i;
- uint8_t idx;
- va_list arglist; //zmienna typu do przechowywania informacji o zmiennych argumentach
- va_start(arglist,format); //Inicjalizuj zmienną listę argumentów
- vsprintf(tmp_rs,format,arglist); //Zapisuj sformatowane dane z arglist na string
- va_end(arglist); //Zakończ przy użyciu zmiennej listy argumentów
- idx=EmptyTX;
- for(i=0;i<strlen(tmp_rs);i++){
- BUF_TX[idx]=tmp_rs[i];
- idx++;
- if(idx>=Bufor_size){idx=0;}
- }
- __disable_irq(); //wyłączenie przerwań
- if((EmptyTX==BusyTX) && (__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TXE)==SET)){
- EmptyTX=idx;
- uint8_t tmp=BUF_TX[BusyTX];
- BusyTX++;
- if(BusyTX>=Bufor_size){BusyTX=0;}
- HAL_UART_Transmit_IT(&huart2,&tmp,1);
- }else{
- EmptyTX=idx;
- }
- __enable_irq(); //włączenie przerwań
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
- if(huart==&huart2){
- EmptyRX++;
- if(EmptyRX>=Bufor_size){EmptyRX=0;}
- HAL_UART_Receive_IT(&huart2,&BUF_RX[EmptyRX],1);
- }
- }
- /*unsigned char DigToHex(unsigned char d)
- // Konwersja liczb 0..F do znakow '0'..'F'
- {
- char zn;
- // if( (d<10)) return('0' + d);
- if((d>=0) && (d<10)) zn = ('0' + d);
- if((d>=10) && (d<=15)) zn = ('A' + d - 10);
- return zn;
- }
- void CharToHex(char c, char x[3])
- //Convert char c to hex reprezentation x
- {
- unsigned char xh,xl;
- xl = (unsigned char)c & 0x0f;
- xh = ((unsigned char)c & 0xf0) >> 4;
- x[0] = DigToHex(xh); x[1] = DigToHex(xl); x[2] = 0;
- }
- unsigned char HexToDig(char c)
- // Konwersja znakow '0'..'F' do liczb 0..F
- {
- char znak;
- if((c>='0') && (c<='9')) znak = (c - '0');
- if((c>='A') && (c<='F')) znak = (c - 'A' + 10);
- return znak;
- }
- char HexToChar(char x[3])
- // Convert hex string x to char
- {
- unsigned char xh,xl,c;
- xh = HexToDig(x[0]);
- xl = HexToDig(x[1]);
- c = ((xh << 4) + xl);
- return (char) c;
- }
- unsigned char lrcgen(char *buf, int size)
- // Wyznaczanie LCR dla bufora buf i size znakow
- {
- unsigned char sum = 0;
- int i;
- for(i=0;i<size;i++) sum = sum + buf[i];
- return(~sum + 1);
- }
- void SendFrm( int Adr, int Command, int Size, char *Data)
- Send Command to port dev, unit Adr, Size char of Data
- Uzyta zmienna globalna FrameBuf
- {
- unsigned char lrc;
- int i,cnt;
- char tmp[3];
- Computing of LCR ----------------
- FrameBuf[0] = (char)(Adr & 0x00FF);
- FrameBuf[1] = (char)(Command & 0x00FF);
- if( Size > 0) for(i=0;i<Size;i++) FrameBuf[i+2] = Data[i];
- lrc = lrcgen(FrameBuf,Size + 2);
- Budowa ramki -------------------------------------------------
- FrameBuf[0] = '#';
- Address -----------------------------------------
- CharToHex((char) Adr,tmp);
- FrameBuf[1] = tmp[0]; FrameBuf[2] = tmp[1];
- Command -----------------------------------------
- CharToHex((char)Command,tmp);
- FrameBuf[3] = tmp[0]; FrameBuf[4] = tmp[1];
- cnt = 5;
- if( Size > 0 ) { Data ---------------------------
- for(i=0;i<Size;i++) {
- CharToHex(Data[i],tmp);
- FrameBuf[cnt] = tmp[0]; FrameBuf[cnt+1] = tmp[1];
- cnt++; cnt++;
- }
- }
- LRC ---------------------------------------
- CharToHex((char) lrc,tmp);
- FrameBuf[cnt] = tmp[0]; FrameBuf[cnt+1] = tmp[1]; cnt= cnt+2;
- End of Frame Marks ------------------------
- FrameBuf[cnt] = CR; cnt++;
- FrameBuf[cnt] = LF; cnt++;
- FrameBuf[cnt+1] = '\0'; <-- usunac
- Ramka gotowa --> wysylac
- printf("\nWyslano: ");
- for(i=0;i<cnt;i++) {
- write(&FrameBuf[i],1);
- printf("%c",FrameBuf[i]);
- }
- send("wyslano: %d znakow\n",cnt);
- }*/
- uint8_t USART_getchar(){
- uint tmp;
- if(EmptyRX!=BusyRX){
- tmp=BUF_RX[BusyRX];
- BusyRX++;
- if(BusyRX == Bufor_size)BusyRX=0;
- return tmp;
- }else{
- return 0;
- }
- }
- /*void atoh(char *ascii_ptr, char* hex_ptr,int len)
- {
- int i;
- for(i = 0; i < (len / 2); i++)
- {
- if((*(ascii_ptr+(2*i)) <= '9'))
- *(hex_ptr+i) = ((*(ascii_ptr+(2*i)) - '0') * 16 );
- else
- *(hex_ptr+i) = (((*(ascii_ptr+(2*i)) - 'A') + 10) << 4);
- (hex_ptr+i) = ((ascii_ptr+(2*i)) <= '9') ? (((ascii_ptr+(2*i)) - '0') * 16 ) : ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
- (hex_ptr+i) |= ((ascii_ptr+(2*i)+1) <= '9') ? ((ascii_ptr+(2*i)+1) - '0') : ((ascii_ptr+(2*i)+1) - 'A' + 10);
- }
- }*/
- /*void atoh2(char ascii_ptr, char hex_ptr,int len)
- {
- int i;
- for(i = 0; i < (len / 2); i++)
- {
- if((ascii_ptr) <= '9')
- (hex_ptr) = ((ascii_ptr - '0') * 16 );
- else
- (hex_ptr+i) = ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
- (hex_ptr+i) = ((ascii_ptr+(2*i)) <= '9') ? (((ascii_ptr+(2*i)) - '0') * 16 ) : ((((ascii_ptr+(2*i)) - 'A') + 10) << 4);
- (hex_ptr+i) |= ((ascii_ptr+(2*i)+1) <= '9') ? ((ascii_ptr+(2*i)+1) - '0') : ((ascii_ptr+(2*i)+1) - 'A' + 10);
- // }
- }*/
- /*char kodow(char com[]){
- int idx=1;
- char tmp[Bufor_size];
- int len = strlen(com);
- atoh(&com,&tmp,len);
- send("kodoww %c",com);
- return tmp;
- }*/
- /*void comma(void){
- uint16_t idx = 0;
- while(EmptyRX!=BusyRX)
- com[idx]=USART_getchar();
- send("GETCHE %c",com[idx]);
- com[idx++]=USART_getchar();
- send("GETCHE1 %c",com[idx]);
- if(com[idx]==pocz_ramka){
- czy_pocz =1;
- idx=0;
- com[idx]=0;
- }else if(com[idx] == ESC){
- czy_specjal =1;
- com[idx]=USART_getchar();}
- else if(com[idx] == ESC_START && czy_specjal == 1){
- com[idx]=pocz_ramka;
- idx++;
- }
- send("kod %c",com);
- for(int i =0 ; i<strlen(com);i++){
- kodow(com);
- send("kod %x",kodow(com));}
- }*/
- int Frame_Size(uint8_t Busy, uint8_t Empty){
- wielkosc_ram= Empty>=Busy?(Empty-Busy):(Empty-Busy+Bufor_size);
- return wielkosc_ram;
- }
- void poczatek(char com[],uint16_t wielkosc,uint8_t pocz){
- for(int i=pocz;i<wielkosc;i++){
- //send("main %c",com[i]);
- if(com[i]==pocz_ramka){
- send("Mamy poczatek %d",i);
- czy_pocz=1;
- Busytab=i;break;
- }
- else {czy_pocz=0;}
- }
- }
- void koniec(char com[],uint16_t wielkosc,uint8_t pocz){
- for(int i=pocz;i<wielkosc;i++){
- //send("main %c",com[i]);
- if(com[i]==kon_ramka){
- send("Mamy koniec %d",i);
- czy_kon=1;
- Emptytab=i;break;
- }
- else {czy_kon=0;}
- }
- }
- void GetCommand(uint8_t EmptyRX, uint8_t BusyRX) {
- uint16_t idx = 0;
- send("ramka");
- BusyRX++;
- if (BusyRX >= Bufor_size) {
- BusyRX = 0;
- }
- while (BusyRX != EmptyRX) {
- com[idx] = USART_getchar(); //BUF_RX[BusyRX];
- BusyRX++;
- idx++;
- if (BusyRX >= Bufor_size) {
- BusyRX = 0;
- }
- if (idx >= Bufor_size) {
- break;
- }
- }
- for(int i =0;i<=(Emptytab-Busytab);i++){
- send("NASZA RAMKA %c\n\r",com[i]);
- }
- }
- void Find_Frame(){
- if(BusyRX!=EmptyRX)
- {Frame_Size(BusyRX,EmptyRX);
- poczatek(BUF_RX,EmptyRX,BusyRX);
- koniec(BUF_RX,EmptyRX,BusyRX);
- if(czy_pocz == 1 && czy_kon == 1){
- GetCommand(Emptytab,Busytab);
- }
- else {send("Brak ramki");}
- }
- }
- void sprawdzenie(){
- if(strlen(com)==16){
- send("ramka ok\n\r");
- }
- else {send("zla ramka\n\r");}
- /*for(int i = 0; i < 6; i++){
- if(com[i] >='0' && com[i]<='9')send("ramka ok1\n\r");
- else send("zla ramka1\n\r");
- }*/
- /*if(com[9]==':')send("ramka ok2\n\r");
- else send("zla ramka2\n\r");*/
- }
- /*
- void read_st1(){
- if(BusyRX!=EmptyRX)
- {
- //buf_pomoc=BusyRX;
- wielkosc=((EmptyRX-BusyRX)+256)%256;
- if(find_start==256) buf_pomoc=BusyRX;
- if(BUF_RX[buf_pomoc]=='*') // wyszukanie pierwszego znaku startu
- {
- send("*\n");
- BusyRX=buf_pomoc;
- find_start=buf_pomoc;
- if((buf_pomoc+1)%256!=EmptyRX) buf_pomoc=(buf_pomoc+1)%256;
- }
- else if(find_start!=256 && BUF_RX[buf_pomoc]=='/') //znalezienie znaku stopu
- {
- send("/\n");
- find_stop=buf_pomoc;
- pomoc_wielkosc=((find_stop-find_start)+257)%256;
- if(pomoc_wielkosc>=6 && pomoc_wielkosc-6<=128) // ramka o odpowiedniej minimalnej maksymalnej dlugosci
- {
- if(BUF_RX[find_start+1]>=48 && BUF_RX[find_start+1]<=57 && // czy 1 znak jest cyfra
- BUF_RX[find_start+2]>=48 && BUF_RX[find_start+2]<=57 && // czy 2 znak jest cyfra
- BUF_RX[find_start+3]>=48 && BUF_RX[find_start+3]<=57 && BUF_RX[find_start+4]==':') // czy 3 znak jest cyfra i czy 4 znak jest :
- {
- if(pomoc_wielkosc-6==(BUF_RX[find_start+1]-48)*100+(BUF_RX[find_start+2]-48)*10+(BUF_RX[find_start+3]-48))
- { //jesli dlugosc zadeklarowana dlugosc bedzie rowna dlugosci ramki
- find_start=(find_start+5)%256;
- while(find_start!=find_stop) //kopiowanie do drugiego bufora
- {
- bufor_komend[drugi_stop]=BUF_RX[find_start];
- find_start=(find_start+1)%256;
- drugi_stop=(drugi_stop+1)%256;
- }
- send("Ramka ok\n");
- BusyRX=(find_stop+1)%256;
- find_start=256;
- find_stop=256;
- //ll
- }
- else // bledna budowa ramki - inna zadeklarowana dlugosc
- {
- send("Bledna ramka dl\n");
- BusyRX=find_stop;
- find_start=256;
- find_stop=256;
- }
- }
- else // bledna budowa ramki - dlugosc ramki nie zapisana jako cyfry
- {
- send("Bledna ramka cy\n");
- BusyRX=(find_stop+1)%256;
- find_start=256;
- find_stop=256;
- }
- }
- else // zbyt krótka lub zbyt dluga ramka
- {
- send("Bledna ramka krdl\n");
- BusyRX=(find_stop+1)%256;
- find_start=256;
- find_stop=256;
- }
- }
- else if(find_start!=256) // znalezienie znaku w srodku ramki
- {
- send("znak w\n");
- if((buf_pomoc+1)%256!=EmptyRX) buf_pomoc=(buf_pomoc+1)%256;
- }
- else // znalezienie znaku poza ramka
- { send("znak poza\n");
- buf_pomoc=(buf_pomoc+1)%256;
- BusyRX=buf_pomoc;
- }
- }
- }*/
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- SD_MPU6050_Result result ;
- char mpu_ok[14] = {"MPU DZIALA\n\r"};
- char mpu_not[16] = {"MPU NIE DZIALA\n"};
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART2_UART_Init();
- MX_I2C2_Init();
- MX_I2C1_Init();
- /* USER CODE BEGIN 2 */
- HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
- /*Ustawienie parametrów akcelerometru*/
- result = SD_MPU6050_Init(&hi2c2,&mpu1,SD_MPU6050_Device_0,SD_MPU6050_Accelerometer_2G);
- HAL_Delay(500);
- /*Sprawdzenie czy moduł działa*/
- if(result == SD_MPU6050_Result_Ok)
- {
- send(mpu_ok);
- }
- else
- {
- send(mpu_not);
- }
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- // HAL_Delay(500);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- SD_MPU6050_ReadAccelerometer(&hi2c2,&mpu1);
- int16_t a_x = mpu1.Accelerometer_X;
- int16_t a_y = mpu1.Accelerometer_Y;
- int16_t a_z = mpu1.Accelerometer_Z;
- /*uint8_t idx;
- while(BusyRX!=EmptyRX){
- bf[idx]= USART_getchar();
- send("ramka %c",bf[idx]);
- if(bf[idx] == pocz_ramka)
- {
- czy_pocz = 1;
- send("pocz");
- idx=0;
- bf[idx]=0;
- send("bufor %c",bf[idx]);
- }else if(bf[idx]==ESC){
- czy_specjal=1;
- bf[idx]=USART_getchar();
- }else if(bf[idx] == ESC_START && czy_specjal == 1){
- bf[idx]=pocz_ramka;
- idx++;}
- */
- HAL_UART_Receive_IT(&huart2,&BUF_RX[EmptyRX],1);
- if(BusyRX != EmptyRX)
- {
- send("BusyRX %d",BusyRX);
- send("EmptyRX %d",EmptyRX);
- send("Pobranie");
- Find_Frame();
- //if(czy_pocz==1 && czy_kon==1){
- // send("sprawdz");
- //sprawdzenie();
- //}
- BusyRX=Emptytab;
- if (BusyRX >= Bufor_size) {
- BusyRX = 0;
- }
- //GetCommand(Emptytab,Busytab);
- //else send("niewiem");
- //send("BusyRX po ++ %d",BusyRX);
- x=Busytab;
- poczatek(BUF_RX,EmptyRX,BusyRX);
- y=Busytab;
- if(x == y){
- BusyRX=EmptyRX;
- send("kon");
- // BUF_RX=0;
- // BusyRX=0;
- // EmptyRX=0;
- }
- }
- // send("x = %d\n\ry = %d\n\rz = %d\n\r",a_x,a_y,a_z);
- memset(com, 0, sizeof(com));
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief I2C1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_I2C1_Init(void)
- {
- /* USER CODE BEGIN I2C1_Init 0 */
- /* USER CODE END I2C1_Init 0 */
- /* USER CODE BEGIN I2C1_Init 1 */
- /* USER CODE END I2C1_Init 1 */
- hi2c1.Instance = I2C1;
- hi2c1.Init.ClockSpeed = 100000;
- hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN I2C1_Init 2 */
- /* USER CODE END I2C1_Init 2 */
- }
- /**
- * @brief I2C2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_I2C2_Init(void)
- {
- /* USER CODE BEGIN I2C2_Init 0 */
- /* USER CODE END I2C2_Init 0 */
- /* USER CODE BEGIN I2C2_Init 1 */
- /* USER CODE END I2C2_Init 1 */
- hi2c2.Instance = I2C2;
- hi2c2.Init.ClockSpeed = 100000;
- hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c2.Init.OwnAddress1 = 0;
- hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c2.Init.OwnAddress2 = 0;
- hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN I2C2_Init 2 */
- /* USER CODE END I2C2_Init 2 */
- }
- /**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART2_UART_Init(void)
- {
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
- /* USER CODE END USART2_Init 2 */
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
- /*Configure GPIO pin : LD2_Pin */
- GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement