docs: record CH390 hardware-bound conclusion

This commit is contained in:
2026-04-01 04:30:27 +08:00
parent 1808f9916f
commit 81594c6520
6 changed files with 340 additions and 20 deletions
+92
View File
@@ -61,6 +61,8 @@ extern SPI_HandleTypeDef hspi1;
/* Timeout for SPI operations (ms) */
#define SPI_TIMEOUT 100
#define CH390_BITBANG_HALF_PERIOD_US 2u
/*----------------------------------------------------------------------------
* Low-level GPIO operations
*---------------------------------------------------------------------------*/
@@ -86,6 +88,80 @@ static inline void ch390_rst(uint8_t state)
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
static inline void ch390_sck(uint8_t state)
{
HAL_GPIO_WritePin(CH390_SCK_PORT, CH390_SCK_PIN,
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
static inline void ch390_mosi(uint8_t state)
{
HAL_GPIO_WritePin(CH390_MOSI_PORT, CH390_MOSI_PIN,
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
static inline uint8_t ch390_miso(void)
{
return (uint8_t)(HAL_GPIO_ReadPin(CH390_MISO_PORT, CH390_MISO_PIN) == GPIO_PIN_SET);
}
static void ch390_spi_gpio_mode(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = CH390_SCK_PIN | CH390_MOSI_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(CH390_SCK_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = CH390_MISO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(CH390_MISO_PORT, &GPIO_InitStruct);
ch390_cs(1u);
ch390_sck(1u);
ch390_mosi(1u);
}
static void ch390_spi_restore_pins(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = CH390_SCK_PIN | CH390_MOSI_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(CH390_SCK_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = CH390_MISO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(CH390_MISO_PORT, &GPIO_InitStruct);
ch390_spi_init();
}
static uint8_t ch390_bitbang_exchange_byte(uint8_t byte)
{
uint8_t bit;
uint8_t rx_data = 0u;
for (bit = 0u; bit < 8u; ++bit) {
ch390_mosi((uint8_t)((byte & 0x80u) != 0u));
ch390_delay_us(CH390_BITBANG_HALF_PERIOD_US);
ch390_sck(0u);
ch390_delay_us(CH390_BITBANG_HALF_PERIOD_US);
rx_data = (uint8_t)((rx_data << 1) | ch390_miso());
ch390_sck(1u);
byte <<= 1;
ch390_delay_us(CH390_BITBANG_HALF_PERIOD_US);
}
return rx_data;
}
/*----------------------------------------------------------------------------
* SPI Communication
*---------------------------------------------------------------------------*/
@@ -241,6 +317,22 @@ void ch390_hardware_reset(void)
ch390_delay_us(50000); /* Wait 50ms for CH390 to initialize reliably */
}
uint8_t ch390_bitbang_read_reg(uint8_t reg)
{
uint8_t value;
__HAL_SPI_DISABLE(&hspi1);
ch390_spi_gpio_mode();
ch390_cs(0u);
(void)ch390_bitbang_exchange_byte((uint8_t)(reg | OPC_REG_R));
value = ch390_bitbang_exchange_byte(0x00u);
ch390_cs(1u);
ch390_spi_restore_pins();
return value;
}
/*----------------------------------------------------------------------------
* CH390 Register/Memory Access Functions (SPI Mode)
*---------------------------------------------------------------------------*/