Compare commits
5
Commits
0805b387ee
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36f179566b | ||
|
|
3e063e12b0 | ||
|
|
4a2c9d01b2 | ||
|
|
488e6ea813 | ||
|
|
736cd974ad |
Vendored
+4
-2
@@ -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"
|
||||
}
|
||||
@@ -12,3 +12,4 @@
|
||||
platform = ch32v
|
||||
board = genericCH32V003F4P6
|
||||
framework = noneos-sdk
|
||||
monitor_speed = 115200
|
||||
|
||||
+3
-13
@@ -34,7 +34,7 @@ void ADCConfig(void) {
|
||||
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_InitStructure.ADC_NbrOfChannel = 4;
|
||||
ADC_Init(ADC1, &ADC_InitStructure);
|
||||
|
||||
ADC_InjectedSequencerLengthConfig(ADC1, 4);
|
||||
@@ -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);
|
||||
@@ -72,8 +66,4 @@ void ADC1_IRQHandler() {
|
||||
adcFlag = 1;
|
||||
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
|
||||
}
|
||||
if (ADC_GetITStatus(ADC1, ADC_IT_JEOC) == SET) {
|
||||
adcFlag = 1;
|
||||
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
|
||||
#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 */
|
||||
+6
-6
@@ -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")));
|
||||
@@ -20,12 +20,14 @@ 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();
|
||||
|
||||
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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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