minor cleanup, add i2c single register read/write functions

This commit is contained in:
true 2024-07-29 03:44:14 -07:00
parent 20870dc3e3
commit 8b3c45cebd
76 changed files with 58 additions and 46 deletions

View File

@ -1,26 +0,0 @@
/*
* i2c.h
*
* Created on: Jul 27, 2024
* Author: true
*/
#ifndef USER_SRC_I2C_H_
#define USER_SRC_I2C_H_
#include <stdint.h>
void i2c_init();
void i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len);
void i2c_write_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len);
int8_t i2c_ack_poll(uint8_t devaddr);
#endif /* USER_SRC_I2C_H_ */

View File

@ -8,6 +8,7 @@ encoding//periph/inc/ch32v00x_pwr.h=GBK
encoding//periph/inc/ch32v00x_rcc.h=GBK encoding//periph/inc/ch32v00x_rcc.h=GBK
encoding//periph/src/ch32v00x_adc.c=GBK encoding//periph/src/ch32v00x_adc.c=GBK
encoding//periph/src/ch32v00x_dbgmcu.c=GBK encoding//periph/src/ch32v00x_dbgmcu.c=GBK
encoding//periph/src/ch32v00x_flash.c=GBK
encoding//periph/src/ch32v00x_gpio.c=GBK encoding//periph/src/ch32v00x_gpio.c=GBK
encoding//periph/src/ch32v00x_i2c.c=GBK encoding//periph/src/ch32v00x_i2c.c=GBK
encoding//periph/src/ch32v00x_misc.c=GBK encoding//periph/src/ch32v00x_misc.c=GBK
@ -42,3 +43,4 @@ encoding//user/src/ui.h=GBK
encoding//user/system_ch32v00x.c=GBK encoding//user/system_ch32v00x.c=GBK
encoding//user/system_ch32v00x.h=UTF-8 encoding//user/system_ch32v00x.h=UTF-8
encoding/CH32V003F4P6.launch=GBK encoding/CH32V003F4P6.launch=GBK
encoding/retro_tech_fw.launch=GBK

View File

