Start work on i2c (not working)

This commit is contained in:
Mitchell Marino 2025-04-16 08:23:01 -05:00
parent 0805b387ee
commit 736cd974ad
5 changed files with 96 additions and 6 deletions

View File

@ -1,6 +1,8 @@
{ {
"files.associations": { "files.associations": {
"neopixel_driver.h": "c", "neopixel_driver.h": "c",
"bit_bang_neopixel.h": "c" "bit_bang_neopixel.h": "c",
} "i2c_slave.h": "c"
},
"idf.portWin": "COM9"
} }

View File

@ -1,2 +1,82 @@
#include "i2c_slave.h" #include "i2c_slave.h"
#include "uart_driver.h"
// https://pallavaggarwal.in/2023/10/05/ch32v003-programming-i2c-eeprom/
void i2c_init() {
GPIO_InitTypeDef GPIO_InitStructure={0};
I2C_InitTypeDef I2C_InitTSturcture={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC, &GPIO_InitStructure );
I2C_InitTSturcture.I2C_ClockSpeed = 100*1000;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitTSturcture.I2C_OwnAddress1 = I2C_SLAVE_ADDR;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
NVIC_EnableIRQ(I2C1_EV_IRQn); // Event interrupt
NVIC_SetPriority(I2C1_EV_IRQn, 2 << 4);
NVIC_EnableIRQ(I2C1_ER_IRQn); // Error interrupt
NVIC_SetPriority(I2C1_ER_IRQn, 2 << 4);
I2C_Init( I2C1, &I2C_InitTSturcture );
I2C_Cmd( I2C1, ENABLE );
I2C_AcknowledgeConfig( I2C1, ENABLE );
}
void I2C1_EV_IRQHandler(void) {
uint16_t STAR1, STAR2 __attribute__((unused));
STAR1 = I2C1->STAR1;
STAR2 = I2C1->STAR2;
if (STAR1 & I2C_STAR1_ADDR) { // Start event
UartStringSend("Start Event\r\n");
}
if (STAR1 & I2C_STAR1_RXNE) { // Write event
UartStringSend("Write Event\r\n");
}
if (STAR1 & I2C_STAR1_TXE) { // Read event
UartStringSend("Read Event\r\n");
}
if (STAR1 & I2C_STAR1_STOPF) { // Stop event
I2C1->CTLR1 &= ~(I2C_CTLR1_STOP); // Clear stop
UartStringSend("Stop Event\r\n");
}
}
void I2C1_ER_IRQHandler(void) {
uint16_t STAR1 = I2C1->STAR1;
if (STAR1 & I2C_STAR1_BERR) { // Bus error
I2C1->STAR1 &= ~(I2C_STAR1_BERR); // Clear error
UartStringSend("Bus error\r\n");
}
if (STAR1 & I2C_STAR1_ARLO) { // Arbitration lost error
I2C1->STAR1 &= ~(I2C_STAR1_ARLO); // Clear error
UartStringSend("Arbitration lost error\r\n");
}
if (STAR1 & I2C_STAR1_AF) { // Acknowledge failure
I2C1->STAR1 &= ~(I2C_STAR1_AF); // Clear error
UartStringSend("Ack Failure\r\n");
}
}

View File

@ -3,6 +3,11 @@
#include <ch32v00x.h> #include <ch32v00x.h>
#define I2C_SLAVE_ADDR 0x33
void i2c_init();
void I2C1_EV_IRQHandler(void) __attribute__((interrupt));
void I2C1_ER_IRQHandler(void) __attribute__((interrupt));
#endif /* I2C_SLAVE_H */ #endif /* I2C_SLAVE_H */

View File

@ -4,6 +4,7 @@
// #include "bit_bang_neopixel.h" // #include "bit_bang_neopixel.h"
#include "uart_driver.h" #include "uart_driver.h"
#include "hall_driver.h" #include "hall_driver.h"
#include "i2c_slave.h"
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
@ -21,11 +22,13 @@ int main(void)
Delay_Ms(1000); Delay_Ms(1000);
USARTx_CFG(); USARTx_CFG();
// UartStringSend("Hello World!\r\n"); UartStringSend("Hello World!\r\n");
SPINeoPixelDriverInit(); SPINeoPixelDriverInit();
// BBNeoPixelDriverInit(); // BBNeoPixelDriverInit();
i2c_init();
init_button(); init_button();
ADCConfig(); ADCConfig();
@ -57,8 +60,8 @@ int main(void)
adcFlag = 0; adcFlag = 0;
char buffer[64] = {0}; char buffer[64] = {0};
sprintf(buffer, "%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]); // sprintf(buffer, "%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]);
UartStringSend(buffer); // UartStringSend(buffer);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (adc_readings[i] > adc_calibration_values[i] + 5) { if (adc_readings[i] > adc_calibration_values[i] + 5) {

View File

@ -17,7 +17,7 @@ void USARTx_CFG(void) {
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Parity = USART_Parity_No;