Compare commits

..
7 Commits
Author SHA1 Message Date
mitchell 36f179566b idek 2025-11-17 12:07:01 -06:00
mitchell 3e063e12b0 not working i2c slave 2025-04-24 14:57:30 -05:00
mitchell 4a2c9d01b2 Update monitor speed 2025-04-16 15:30:16 -05:00
mitchell 488e6ea813 fix hall driver?? 2025-04-16 08:39:39 -05:00
mitchell 736cd974ad Start work on i2c (not working) 2025-04-16 08:23:01 -05:00
mitchell 0805b387ee create scaffolding for i2c slave code 2025-03-24 18:45:37 -05:00
mitchell 4f2d9dbfb4 normalize the code 2025-03-24 18:38:05 -05:00
10 changed files with 259 additions and 227 deletions
+4 -2
View File
@@ -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"
}
+1
View File
@@ -12,3 +12,4 @@
platform = ch32v
board = genericCH32V003F4P6
framework = noneos-sdk
monitor_speed = 115200
+69
View File
@@ -0,0 +1,69 @@
#include "hall_driver.h"
volatile uint8_t adcFlag = 0;
void ADCConfig(void) {
ADC_InitTypeDef ADC_InitStructure = {0};
GPIO_InitTypeDef GPIO_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // multiple channels
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigInjecConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 4;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_InjectedSequencerLengthConfig(ADC1, 4);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_241Cycles);
ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE);
NVIC_EnableIRQ(ADC_IRQn);
ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
/**
* The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected
* conversion.
*/
void ADC1_IRQHandler() {
// UartStringSend("interupt!!!\r\n");
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) {
adcFlag = 1;
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
}
+4 -74
View File
@@ -3,84 +3,14 @@
#include <ch32v00x.h>
void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast")));
void ADCConfig(void) {
ADC_InitTypeDef ADC_InitStructure = {0};
GPIO_InitTypeDef GPIO_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOD, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // multiple channels
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigInjecConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_InjectedSequencerLengthConfig(ADC1, 4);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_241Cycles);
ADC_InjectedChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_241Cycles);
ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE);
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// ADC_Calibration_Vol(ADC1, ADC_CALVOL_75PERCENT);
// ADC_Calibration_Vol(ADC1, ADC_CALVOL_50PERCENT);
ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1));
}
int adcFlag = 0;
extern volatile uint8_t adcFlag;
/**
* The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected
* conversion.
*/
void ADC1_IRQHandler() {
// UartStringSend("interupt!!!\r\n");
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) {
adcFlag = 1;
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) {
adcFlag = 1;
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
}
void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast")));
void ADCConfig(void);
#endif
+73
View File
@@ -0,0 +1,73 @@
#include "ch32v00x.h"
#include "i2c_slave.h"
#include <stdio.h>
// https://pallavaggarwal.in/2023/10/05/ch32v003-programming-i2c-eeprom/
void i2c_init() {
// Enable clocks
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
// Set PB6/PB7 to AF Open-Drain
GPIOB->CFGLR &= ~((0xF << (4 * 6)) | (0xF << (4 * 7)));
GPIOB->CFGLR |= (0xF << (4 * 6)) | (0xF << (4 * 7)); // 0xF = AF OD 50MHz
I2C1->CTLR1 = I2C_CTLR1_SWRST;
I2C1->CTLR1 = 0;
I2C1->OADDR1 = (address << 1) & 0xFE;
I2C1->OADDR1 |= I2C_OADDR1_ADDMODE; // 7-bit addr
I2C1->CTLR2 = 36; // 36 MHz APB1
I2C1->STAR1 = 0;
I2C1->STAR2 = 0;
I2C1->CTLR1 |= I2C_CTLR1_ACK | I2C_CTLR1_PE;
I2C1->CTLR1 |= I2C_CTLR1_ITBUFEN | I2C_CTLR1_ITEVTEN;
NVIC_EnableIRQ(I2C1_EV_IRQn);
}
void I2C1_EV_IRQHandler(void) {
uint16_t STAR1, STAR2 __attribute__((unused));
STAR1 = I2C1->STAR1;
STAR2 = I2C1->STAR2;
if (STAR1 & I2C_STAR1_ADDR) { // Start event
printf("Start Event\r\n");
}
if (STAR1 & I2C_STAR1_RXNE) { // Write event
printf("Write Event\r\n");
}
if (STAR1 & I2C_STAR1_TXE) { // Read event
printf("Read Event\r\n");
}
if (STAR1 & I2C_STAR1_STOPF) { // Stop event
I2C1->CTLR1 &= ~(I2C_CTLR1_STOP); // Clear stop
printf("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
printf("Bus error\r\n");
}
if (STAR1 & I2C_STAR1_ARLO) { // Arbitration lost error
I2C1->STAR1 &= ~(I2C_STAR1_ARLO); // Clear error
printf("Arbitration lost error\r\n");
}
if (STAR1 & I2C_STAR1_AF) { // Acknowledge failure
I2C1->STAR1 &= ~(I2C_STAR1_AF); // Clear error
printf("Ack Failure\r\n");
}
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef I2C_SLAVE_H
#define I2C_SLAVE_H
#include <ch32v00x.h>
#define I2C_SLAVE_ADDR 0x33
void i2c_init();
void I2C1_EV_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void I2C1_ER_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif /* I2C_SLAVE_H */
+8 -11
View File
@@ -2,8 +2,8 @@
#include <debug.h>
#include "neopixel_driver.h"
// #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")));
@@ -13,19 +13,21 @@ void init_button(void);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
SystemCoreClockUpdate();
Delay_Init();
// allow some time to enter flash mode, in case something gets screwed up
Delay_Ms(1000);
USARTx_CFG();
// UartStringSend("Hello World!\r\n");
USART_Printf_Init(115200);
printf("Hello World!\r\n");
SPINeoPixelDriverInit();
// BBNeoPixelDriverInit();
i2c_init();
init_button();
ADCConfig();
@@ -56,9 +58,7 @@ 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);
// printf("%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]);
for (int i = 0; i < 4; i++) {
if (adc_readings[i] > adc_calibration_values[i] + 5) {
@@ -106,8 +106,5 @@ void NMI_Handler(void) {
void HardFault_Handler(void)
{
SendNeoPixelColor(0, 255, 0);
while (1)
{
}
while (1);
}
+81
View File
@@ -0,0 +1,81 @@
#include "neopixel_driver.h"
void SPINeoPixelDriverInit(void) {
GPIO_InitTypeDef GPIO_InitStructure={0};
SPI_InitTypeDef SPI_InitStructure={0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_SPI1, ENABLE);
// init MOSI on PC6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
#ifdef CLK_24MHZ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
#endif
#ifdef CLK_8MHZ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
#endif
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPISendByte(0);
}
void SPISendByte(uint8_t byte) {
// wait while flag is zero or TX buffer not empty
// while( SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
}
void SendNeoPixel0() {
#ifdef CLK_24MHZ
// 420ns High at 24MHz
SPISendByte(0b11000000);
#endif
#ifdef CLK_8MHZ
// ????????
SPISendByte(0b10000000);
#endif
}
void SendNeoPixel1() {
#ifdef CLK_24MHZ
// 750ns High at 24MHz
SPISendByte(0b11110000);
#endif
#ifdef CLK_8MHZ
// ????????
SPISendByte(0b1110000);
#endif
}
void SendNeoPixelByte(uint8_t byte) {
for (int i = 7; i >= 0; i--) {
if ((byte >> i) & 1) {
SendNeoPixel1();
} else {
SendNeoPixel0();
}
}
}
void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue) {
// Send in GRB order, MSB first
SendNeoPixelByte(green);
SendNeoPixelByte(red);
SendNeoPixelByte(blue);
}
+5 -79
View File
@@ -6,84 +6,10 @@
// #define CLK_24MHZ
#define CLK_8MHZ
void SPINeoPixelDriverInit(void) {
GPIO_InitTypeDef GPIO_InitStructure={0};
SPI_InitTypeDef SPI_InitStructure={0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_SPI1, ENABLE);
// init MOSI on PC6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
#ifdef CLK_24MHZ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
#endif
#ifdef CLK_8MHZ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
#endif
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPISendByte(0);
}
void SPISendByte(uint8_t byte) {
// wait while flag is zero or TX buffer not empty
// while( SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
}
void SendNeoPixel0() {
#ifdef CLK_24MHZ
// 420ns High at 24MHz
SPISendByte(0b11000000);
#endif
#ifdef CLK_8MHZ
// ????????
SPISendByte(0b10000000);
#endif
}
void SendNeoPixel1() {
#ifdef CLK_24MHZ
// 750ns High at 24MHz
SPISendByte(0b11110000);
#endif
#ifdef CLK_8MHZ
// ????????
SPISendByte(0b1110000);
#endif
}
void SendNeoPixelByte(uint8_t byte) {
for (int i = 7; i >= 0; i--) {
if ((byte >> i) & 1) {
SendNeoPixel1();
} else {
SendNeoPixel0();
}
}
}
void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue) {
// Send in GRB order, MSB first
SendNeoPixelByte(green);
SendNeoPixelByte(red);
SendNeoPixelByte(blue);
}
void SPINeoPixelDriverInit(void);
void SPISendByte(uint8_t byte);
void SendNeoPixel0();
void SendNeoPixelByte(uint8_t byte);
void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue);
#endif // NEOPIXEL_DRIVER_H
-61
View File
@@ -1,61 +0,0 @@
#ifndef UART_DRIVER_H
#define UART_DRIVER_H
#include <ch32v00x.h>
void USARTx_CFG(void) {
GPIO_InitTypeDef GPIO_InitStructure = {0};
USART_InitTypeDef USART_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);
/* USART1 TX-->D.5 RX-->D.6 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);;
USART_Cmd(USART1, ENABLE);
}
void UartBufferSend(uint8_t* buffer, uint16_t length)
{
uint16_t i = 0;
for(i =0; i < length; i++)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* waiting for sending finish */
USART_SendData(USART1, buffer[i]);
}
}
void UartStringSend(char* string) {
uint16_t i = 0;
while (string[i] != '\0') {
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); /* waiting for sending finish */
USART_SendData(USART1, string[i]);
i += 1;
}
}
#endif // UART_DRIVER_H