@ -51,7 +51,7 @@ void HardFault_Handler(void)
volatile uint16_t ticnt; volatile uint16_t ticnt;
volatile uint32_t uptime; volatile uint32_t uptime;
void SysTick_Handler() void SysTick_Handler(void)
{ {
if (++ticnt > 0x7ff) { if (++ticnt > 0x7ff) {
ticnt = 0; ticnt = 0;
@ -81,5 +81,3 @@ void SysTick_Handler()
return; return;
} }

View File

@ -22,6 +22,5 @@ extern volatile uint16_t ticnt;
extern volatile uint32_t uptime; extern volatile uint32_t uptime;
#endif /* __CH32V00x_IT_H */ #endif /* __CH32V00x_IT_H */

View File

@ -30,7 +30,7 @@
void systick_init(void) void systick_init(void)
{ {
SysTick->CMP = (SystemCoreClock / 2000) - 1; // we want a 2KHz interrupt SysTick->CMP = (SystemCoreClock / 2048) - 1; // we want a 2048Hz interrupt
SysTick->CNT = 0; // clear counter SysTick->CNT = 0; // clear counter
SysTick->CTLR = 0xF; // start counter in /1 mode, enable interrupts, auto-reset counter SysTick->CTLR = 0xF; // start counter in /1 mode, enable interrupts, auto-reset counter
SysTick->SR = 0; // clear count comparison flag SysTick->SR = 0; // clear count comparison flag

View File

@ -4,11 +4,11 @@
* routines more or less copied from WCH example code, then fucked around with to work. * routines more or less copied from WCH example code, then fucked around with to work.
* *
* these routines have serious issues. * these routines have serious issues.
* - any i2c issue will lock up the machine, and there's no timeout handlers * - any i2c issue may lock up the machine.
* - timeout handlers are hastily added and may have problems.
* - there's no error handling of any kind * - there's no error handling of any kind
* - the library code makes some serious assumptions re: flags * - the library code makes some serious assumptions re: flags
* * - it's a shitpile of polling
* this MUST be fixed before con.
*/ */
#include <ch32v00x.h> #include <ch32v00x.h>
@ -43,9 +43,9 @@ void i2c_init()
} }
/* /*
* reads data from devices which use a single-byte address * reads data from devices which use a single-byte address register
*/ */
int8_t i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len) int8_t i2c_read_addr1b(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len)
{ {
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while((I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET) && timeout--); while((I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET) && timeout--);
@ -56,12 +56,12 @@ int8_t i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--);
if (!timeout) return -2; if (!timeout) return -2;
I2C_Send7bitAddress(I2C1, devaddr, I2C_Direction_Transmitter); I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Transmitter);
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) && timeout--);
if (!timeout) return -3; if (!timeout) return -3;
I2C_SendData(I2C1, addr); I2C_SendData(I2C1, reg);
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) && timeout--);
if (!timeout) return -4; if (!timeout) return -4;
@ -71,7 +71,7 @@ int8_t i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--);
if (!timeout) return -5; if (!timeout) return -5;
I2C_Send7bitAddress(I2C1, devaddr, I2C_Direction_Receiver); I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Receiver);
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) && timeout--);
if (!timeout) return -6; if (!timeout) return -6;
@ -92,7 +92,15 @@ int8_t i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len
return 0; return 0;
} }
int8_t i2c_write_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len) uint8_t i2c_read_reg_8b(uint8_t addr, uint8_t reg)
{
uint8_t dat;
i2c_read_addr1b(addr, reg, &dat, 1);
return dat;
}
int8_t i2c_write_addr1b(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len)
{ {
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while((I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET) && timeout--); while((I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET) && timeout--);
@ -103,12 +111,12 @@ int8_t i2c_write_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t le
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--);
if (!timeout) return -2; if (!timeout) return -2;
I2C_Send7bitAddress(I2C1, devaddr, I2C_Direction_Transmitter); I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Transmitter);
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) && timeout--);
if (!timeout) return -3; if (!timeout) return -3;
I2C_SendData(I2C1, addr); I2C_SendData(I2C1, reg);
timeout = I2C_TIMEOUT; timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) && timeout--);
if (!timeout) return -4; if (!timeout) return -4;
@ -126,7 +134,12 @@ int8_t i2c_write_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t le
return 0; return 0;
} }
int8_t i2c_ack_poll(uint8_t devaddr) void i2c_write_reg_8b(uint8_t addr, uint8_t reg, uint8_t dat)
{
i2c_write_addr1b(addr, reg, &dat, 1);
}
int8_t i2c_ack_poll(uint8_t addr)
{ {
int8_t addr_match = 0; int8_t addr_match = 0;
@ -139,7 +152,7 @@ int8_t i2c_ack_poll(uint8_t devaddr)
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) && timeout--);
if (!timeout) return -2; if (!timeout) return -2;
I2C_Send7bitAddress(I2C1, devaddr, I2C_Direction_Receiver); I2C_Send7bitAddress(I2C1, addr, I2C_Direction_Receiver);
timeout = I2C_TIMEOUT_ACK_POLL; timeout = I2C_TIMEOUT_ACK_POLL;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) && timeout--); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) && timeout--);
if (!timeout) { if (!timeout) {

View File

@ -0,0 +1,26 @@
/*
* Created on: Jul 27, 2024
*/
#ifndef USER_SRC_I2C_H_
#define USER_SRC_I2C_H_
#include <stdint.h>
void i2c_init();
int8_t i2c_read_addr1b(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
int8_t i2c_write_addr1b(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len);
uint8_t i2c_read_reg_8b(uint8_t addr, uint8_t reg);
void i2c_write_reg_8b(uint8_t addr, uint8_t reg, uint8_t dat);
int8_t i2c_ack_poll(uint8_t devaddr);
#endif /* USER_SRC_I2C_H_ */

View File

@ -6,8 +6,8 @@
#define FL3729_CS_OUTPUTS 15 #define FL3729_CS_OUTPUTS 15
#define LED_INIT_CURRENT 0
#define LED_INIT_CURRENT 0
#define LED_INIT_SCALING 0xff #define LED_INIT_SCALING 0xff