感觉像是程序跑到某个错误地方了,复位mcu后功能都没有了,i2c没有ack,断电重新正常。
我贴中断代码给你看下,没有while死等的地方
void I2C0_IRQHandler(void)
{
uint32_t u32Status;
P05 = 1;
u32Status = I2C_GET_STATUS(I2C0);
if(I2C_GET_TIMEOUT_FLAG(I2C0))
{
if( g_u8SlvDataRx ) { // 20190922jone ++
I2C_ClearTimeoutFlag(I2C0);
g_u8SlvDataRx = 0;
}
//if( g_u8SlvDataTx ) {// 20190920 jone --
else if( g_u8SlvDataTx ) { // 20190922 jone ++
I2C_ClearTimeoutFlag(I2C0);
g_u8SlvDataTx = 0;
}
else {
/* Clear I2C0 Timeout Flag */
I2C_ClearTimeoutFlag(I2C0);
}
est_i2c_debug_2 = 0;
}
else
{
if(s_I2C0HandlerFn != NULL) {
s_I2C0HandlerFn(u32Status);
}
est_i2c_debug_2 ++;
if( (4 * 100000) <= est_i2c_debug_2 ) {
est_i2c_debug_2 = 0;
est_rst_i2c_in_irq();
}
}
P05 = 0;
}
void I2C_SlaveTRx(uint32_t u32Status)
{
if(u32Status == 0x60) /* Own SLA+W has been receive; ACK has been return */
{
g_u8SlvDataRx = 0;
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0x80) /* Previously address with own SLA address
Data has been received; ACK has been returned*/
{
slave_buff_rx[g_u8SlvDataRx] = (unsigned char) I2C_GET_DATA(I2C0);
g_u8SlvDataRx++;
if( 6 < g_u8SlvDataRx ) { // 20190922 jone ++
g_u8SlvDataRx = 0;
}
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0xA8) /* Own SLA+R has been receive; ACK has been return */
{ // r-1th:
g_u8SlvDataTx = 0;
I2C_SET_DATA(I2C0, slave_buff_tx[g_u8SlvDataTx]);
g_u8SlvDataTx++;
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0xb8) /* first byte has been send; ACK has been return, send next byte */
{ // r-2th:
I2C_SET_DATA(I2C0, slave_buff_tx[g_u8SlvDataTx]);
g_u8SlvDataTx++;
if( 6 < g_u8SlvDataTx ) { // 20190922 jone ++
g_u8SlvDataTx = 0;
}
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
//if( 6 <= g_u8SlvDataTx ) {
// g_u8SlvDataTx = 6;
//}
//else {
// I2C_SET_DATA(I2C0, slave_buff_tx[g_u8SlvDataTx]);
// g_u8SlvDataTx++;
//}
//I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0xc8) /* byte has been send; ACK has been return, send the last byte */
{ // 20191113 jone--
I2C_SET_DATA(I2C0, slave_buff_tx[g_u8SlvDataTx]);
g_u8SlvDataTx++;
if( 6 < g_u8SlvDataTx ) { // 20190922 jone ++
g_u8SlvDataTx = 0;
}
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0xC0) /* Data byte or last data in I2CDAT has been transmitted
Not ACK has been received */
{ // 3-th
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
if( 6 <= g_u8SlvDataTx ) {
slave_i2c_tx_len = g_u8SlvDataTx;
g_u8SlvDataTx = 0;
slave_i2c_tx ++;
i2c_isr_tx_end = 1;
}
}
else if(u32Status == 0x88) /* Previously addressed with own SLA address; NOT ACK has
been returned */
{
g_u8SlvDataRx = 0;
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else if(u32Status == 0xA0) /* A STOP or repeated START has been received while still
addressed as Slave/Receiver*/
{
//if( g_u8SlvDataRx ) { // 20190920 jone --
if( 6 <= g_u8SlvDataRx ) { // 20190920 jone ++
slave_i2c_rx_len = g_u8SlvDataRx;
g_u8SlvDataRx = 0;
slave_i2c_rx ++;
i2c_isr_rx_end = 1;
}
//if( g_u8SlvDataTx ) {// 20190920 jone --
if( 6 <= g_u8SlvDataTx ) { // 20190920 jone ++
slave_i2c_tx_len = g_u8SlvDataTx;
g_u8SlvDataTx = 0;
slave_i2c_tx ++;
i2c_isr_tx_end = 1;
}
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA);
}
else
{
/* TO DO */
//printf("Status 0x%x is NOT processed\n", u32Status);
if(u32Status == 0x00) { // 20190920 jone ++
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO_SI); // 20190920 jone ++
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_STO); // 20190920 jone ++
//I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); // 20191115 jone --; 20190920 jone ++
} // 20190920 jone ++
else if( 0xb0 == u32Status ) { // 20191115 jone ++
I2C_SET_DATA(I2C0, slave_buff_tx[0]); // 20191115 jone ++
g_u8SlvDataTx = 1; // 20191115 jone ++
est_i2c_debug = 1;
}
else if( 0x68 == u32Status ) { // 20191115 jone ++
g_u8SlvDataRx = 0; // 20191115 jone ++
est_i2c_debug = 2;
}
else {// 20191115 jone ++
est_i2c_debug = 3;
}
I2C_SET_CONTROL_REG(I2C0, I2C_I2CON_SI_AA); // 20191115 jone ++
}
if( i2c_isr_rx_end ) { // get cmd from cpu
i2c_rx_buf_checksum = slave_buff_rx[0]; // 20190920 jone ++
i2c_rx_buf_checksum += slave_buff_rx[1]; // 20190920 jone ++
i2c_rx_buf_checksum += slave_buff_rx[2]; // 20190920 jone ++
i2c_rx_buf_checksum += slave_buff_rx[3]; // 20190920 jone ++
if( slave_buff_rx[4] == i2c_rx_buf_checksum ) { // 20190920 jone ++
if( WM_CMD_READ_INPUT == slave_buff_rx[0] ) {
slave_buff_tx[0] = i2c_input_buf[0];
slave_buff_tx[1] = i2c_input_buf[1];
slave_buff_tx[2] = i2c_input_buf[2];
slave_buff_tx[3] = i2c_input_buf[3];
slave_buff_tx[4] = i2c_input_buf[4];
slave_buff_tx[5] = i2c_input_buf[5];
i2c_isr_tx_read = 1;
}
else if( WM_CMD_CONFIG_GPIO == slave_buff_rx[0] ) {
if( GPIO_MAX > slave_buff_rx[1] ) {
if( SET_COIN_CLEAN_ERROR == slave_buff_rx[2] ) {
vInput_coin_reset[slave_buff_rx[1]] ++;
}
else if( SET_COIN_MODE == slave_buff_rx[2] ) {
vInput_coin_level_code[slave_buff_rx[1]] = slave_buff_rx[3];
}
}
}
else if( WM_CMD_SET_OUT == slave_buff_rx[0] ) {
if( GPIO_MAX > slave_buff_rx[1] ) {
if( GPIO_IS_CNT == def_config_mode[slave_buff_rx[1]] ) {
if( slave_buff_rx[3] ) { // value must be true
wm_out_cnt[slave_buff_rx[1]] += slave_buff_rx[3];
}
}
else {
vOutput_getdata_buff[slave_buff_rx[1]] = slave_buff_rx[3];
}
}
}
else if( WM_CMD_SET_COIN_COUNT == slave_buff_rx[0] ) {
if( GPIO_MAX > slave_buff_rx[1] ) {
est_i2c_temp = slave_buff_rx[2];
est_i2c_temp <<= 8;
est_i2c_temp += slave_buff_rx[3];
vInput_coin_count_buff[slave_buff_rx[1]] = est_i2c_temp;
vInput_coin_count_flag[slave_buff_rx[1]] ++;
}
}
else if( WM_CMD_SET_SHA256_ARG == slave_buff_rx[0] ) {
if( 0 == (slave_buff_rx[1] & 0xc0) ) {
est_i2c_temp = slave_buff_rx[1];
est_sha256_challenge_isr[est_i2c_temp] = slave_buff_rx[2];
est_i2c_temp ++;
est_sha256_challenge_isr[est_i2c_temp] = slave_buff_rx[3];
est_i2c_temp ++;
est_sha256_challenge_isr[est_i2c_temp] = slave_buff_rx[4];
if( 0x3f == slave_buff_rx[1] ) { // receive data over
est_sha256_en_isr ++;
est_sha256_cha_isr_len = (slave_buff_rx[2] & 0x3f);
est_sha256_cha_mode_0 = slave_buff_rx[3];
est_sha256_cha_mode_1 = slave_buff_rx[4];
}
}
}
else if( WM_CMD_READ_COIN_COUNT == slave_buff_rx[0] ) {
if( GPIO_MAX > slave_buff_rx[1] ) {
slave_buff_tx[0] = slave_buff_rx[0];
slave_buff_tx[1] = slave_buff_rx[1];
slave_buff_tx[2] = ((vInput_coin_count[slave_buff_rx[1]] >> 8) & 0xff);
slave_buff_tx[3] = (vInput_coin_count[slave_buff_rx[1]] & 0xff);
i2c_rx_buf_checksum = slave_buff_tx[0];
i2c_rx_buf_checksum += slave_buff_tx[1];
i2c_rx_buf_checksum += slave_buff_tx[2];
i2c_rx_buf_checksum += slave_buff_tx[3];
slave_buff_tx[4] = i2c_rx_buf_checksum;
slave_buff_tx[5] = i2c_rx_buf_checksum;
}
}
else if( WM_CMD_SET_COIN_OUT_DELAY == slave_buff_rx[0] ) {
if( GPIO_MAX > slave_buff_rx[1] ) {
est_i2c_temp = slave_buff_rx[2];
est_i2c_temp <<= 8;
est_i2c_temp += slave_buff_rx[3];
vInput_coin_out_port_delay[slave_buff_rx[1]] = est_i2c_temp;
}
}
else if( WM_CMD_ENABLE_NEXT_INPUT == slave_buff_rx[0] ) {
vInput_SdIIC_valid = 0; /* wait for next input key */
}
else if( WM_CMD_READ_VERSION == slave_buff_rx[0] ) {
slave_buff_tx[0] = slave_buff_rx[0];
slave_buff_tx[1] = slave_buff_rx[1];
slave_buff_tx[2] = boot_fw;
slave_buff_tx[3] = WM_KEYBOARD_ID;
i2c_rx_buf_checksum = slave_buff_tx[0];
i2c_rx_buf_checksum += slave_buff_tx[1];
i2c_rx_buf_checksum += slave_buff_tx[2];
i2c_rx_buf_checksum += slave_buff_tx[3];
slave_buff_tx[4] = i2c_rx_buf_checksum;
slave_buff_tx[5] = i2c_rx_buf_checksum;
}
slave_buff_rx[0] = 0x00; // 20190920 jone ++
slave_buff_rx[1] = 0x00; // 20190920 jone ++
slave_buff_rx[2] = 0x00; // 20190920 jone ++
slave_buff_rx[3] = 0x00; // 20190920 jone ++
} // 20190920 jone ++
else { // 20190920 jone ++
i2c_rx_buf_checksum = 0x00; // 20190920 jone ++
slave_buff_rx[0] = 0x00; // 20190920 jone ++
slave_buff_rx[1] = 0x00; // 20190920 jone ++
slave_buff_rx[2] = 0x00; // 20190920 jone ++
slave_buff_rx[3] = 0x00; // 20190920 jone ++
}
}
if( i2c_isr_tx_end ) { // mcu send data over
if( i2c_isr_tx_read ) { // cpu read input triged by int
vInput_SdIIC_valid = 0; // enable next data packet
i2c_isr_tx_read = 0;
}
}
i2c_isr_rx_end = 0;
i2c_isr_tx_end = 0;
}
void TMR1_IRQHandler(void)
{
if( TIMER_GetIntFlag(TIMER1) == 1 ) {
/* Clear Timer1 time-out interrupt flag */
TIMER_ClearIntFlag(TIMER1);
est_timer1_count ++;
if( ADC_GET_INT_FLAG(ADC, ADC_ADF_INT) ) { // adc convert ok
ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);
est_0_adc[est_index] = ADC_GET_CONVERSION_DATA(ADC, 0);
est_1_adc[est_index] = ADC_GET_CONVERSION_DATA(ADC, 1);
ADC_START_CONV(ADC);
est_index ++;
est_index &= 0x0f;
if( 0 == est_index ) {
est_adc[0] = 0;
est_adc[1] = 0;
for( est_adc_temp = 0; 16 > est_adc_temp; est_adc_temp ++ ) {
est_adc[0] += est_0_adc[est_adc_temp];
est_adc[1] += est_1_adc[est_adc_temp];
}
est_adc[0] >>= 4;
est_adc[1] >>= 4;
}
}
}
}
void UART0_IRQHandler(void)
{
/*----- Determine interrupt source -----*/
uint32_t u32IntSrc = UART0->ISR;
if (u32IntSrc & UART_ISR_RDA_INT_Msk) {
uart_rcvbuf[buf_in] = UART0->RBR;
buf_in ++;
buf_in &= 0x3f;
}
}
|