Compare commits
3
Commits
488e6ea813
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36f179566b | ||
|
|
3e063e12b0 | ||
|
|
4a2c9d01b2 |
@@ -12,3 +12,4 @@
|
||||
platform = ch32v
|
||||
board = genericCH32V003F4P6
|
||||
framework = noneos-sdk
|
||||
monitor_speed = 115200
|
||||
|
||||
+2
-8
@@ -44,15 +44,9 @@ void ADCConfig(void) {
|
||||
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);
|
||||
NVIC_EnableIRQ(ADC_IRQn);
|
||||
ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
|
||||
ADC_ResetCalibration(ADC1);
|
||||
|
||||
+25
-34
@@ -1,41 +1,32 @@
|
||||
#include "ch32v00x.h"
|
||||
#include "i2c_slave.h"
|
||||
#include "uart_driver.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// https://pallavaggarwal.in/2023/10/05/ch32v003-programming-i2c-eeprom/
|
||||
void i2c_init() {
|
||||
GPIO_InitTypeDef GPIO_InitStructure={0};
|
||||
I2C_InitTypeDef I2C_InitTSturcture={0};
|
||||
// Enable clocks
|
||||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
||||
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
|
||||
|
||||
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );
|
||||
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
|
||||
// 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
|
||||
|
||||
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 );
|
||||
I2C1->CTLR1 = I2C_CTLR1_SWRST;
|
||||
I2C1->CTLR1 = 0;
|
||||
|
||||
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 );
|
||||
I2C1->OADDR1 = (address << 1) & 0xFE;
|
||||
I2C1->OADDR1 |= I2C_OADDR1_ADDMODE; // 7-bit addr
|
||||
|
||||
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;
|
||||
I2C1->CTLR2 = 36; // 36 MHz APB1
|
||||
|
||||
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);
|
||||
I2C1->STAR1 = 0;
|
||||
I2C1->STAR2 = 0;
|
||||
|
||||
I2C_Init( I2C1, &I2C_InitTSturcture );
|
||||
I2C1->CTLR1 |= I2C_CTLR1_ACK | I2C_CTLR1_PE;
|
||||
I2C1->CTLR1 |= I2C_CTLR1_ITBUFEN | I2C_CTLR1_ITEVTEN;
|
||||
|
||||
I2C_Cmd( I2C1, ENABLE );
|
||||
|
||||
I2C_AcknowledgeConfig( I2C1, ENABLE );
|
||||
NVIC_EnableIRQ(I2C1_EV_IRQn);
|
||||
}
|
||||
|
||||
void I2C1_EV_IRQHandler(void) {
|
||||
@@ -44,20 +35,20 @@ void I2C1_EV_IRQHandler(void) {
|
||||
STAR2 = I2C1->STAR2;
|
||||
|
||||
if (STAR1 & I2C_STAR1_ADDR) { // Start event
|
||||
UartStringSend("Start Event\r\n");
|
||||
printf("Start Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_RXNE) { // Write event
|
||||
UartStringSend("Write Event\r\n");
|
||||
printf("Write Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_TXE) { // Read event
|
||||
UartStringSend("Read Event\r\n");
|
||||
printf("Read Event\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_STOPF) { // Stop event
|
||||
I2C1->CTLR1 &= ~(I2C_CTLR1_STOP); // Clear stop
|
||||
UartStringSend("Stop Event\r\n");
|
||||
printf("Stop Event\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,17 +57,17 @@ void I2C1_ER_IRQHandler(void) {
|
||||
|
||||
if (STAR1 & I2C_STAR1_BERR) { // Bus error
|
||||
I2C1->STAR1 &= ~(I2C_STAR1_BERR); // Clear error
|
||||
UartStringSend("Bus error\r\n");
|
||||
printf("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");
|
||||
printf("Arbitration lost error\r\n");
|
||||
}
|
||||
|
||||
if (STAR1 & I2C_STAR1_AF) { // Acknowledge failure
|
||||
I2C1->STAR1 &= ~(I2C_STAR1_AF); // Clear error
|
||||
UartStringSend("Ack Failure\r\n");
|
||||
printf("Ack Failure\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -7,7 +7,8 @@
|
||||
|
||||
void i2c_init();
|
||||
|
||||
void I2C1_EV_IRQHandler(void) __attribute__((interrupt));
|
||||
void I2C1_ER_IRQHandler(void) __attribute__((interrupt));
|
||||
|
||||
void I2C1_EV_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void I2C1_ER_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
|
||||
#endif /* I2C_SLAVE_H */
|
||||
+3
-6
@@ -2,7 +2,6 @@
|
||||
#include <debug.h>
|
||||
#include "neopixel_driver.h"
|
||||
// #include "bit_bang_neopixel.h"
|
||||
#include "uart_driver.h"
|
||||
#include "hall_driver.h"
|
||||
#include "i2c_slave.h"
|
||||
|
||||
@@ -21,8 +20,8 @@ int main(void)
|
||||
// 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();
|
||||
@@ -59,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) {
|
||||
|
||||
@@ -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 = 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;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user