Compare commits

..

2 Commits

Author SHA1 Message Date
0805b387ee create scaffolding for i2c slave code 2025-03-24 18:45:37 -05:00
4f2d9dbfb4 normalize the code 2025-03-24 18:38:05 -05:00
9 changed files with 240 additions and 212 deletions

79
src/hall_driver.c Normal file
View File

@ -0,0 +1,79 @@
#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 = 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));
}
/**
* 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);
}
}

View File

@ -3,84 +3,14 @@
#include <ch32v00x.h> #include <ch32v00x.h>
void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast"))); extern volatile uint8_t adcFlag;
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;
/** /**
* The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected * The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected
* conversion. * conversion.
*/ */
void ADC1_IRQHandler() { void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast")));
// UartStringSend("interupt!!!\r\n");
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) { void ADCConfig(void);
adcFlag = 1;
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) {
adcFlag = 1;
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
}
#endif #endif

2
src/i2c_slave.c Normal file
View File

@ -0,0 +1,2 @@
#include "i2c_slave.h"

8
src/i2c_slave.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef I2C_SLAVE_H
#define I2C_SLAVE_H
#include <ch32v00x.h>
#endif /* I2C_SLAVE_H */

View File

@ -13,7 +13,7 @@ void init_button(void);
int main(void) int main(void)
{ {
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
SystemCoreClockUpdate(); SystemCoreClockUpdate();
Delay_Init(); Delay_Init();
@ -106,8 +106,5 @@ void NMI_Handler(void) {
void HardFault_Handler(void) void HardFault_Handler(void)
{ {
SendNeoPixelColor(0, 255, 0); SendNeoPixelColor(0, 255, 0);
while (1) while (1);
{
}
} }

81
src/neopixel_driver.c Normal file
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);
}

View File

@ -6,84 +6,10 @@
// #define CLK_24MHZ // #define CLK_24MHZ
#define CLK_8MHZ #define CLK_8MHZ
void SPINeoPixelDriverInit(void) { void SPINeoPixelDriverInit(void);
GPIO_InitTypeDef GPIO_InitStructure={0}; void SPISendByte(uint8_t byte);
SPI_InitTypeDef SPI_InitStructure={0}; void SendNeoPixel0();
void SendNeoPixelByte(uint8_t byte);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_SPI1, ENABLE); void SendNeoPixelColor(uint8_t red, uint8_t green, uint8_t blue);
// 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);
}
#endif // NEOPIXEL_DRIVER_H #endif // NEOPIXEL_DRIVER_H

56
src/uart_driver.c Normal file
View File

@ -0,0 +1,56 @@
#include "uart_driver.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;
}
}

View File

@ -3,59 +3,8 @@
#include <ch32v00x.h> #include <ch32v00x.h>
void USARTx_CFG(void) { void USARTx_CFG(void);
GPIO_InitTypeDef GPIO_InitStructure = {0}; void UartBufferSend(uint8_t* buffer, uint16_t length);
USART_InitTypeDef USART_InitStructure = {0}; void UartStringSend(char* string);
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 #endif // UART_DRIVER_H