diff --git a/.vscode/settings.json b/.vscode/settings.json index 527a059..3400d72 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,8 @@ { "files.associations": { "neopixel_driver.h": "c", - "bit_bang_neopixel.h": "c" - } + "bit_bang_neopixel.h": "c", + "i2c_slave.h": "c" + }, + "idf.portWin": "COM9" } \ No newline at end of file diff --git a/src/i2c_slave.c b/src/i2c_slave.c index dff3d5e..6e22ac2 100644 --- a/src/i2c_slave.c +++ b/src/i2c_slave.c @@ -1,2 +1,82 @@ #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"); + } +} diff --git a/src/i2c_slave.h b/src/i2c_slave.h index 09bdb8e..2108757 100644 --- a/src/i2c_slave.h +++ b/src/i2c_slave.h @@ -3,6 +3,11 @@ #include +#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 */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index e46b2da..7f8e055 100755 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ // #include "bit_bang_neopixel.h" #include "uart_driver.h" #include "hall_driver.h" +#include "i2c_slave.h" void NMI_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); USARTx_CFG(); - // UartStringSend("Hello World!\r\n"); + UartStringSend("Hello World!\r\n"); SPINeoPixelDriverInit(); // BBNeoPixelDriverInit(); + i2c_init(); + init_button(); ADCConfig(); @@ -57,8 +60,8 @@ int main(void) adcFlag = 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]); - UartStringSend(buffer); + // 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); for (int i = 0; i < 4; i++) { if (adc_readings[i] > adc_calibration_values[i] + 5) { diff --git a/src/uart_driver.c b/src/uart_driver.c index a1d5c75..331d839 100644 --- a/src/uart_driver.c +++ b/src/uart_driver.c @@ -17,7 +17,7 @@ void USARTx_CFG(void) { GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 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_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No;