Compare commits

..

No commits in common. "0805b387eeba1e09d232d5ef3e21c20381f6e4b3" and "fed8829bba4a72c215bee9829b2b4dce86bf65fe" have entirely different histories.

9 changed files with 212 additions and 240 deletions

View File

@ -1,79 +0,0 @@
#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,14 +3,84 @@
#include <ch32v00x.h>
extern volatile uint8_t adcFlag;
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;
/**
* The function ADC1_IRQHandler handles the interrupt for ADC1 and prints the value of the injected
* conversion.
*/
void ADC1_IRQHandler() __attribute__((interrupt("WCH-Interrupt-fast")));
void ADCConfig(void);
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);
}
}
#endif

View File

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

View File

@ -1,8 +0,0 @@
#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)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
@ -106,5 +106,8 @@ void NMI_Handler(void) {
void HardFault_Handler(void)
{
SendNeoPixelColor(0, 255, 0);
while (1);
while (1)
{
}
}

View File

@ -1,81 +0,0 @@
#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,10 +6,84 @@
// #define CLK_24MHZ
#define CLK_8MHZ
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);
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);
}
#endif // NEOPIXEL_DRIVER_H

View File

@ -1,56 +0,0 @@
#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,8 +3,59 @@
#include <ch32v00x.h>
void USARTx_CFG(void);
void UartBufferSend(uint8_t* buffer, uint16_t length);
void UartStringSend(char* string);
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