dc33-retro-tech-addon/firmware/retro_tech_fw/code/ch32x035_it.c
true f8a987a592 soft I2C implemented, LEDs are now lighting
copied over and modified for use my CH59x soft I2C.

CH32X GPIO does not have an open drain mode, so I have to implement this with RMW to the mode register. After doing this and adding missing functions needed for existing code, LED writes and EEPROM reads and writes.

using the AWU for system timing so the clock speed can change.

existing issues:
- changing clock speed divider in soft i2c routines causes the system to crash
- ADC seems to be reading, but result isn't being used
2025-07-24 06:19:24 -07:00

90 lines
2.0 KiB
C

/********************************** (C) COPYRIGHT *******************************
* File Name : ch32x035_it.c
* Author : WCH
* Version : V1.0.0
* Date : 2024/10/28
* Description : Main Interrupt Service Routines.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32x035_it.h"
#include "ch32x035.h"
#include "src/adc.h"
#include "src/btn.h"
#include "src/led.h"
/* __attribute__((interrupt("WCH-Interrupt-fast"))); */
void NMI_Handler(void);
void HardFault_Handler(void);
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void AWU_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*********************************************************************
* @fn NMI_Handler
*
* @brief This function handles NMI exception.
*
* @return none
*/
void NMI_Handler(void)
{
while (1);
}
/*********************************************************************
* @fn HardFault_Handler
*
* @brief This function handles Hard Fault exception.
*
* @return none
*/
void HardFault_Handler(void)
{
NVIC_SystemReset();
while (1);
}
/* retro tech code
*/
volatile uint16_t ticnt;
volatile uint32_t uptime;
void AWU_IRQHandler(void)
{
// clear interrupt flag
EXTI->INTFR = EXTI_INTF_INTF27;
if (++ticnt > 0x1ff) {
ticnt = 0;
uptime++;
}
// send new LEDs (1/128 duty)
if ((ticnt & 0x3) == 0) {
led_send();
}
// process buttons (1/512 duty)
btn_poll();
// process ADC (1/256 duty)
if ((ticnt & 0x1) == 0) {
adc_convert();
}
if ((ticnt & 0x1) == 1) {
adc_read();
}
// clear comparison flag
SysTick->SR = 0;
return;
}