From: kyz Date: Wed, 10 Apr 2024 02:22:14 +0000 (+0800) Subject: add sdmmc X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=227069cddbd56df27630701c1f4f924c32953bfd;p=yn%2Fweldingcontroller.git add sdmmc 增加sdmmc读写功能代码 关闭温度检测 --- diff --git a/Core/Inc/stm32h7xx_it.h b/Core/Inc/stm32h7xx_it.h index 59038c1..e109ac2 100644 --- a/Core/Inc/stm32h7xx_it.h +++ b/Core/Inc/stm32h7xx_it.h @@ -56,6 +56,7 @@ void DMA1_Stream1_IRQHandler(void); void DMA1_Stream2_IRQHandler(void); void DMA1_Stream3_IRQHandler(void); void USART1_IRQHandler(void); +void SDMMC1_IRQHandler(void); void UART4_IRQHandler(void); void UART5_IRQHandler(void); void TIM7_IRQHandler(void); diff --git a/Core/Src/main.c b/Core/Src/main.c index 2f26ca4..395f02f 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,7 +26,6 @@ #include "iwdg.h" #include "lwip.h" #include "octospi.h" -#include "sdmmc.h" #include "spi.h" #include "tim.h" #include "usart.h" @@ -134,9 +133,8 @@ int main(void) MX_TIM2_Init(); MX_UART4_Init(); MX_OCTOSPI1_Init(); - MX_SDMMC1_SD_Init(); MX_TIM1_Init(); - MX_IWDG1_Init(); +// MX_IWDG1_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ @@ -234,7 +232,7 @@ void PeriphCommonClock_Config(void) /** Initializes the peripherals clock */ - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC; + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC|RCC_PERIPHCLK_SDMMC; PeriphClkInitStruct.PLL2.PLL2M = 8; PeriphClkInitStruct.PLL2.PLL2N = 24; PeriphClkInitStruct.PLL2.PLL2P = 2; @@ -243,6 +241,7 @@ void PeriphCommonClock_Config(void) PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3; PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE; PeriphClkInitStruct.PLL2.PLL2FRACN = 0; + PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL2; PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { diff --git a/Core/Src/sdmmc.c b/Core/Src/sdmmc.c index a3562cb..7930266 100644 --- a/Core/Src/sdmmc.c +++ b/Core/Src/sdmmc.c @@ -43,7 +43,7 @@ void MX_SDMMC1_SD_Init(void) hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE; hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B; hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE; - hsd1.Init.ClockDiv = 0; + hsd1.Init.ClockDiv = 2; if (HAL_SD_Init(&hsd1) != HAL_OK) { Error_Handler(); @@ -58,22 +58,11 @@ void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; - RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; if(sdHandle->Instance==SDMMC1) { /* USER CODE BEGIN SDMMC1_MspInit 0 */ /* USER CODE END SDMMC1_MspInit 0 */ - - /** Initializes the peripherals clock - */ - PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SDMMC; - PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL; - if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) - { - Error_Handler(); - } - /* SDMMC1 clock enable */ __HAL_RCC_SDMMC1_CLK_ENABLE(); @@ -102,6 +91,9 @@ void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle) GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1; HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); + /* SDMMC1 interrupt Init */ + HAL_NVIC_SetPriority(SDMMC1_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(SDMMC1_IRQn); /* USER CODE BEGIN SDMMC1_MspInit 1 */ /* USER CODE END SDMMC1_MspInit 1 */ @@ -132,6 +124,8 @@ void HAL_SD_MspDeInit(SD_HandleTypeDef* sdHandle) HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2); + /* SDMMC1 interrupt Deinit */ + HAL_NVIC_DisableIRQ(SDMMC1_IRQn); /* USER CODE BEGIN SDMMC1_MspDeInit 1 */ /* USER CODE END SDMMC1_MspDeInit 1 */ diff --git a/Core/Src/stm32h7xx_it.c b/Core/Src/stm32h7xx_it.c index d6a9c6f..761f9ed 100644 --- a/Core/Src/stm32h7xx_it.c +++ b/Core/Src/stm32h7xx_it.c @@ -60,6 +60,7 @@ extern PCD_HandleTypeDef hpcd_USB_OTG_HS; extern DMA_HandleTypeDef hdma_adc1; extern DMA_HandleTypeDef hdma_adc2; extern DMA_HandleTypeDef hdma_adc3; +extern SD_HandleTypeDef hsd1; extern UART_HandleTypeDef huart4; extern UART_HandleTypeDef huart5; extern UART_HandleTypeDef huart1; @@ -223,6 +224,20 @@ void USART1_IRQHandler(void) /* USER CODE END USART1_IRQn 1 */ } +/** + * @brief This function handles SDMMC1 global interrupt. + */ +void SDMMC1_IRQHandler(void) +{ + /* USER CODE BEGIN SDMMC1_IRQn 0 */ + + /* USER CODE END SDMMC1_IRQn 0 */ + HAL_SD_IRQHandler(&hsd1); + /* USER CODE BEGIN SDMMC1_IRQn 1 */ + + /* USER CODE END SDMMC1_IRQn 1 */ +} + /** * @brief This function handles UART4 global interrupt. */ diff --git a/MDK-ARM/welding_controller_v2.uvprojx b/MDK-ARM/welding_controller_v2.uvprojx index c7c571e..0334dad 100644 --- a/MDK-ARM/welding_controller_v2.uvprojx +++ b/MDK-ARM/welding_controller_v2.uvprojx @@ -445,6 +445,8 @@ 2 2 11 + + 1 @@ -472,19 +474,13 @@ 2 2 2 + + + + + + - - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 1 - @@ -766,6 +762,8 @@ 2 2 11 + + 1 @@ -793,19 +791,13 @@ 2 2 2 + + + + + + - - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 1 - @@ -1243,6 +1235,16 @@ 1 ..\user\user_report.c + + user_sdmmc.c + 1 + ..\user\user_sdmmc.c + + + ht1622.c + 1 + ..\user\ht1622.c + diff --git a/user/ht1622.c b/user/ht1622.c new file mode 100644 index 0000000..9f5bd78 --- /dev/null +++ b/user/ht1622.c @@ -0,0 +1,303 @@ +/* +1. RAM 访问: + ---------------------------------------------- + | COM7 COM6 COM5 COM4 | COM3 COM2 COM1 COM0 | + ---------------------------------------------- +SEG0 | 1 | 0 | + ---------------------------------------------- +SEG1 | 3 | 2 | + ---------------------------------------------- + ... + ---------------------------------------------- +SEG31 | 63 | 62 | + ---------------------------------------------- + | D3 D2 D1 D0 | D3 D2 D1 D0 | + ---------------------------------------------- + + 每个存储单元地址6Bits,存储数据4Bits, 一共64个单元 + 如地址 3存储数据对应 D3:COM7_SEG1, D2:COM6_SEG1, D1:COM5_SEG1, D0:COM4_SEG1 +2. 控制信号 +/CS 片选 +/WR 写CLK 3V/最大150K +/RD 读CLK 3V/最大75K +DATA 数据输入输出 + +mode ID +----------- +READ 110 +WRITE 101 +READ-MODIFY_WRITE 101 +COMMAND 100 + +读取操作, +读取单个单元,发送9个bit 读取4个bit, 发送 WR_CLK 110 A5...A0, 读取RD_CLK D0 D1 D2 D3 +读取连续单元,发送9个bit 读取4个bit, 发送 WR_CLK 110 MA(A5...A0), 读取RD_CLK (MA:D0 D1 D2 D3) (MA+1:D0 D1 D2 D3) ... (MA+n:D0 D1 D2 D3) +写入操作, +写入单个单元,发送9个bit 读取4个bit, 发送 WR_CLK 101 A5...A0, 发送 WR_CLK D0 D1 D2 D3 +写入连续单元,发送9个bit 读取4个bit, 发送 101 MA(A5...A0), 发送 WR_CLK (MA:D0 D1 D2 D3) (MA+1:D0 D1 D2 D3) ... (MA+n:D0 D1 D2 D3) + +读取修改写入READ-MODIFY-WRITE Mode +先读取 发送9个bit 读取4个bit 再写入4bit, 发送 WR_CLK 110 A5...A0, 读取RD_CLK D0 D1 D2 D3 再写入新的值到当前存储单元写4个bit 发送 WR_CLK (MA:D0 D1 D2 D3) + +命令模式 +发送 100 C8...C0 ... C8...C0 +*/ +#include "user.h" + +#define MODE_ID +#define MODE_ID_READ 0x06 //110 +#define MODE_ID_WRITE 0x05 //101 +#define MODE_ID_MODIFY 0x05 //101 +#define MODE_ID_COMMAND 0x04 //100 + +#define CMD_SYS_DIS 0 +#define CMD_SYS_EN 1 +#define CMD_LCD_OFF 2 +#define CMD_LCD_ON 3 +#define CMD_TIMER_DIS 4 +#define CMD_WDT_DIS 5 +#define CMD_TIMER_EN 6 +#define CMD_WET_EN 7 +#define CMD_TONE_OFF 8 +#define CMD_CLR_TIMER 0x0D +#define CMD_CLR_WDT 0x0F +#define CMD_RC_32K 0x18 +#define CMD_EXT_32K 0x1C +#define CMD_TONE_4k 0x40 +#define CMD_TONE_2K 0x60 +#define CMD_IRQ_DIS 0x80 +#define CMD_IRQ_EN 0x88 +#define CMD_F1 0xA0 +#define CMD_F2 0xA1 +#define CMD_F4 0xA2 +#define CMD_F8 0xA3 +#define CMD_F16 0xA4 +#define CMD_F32 0xA5 +#define CMD_F64 0xA6 +#define CMD_F128 0xA7 +#define CMD_TEST 0xE0 +#define CMD_NORMAL 0xE3 + +#define CMD_VALUE_12B( cmd_8b ) ( (0x04<<9)|((cmd_8b)<<1) ) + + +#define ht1622_cs_l() +#define ht1622_cs_h() +#define ht1622_rd_clk_l() +#define ht1622_rd_clk_h() +#define ht1622_wr_clk_l() +#define ht1622_wr_clk_h() +#define ht1622_data_out_l() +#define ht1622_data_out_h() + +#define ht1622_data_read() 0 + +void ht1622_data_dir_input(void) +{ +} + +void ht1622_data_dir_output(void) +{ +} + +void ht1622_delay_ms( uint32_t ms ) +{ +} + + +void ht1622_read_bits( uint8_t *data, uint32_t bits ) +{ + //dir:0 bn > ... > b1 > b0 + //dir:1 b0 > b1 > ... > bn + uint32_t d = 0; + for( int i = 0; i< bits; i++ ){ + ht1622_rd_clk_l(); + ht1622_delay_ms(1); + if( ht1622_data_read() ) + d |= 1< ... > b1 > b0 + //dir:1 b0 > b1 > ... > bn + for( int i = 0; i< bits; i++ ){ + ht1622_wr_clk_l(); + if( bit_order ){ + if( data & (1<>=1; + } + ht1622_delay_ms(1); + ht1622_delay_ms(1); + ht1622_wr_clk_h(); + ht1622_delay_ms(1); + } +} + +void ht1622_command( uint8_t cmd_8b ) +{ + ht1622_cs_h(); + ht1622_data_dir_output(); + ht1622_wr_clk_h(); + ht1622_rd_clk_h(); + ht1622_data_out_h(); + + ht1622_cs_l(); + ht1622_write_bits( CMD_VALUE_12B( cmd_8b ), 12, 0 ); + ht1622_cs_h(); +} + +void ht1622_ram_write( uint8_t addr, uint8_t *data_4b, uint8_t data_nr ) +{ + ht1622_cs_h(); + ht1622_data_dir_output(); + ht1622_wr_clk_h(); + ht1622_rd_clk_h(); + ht1622_data_out_h(); + + ht1622_cs_l(); + + ht1622_write_bits( MODE_ID_WRITE, 3, 0 ); + ht1622_write_bits( addr, 6, 0 ); + for( int i=0; i