Compare commits
No commits in common. "3e063e12b0d14bf40818771150f65388e83680ca" and "488e6ea8132fa417735a0fd1e9297c7c26e94251" have entirely different histories.
3e063e12b0
...
488e6ea813
@ -12,4 +12,3 @@
|
||||
platform = ch32v
|
||||
board = genericCH32V003F4P6
|
||||
framework = noneos-sdk
|
||||
monitor_speed = 115200
|
||||
|
||||
@ -44,9 +44,15 @@ void ADCConfig(void) {
|
||||
ADC_InjectedChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_241Cycles);
|
||||
ADC_ExternalTrigInjectedConvCmd(ADC1, DISABLE);
|
||||
|
||||
NVIC_EnableIRQ(ADC_IRQn);
|
||||
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);
|
||||
|
||||
@ -1,23 +1,23 @@
|
||||
#include "i2c_slave.h"
|
||||
#include <stdio.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);
|
||||
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_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);
|
||||
GPIO_Init( GPIOC, &GPIO_InitStructure );
|
||||
|
||||
I2C_InitTSturcture.I2C_ClockSpeed = 100*1000;
|
||||
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
|
||||
@ -31,11 +31,11 @@ void i2c_init() {
|
||||
NVIC_EnableIRQ(I2C1_ER_IRQn); // Error interrupt
|
||||
NVIC_SetPriority(I2C1_ER_IRQn, 2 << 4);
|
||||
|
||||
I2C_Init(I2C1, &I2C_InitTSturcture);
|
||||
I2C_Init( I2C1, &I2C_InitTSturcture );
|
||||
|
||||
I2C_Cmd(I2C1, ENABLE);
|
||||
I2C_Cmd( I2C1, ENABLE );
|
||||
|
||||
I2C_AcknowledgeConfig(I2C1, ENABLE);
|
||||
I2C_AcknowledgeConfig( I2C1, ENABLE );
|
||||
}
|
||||
|
||||
void I2C1_EV_IRQHandler(void) {
|
||||
@ -44,20 +44,20 @@ void I2C1_EV_IRQHandler(void) {
|
||||
STAR2 = I2C1->STAR2;
|
||||
|
||||
if (STAR1 & I2C_STAR1_ADDR) { // Start event
|
||||
printf("Start Event\r\n");
|
||||
UartStringSend("Start Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_RXNE) { // Write event
|
||||
printf("Write Event\r\n");
|
||||
UartStringSend("Write Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_TXE) { // Read event
|
||||
printf("Read Event\r\n");
|
||||
UartStringSend("Read Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_STOPF) { // Stop event
|
||||
I2C1->CTLR1 &= ~(I2C_CTLR1_STOP); // Clear stop
|
||||
printf("Stop Event\r\n");
|
||||
UartStringSend("Stop Event\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,17 +66,17 @@ void I2C1_ER_IRQHandler(void) {
|
||||
|
||||
if (STAR1 & I2C_STAR1_BERR) { // Bus error
|
||||
I2C1->STAR1 &= ~(I2C_STAR1_BERR); // Clear error
|
||||
printf("Bus error\r\n");
|
||||
UartStringSend("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");
|
||||
UartStringSend("Arbitration lost error\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_AF) { // Acknowledge failure
|
||||
I2C1->STAR1 &= ~(I2C_STAR1_AF); // Clear error
|
||||
printf("Ack Failure\r\n");
|
||||
UartStringSend("Ack Failure\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#include <debug.h>
|
||||
#include "neopixel_driver.h"
|
||||
// #include "bit_bang_neopixel.h"
|
||||
#include "uart_driver.h"
|
||||
#include "hall_driver.h"
|
||||
#include "i2c_slave.h"
|
||||
|
||||
@ -20,8 +21,8 @@ int main(void)
|
||||
// allow some time to enter flash mode, in case something gets screwed up
|
||||
Delay_Ms(1000);
|
||||
|
||||
USART_Printf_Init(115200);
|
||||
printf("Hello World!\r\n");
|
||||
USARTx_CFG();
|
||||
UartStringSend("Hello World!\r\n");
|
||||
|
||||
SPINeoPixelDriverInit();
|
||||
// BBNeoPixelDriverInit();
|
||||
@ -58,7 +59,9 @@ int main(void)
|
||||
|
||||
adcFlag = 0;
|
||||
|
||||
// printf("%d\t%d\t%d\t%d\r\n", adc_readings[0], adc_readings[1], adc_readings[2], adc_readings[3]);
|
||||
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);
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (adc_readings[i] > adc_calibration_values[i] + 5) {
|
||||
|
||||
56
src/uart_driver.c
Normal file
56
src/uart_driver.c
Normal 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 = 115200;
|
||||
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;
|
||||
}
|
||||
}
|
||||
10
src/uart_driver.h
Executable file
10
src/uart_driver.h
Executable file
@ -0,0 +1,10 @@
|
||||
#ifndef UART_DRIVER_H
|
||||
#define UART_DRIVER_H
|
||||
|
||||
#include <ch32v00x.h>
|
||||
|
||||
void USARTx_CFG(void);
|
||||
void UartBufferSend(uint8_t* buffer, uint16_t length);
|
||||
void UartStringSend(char* string);
|
||||
|
||||
#endif // UART_DRIVER_H
|
||||
Loading…
Reference in New Issue
Block a user