improve i2c; add gpio init functions; some minor fixups and cleanup

i2c is totally fucking broken though and needs to be replaced before con.
This commit is contained in:
true 2024-07-28 16:16:03 -07:00
parent 7a11164f8d
commit 65a034de67
8 changed files with 123 additions and 7 deletions

View File

@ -38,6 +38,50 @@ void systick_init(void)
NVIC_EnableIRQ(SysTicK_IRQn); // enable interrupt NVIC_EnableIRQ(SysTicK_IRQn); // enable interrupt
} }
void gpio_init()
{
GPIO_InitTypeDef gpio = {0};
gpio.GPIO_Speed = GPIO_Speed_2MHz;
// Soft I2C, USART TX/RX; currently unused
gpio.GPIO_Mode = GPIO_Mode_IPD;
gpio.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_Init(GPIOD, &gpio);
gpio.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOA, &gpio);
// lightsense LED anode
gpio.GPIO_Mode = GPIO_Mode_Out_OD;
gpio.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOD, &gpio);
// unused pins
gpio.GPIO_Mode = GPIO_Mode_IPD;
gpio.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_7;
GPIO_Init(GPIOC, &gpio);
gpio.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &gpio);
// I2C will be handled by the driver
// IS_SDB IS31FL3729 shutdown pin (active low)
GPIOC->BCR = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_Out_OD;
gpio.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOC, &gpio);
// BTN1, BTN2 will be handled by button handler
// WP for EEPROM
GPIOC->BSHR = GPIO_Pin_6;
gpio.GPIO_Mode = GPIO_Mode_Out_OD;
gpio.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOC, &gpio);
}
/********************************************************************* /*********************************************************************
* @fn main * @fn main
* *
@ -57,6 +101,9 @@ int main(void)
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1, ENABLE); RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1, ENABLE);
// configure gpio pins
gpio_init();
// get saved settings // get saved settings
i2c_init(); i2c_init();
userconf_load(); userconf_load();

View File

@ -18,6 +18,11 @@ void is31fl3729_init(uint8_t i2c_addr, uint8_t config, uint8_t global_current)
{ {
uint8_t buf; uint8_t buf;
// enable device
#ifdef FL3729_SDB_PORT
FL3729_SDB_PORT->BSHR = FL3729_SDB_PIN;
#endif
// reset config registers // reset config registers
buf = FL3729_RESET_VALUE; buf = FL3729_RESET_VALUE;
i2c_write_addr1b(i2c_addr, FL3729_REG_RESET, &buf, 1); i2c_write_addr1b(i2c_addr, FL3729_REG_RESET, &buf, 1);

View File

@ -7,10 +7,14 @@
#include <ch32v00x.h>
#include <stdint.h> #include <stdint.h>
#define FL3729_SDB_PORT GPIOC
#define FL3729_SDB_PIN GPIO_Pin_3
#define FL3729_BASE_ADDR 0x68 #define FL3729_BASE_ADDR 0x68
#define FL3729_ADPIN_GND 0 #define FL3729_ADPIN_GND 0

View File

@ -13,13 +13,15 @@
// deadzone fudge factors for potentionmeter. test on actual badge
// and see if necessary, or if pot hardware + adc periph is good enough
#define POT_LO 40 #define POT_LO 40
#define POT_HI (1024-POT_LO) #define POT_HI (1024-POT_LO)
uint16_t adc_idx;
uint16_t adc_val[16]; uint16_t adc_val[16];
uint16_t adc_val_idx;
uint16_t adc_avg; uint16_t adc_avg;
uint16_t lsens_val; uint16_t lsens_val;
@ -80,9 +82,9 @@ void adc_convert()
void adc_read() void adc_read()
{ {
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc_val[adc_idx++] = ADC_GetConversionValue(ADC1); adc_val[adc_val_idx++] = ADC_GetConversionValue(ADC1);
adc_idx &= 0x0f; adc_val_idx &= 0x0f;
if (!adc_idx) adc_calc_avg(); if (!adc_val_idx) adc_calc_avg();
if (ADC1->CTLR1 & ADC_JAUTO) { if (ADC1->CTLR1 & ADC_JAUTO) {
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_JEOC)); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_JEOC));

View File

@ -14,6 +14,7 @@
#include <ch32v00x.h> #include <ch32v00x.h>
#include <stdint.h> #include <stdint.h>
#include "eeprom.h"
#include "i2c.h" #include "i2c.h"
@ -52,5 +53,26 @@ void eeprom_write_bytes(uint8_t page, uint8_t addr, uint8_t *data, uint8_t len)
page |= EEPROM_BASE_ADDR; // add address to page page |= EEPROM_BASE_ADDR; // add address to page
page <<= 1; // make i2c address from page page <<= 1; // make i2c address from page
// disable systick interrupt
NVIC_DisableIRQ(SysTicK_IRQn);
// write the data
#ifdef EEPROM_WP_PORT
EEPROM_WP_PORT->BCR = EEPROM_WP_PIN;
#endif
while (i2c_ack_poll(page));
i2c_write_addr1b(page, addr, data, len); i2c_write_addr1b(page, addr, data, len);
while (i2c_ack_poll(page));
#ifdef EEPROM_WP_PORT
EEPROM_WP_PORT->BSHR = EEPROM_WP_PIN;
#endif
// clear systick timer
SysTick->CNT = 0;
SysTick->SR = 0;
// re-enable systick interrupt
NVIC_EnableIRQ(SysTicK_IRQn);
} }

View File

@ -9,10 +9,16 @@
#define USER_SRC_EEPROM_H_ #define USER_SRC_EEPROM_H_
#include <ch32v00x.h>
#include <stdint.h> #include <stdint.h>
#define EEPROM_WP_PORT GPIOC
#define EEPROM_WP_PIN GPIO_Pin_7
void eeprom_read_bytes(uint8_t page, uint8_t addr, uint8_t *data, uint8_t len); void eeprom_read_bytes(uint8_t page, uint8_t addr, uint8_t *data, uint8_t len);
void eeprom_write_bytes(uint8_t page, uint8_t addr, uint8_t *data, uint8_t len); void eeprom_write_bytes(uint8_t page, uint8_t addr, uint8_t *data, uint8_t len);

View File

@ -1,13 +1,23 @@
/* /*
* i2c.c * i2c.c
* *
* Created on: Jul 27, 2024 * routines more or less copied from WCH example code, then fucked around with to work.
* Author: true *
* these routines have serious issues.
* - any i2c issue will lock up the machine, and there's no timeout handlers
* - there's no error handling of any kind
* - the library code makes some serious assumptions re: flags
*
* this MUST be fixed before con.
*/ */
#include <ch32v00x.h> #include <ch32v00x.h>
#define I2C_TIMEOUT 0xffff;
void i2c_init() void i2c_init()
{ {
GPIO_InitTypeDef gpio = {0}; GPIO_InitTypeDef gpio = {0};
@ -87,5 +97,23 @@ void i2c_write_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len)
} }
} }
I2C_GenerateSTOP( I2C1, ENABLE ); I2C_GenerateSTOP(I2C1, ENABLE);
}
int8_t i2c_ack_poll(uint8_t devaddr)
{
int8_t addr_match = 0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1, devaddr, I2C_Direction_Receiver);
if (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
addr_match = -1;
I2C_GenerateSTOP(I2C1, ENABLE);
return addr_match;
} }

View File

@ -19,6 +19,8 @@ void i2c_init();
void i2c_read_addr1b(uint8_t devaddr, uint8_t addr, uint8_t *data, uint8_t len); 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); 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_ */ #endif /* USER_SRC_I2C_H_ */