refactor: minor code changes

This commit is contained in:
IOsetting 2023-03-02 10:09:51 +08:00
parent a5862acace
commit c716d1f019
2 changed files with 22 additions and 71 deletions

View File

@ -2,12 +2,13 @@
* Demo: I2C - SSD1306 OLED
*
* PY32 SSD1306
* PA9 SCL
* PA10 SDA
* PA9/PF1 SCL
* PA10/PF0 SDA
*/
#include <string.h>
#include "main.h"
#include "py32f0xx_bsp_printf.h"
#include "py32f0xx_bsp_clock.h"
#include "ssd1306.h"
#define I2C_ADDRESS 0xA0 /* host/client address */
@ -18,21 +19,18 @@
__IO uint32_t i2cState = I2C_STATE_READY;
static void APP_SystemClockConfig(void);
static void APP_I2cMasterConfig(void);
static void APP_I2C_Config(void);
int main(void)
{
int y1, y2;
APP_SystemClockConfig();
BSP_RCC_HSI_24MConfig();
BSP_USART_Config(115200);
printf("I2C Demo: \r\nClock: %ld\r\n", SystemCoreClock);
APP_I2cMasterConfig();
APP_I2C_Config();
uint8_t res = SSD1306_Init();
printf("OLED init: %d\n", res);
@ -123,11 +121,11 @@ int main(void)
while(1);
}
static void APP_I2cMasterConfig(void)
static void APP_I2C_Config(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOF);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
/**
@ -137,18 +135,18 @@ static void APP_I2cMasterConfig(void)
* Change pins to PF1 / PF0 for parts have no PA9 / PA10
*/
// PA9 SCL
GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Alternate = LL_GPIO_AF_12;
LL_GPIO_Init(GPIOF, &GPIO_InitStruct);
// PA10 SDA
GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
GPIO_InitStruct.Alternate = LL_GPIO_AF_6;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
GPIO_InitStruct.Alternate = LL_GPIO_AF_12;
LL_GPIO_Init(GPIOF, &GPIO_InitStruct);
LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
@ -213,23 +211,6 @@ void APP_I2C_Transmit(uint8_t devAddress, uint8_t memAddress, uint8_t *pData, ui
i2cState = I2C_STATE_READY;
}
/**
* Set system clock to 48MHz
*/
static void APP_SystemClockConfig(void)
{
LL_UTILS_ClkInitTypeDef UTILS_ClkInitStruct;
LL_RCC_HSI_Enable();
LL_RCC_HSI_SetCalibFreq(LL_RCC_HSICALIBRATION_24MHz);
while (LL_RCC_HSI_IsReady() != 1);
UTILS_ClkInitStruct.AHBCLKDivider = LL_RCC_SYSCLK_DIV_1;
UTILS_ClkInitStruct.APB1CLKDivider = LL_RCC_APB1_DIV_1;
LL_PLL_ConfigSystemClock_HSI(&UTILS_ClkInitStruct);
LL_InitTick(48000000, 1000U);
}
void APP_ErrorHandler(void)
{
while (1);

View File

@ -1,63 +1,35 @@
/**
******************************************************************************
* @file main.c
* @author MCU Application Team
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) Puya Semiconductor Co.
* All rights reserved.</center></h2>
*
* <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "py32f0xx_bsp_printf.h"
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private user code ---------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void APP_LedConfig(void);
static void APP_GPIO_Config(void);
int main(void)
{
HAL_Init();
APP_LedConfig();
APP_GPIO_Config();
BSP_USART_Config();
printf("SystemClk:%ld\r\n", SystemCoreClock);
while (1)
{
HAL_Delay(1000);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
printf("echo\r\n");
}
}
static void APP_LedConfig(void)
static void APP_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
__HAL_RCC_GPIOA_CLK_ENABLE();
// PA0
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void APP_ErrorHandler(void)
@ -75,5 +47,3 @@ void assert_failed(uint8_t *file, uint32_t line)
while (1);
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT Puya *****END OF FILE******************/