当前位置:网站首页>Realize IIC data / instruction interaction with micro batg135
Realize IIC data / instruction interaction with micro batg135
2022-07-04 06:23:00 【Tears like eating hot pot】
This routine is used in a project IIC Realization 3 Direct data interaction of Taichung micro , And the wechat IIC Routines tend to be simple , There are many problems in the implementation , To really achieve IIC, Need to rewrite .
First of all, we need to initialize the micro as a slave :
void IICA0_Init(void)
{
CGC->PER0 |= CGC_PER0_IICA0EN_Msk; /* enables input clock supply */
IICA->IICCTL00 = _00_IICA_OPERATION_STOP; /* stop operation */
INTC_DisableIRQ(IICA_IRQn); /* disable INTIICA interrupt flag */
INTC_ClearPendingIRQ(IICA_IRQn); /* clear INTIICA interrupt flag */#ifdef IICA_STANDARD_MODE /* Max rate: 100Kbps */
IICA->IICCTL01 = _00_IICA_OPERATE_STANDARD; /* operates in standard mode */
IICA->IICWL0 = _4B_IICA_LOW_WIDTH;
IICA->IICWH0 = _53_IICA_HIGH_WIDTH;
#endif
#ifdef IICA_FAST_MODE /* Max rate: 400Kbps */
IICA->IICCTL01 = _08_IICA_OPERATE_FAST; /* operates in fast mode */
IICA->IICCTL01 |= _04_IICA_FILTER_ON; /* filter on */
IICA->IICWL0 = _14_IICA_LOW_WIDTH;
IICA->IICWH0 = _12_IICA_HIGH_WIDTH;
#endif
IICA->IICCTL01 |= _01_IICA_FCLK_2_SELECTED;
IICA->SVA0 = _98_IICA0_SLAVEADDRESS;
IICA->IICF0 |= _02_IICA_WITHOUT_DETECTION; /* enable generation of a start condition without detecting a stop
condition */
IICA->IICF0 |= _01_IICA_RESERVATION_DISABLE; /* disable communication reservation */
IICA->IICCTL00 |= _08_IICA_INTERRUPT_REQUEST_NINTH; /* interrupt request is generated at the ninth clock's falling edge */
IICA->IICCTL00 |= _00_IICA_ACKOWNLEDGMENT_DISABLE; /* enable acknowledgment */
IICA->IICCTL00 |= _80_IICA_OPERATION_ENABLE; /* enable operation */
IICA->IICCTL00 |= _40_IICA_OPERATION_STANDBY; /* this exits from the current communications and sets standby mode */
INTC_EnableIRQ(IICA_IRQn); /* enable INTIICA interrupt flag *//* Set SCLA0, SDAA0 pin */
SCLA0_PORT_SETTING();
SDAA0_PORT_SETTING();
}
among : According to the... Of Zhongwei GPIO Oral IIC Pin mapping
#define SCLA0_PORT_SETTING() do{ \
PORT->SCLA0PCFG = 0x13; /* allocate SCLA0 to P30 */ \
PORT->P3 &= ~(1 << 0); /* P30 output low level */ \
PORT->PM3 &= ~(1 << 0); /* P30 is N-ch open-drain output mode */ \
PORT->PMC3 &= ~(1 << 0); /* P30 digital function */ \
}while(0)/* ToDo: You can allocate the SDA00 to any desired pins with SDA00PCFG register */
#define SDAA0_PORT_SETTING() do{ \
PORT->SDAA0PCFG = 0x17; /* allocate SDAA0 to P50 */ \
PORT->P5 &= ~(1 << 0); /* P50 output low level */ \
PORT->PM5 &= ~(1 << 0); /* P50 is used as SDAA0 inout */ \
PORT->PMC5 &= ~(1 << 0); /* P50 digital function */ \
}while(0)
Next, micro enterprise as a slave , Realize the sending and receiving of data :
void IIC_Rec_Fuction(void)
{
static uint8_t times = 0;
static uint16_t interval = 0;
uint8_t csc = 0;
if (0 == times)
{
times = 1;
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
interval = gMillisecondTick;
}
if (1 == times)
{
if (((gMillisecondTick - interval) & 0xFFFF) > 10) //10ms
{
if ((g_iica0_rx_end == 1) && (RxBuffer2[0] == 0x55) && (RxBuffer2[1] == 0xAA) && (RxBuffer2[2] != 0x00))
{
for(uint16_t i = 0; i < (RxBuffer2[2] + 1);i++ )
{
csc ^= RxBuffer2[i];
}
if(csc == RxBuffer2[RxBuffer2[2] + 1])
{
Sys_Data.CmdData = (RxBuffer2[4]<<8) + RxBuffer2[5]; /* Get function instructions */
memcpy(Sys_Data.Rec_Data,RxBuffer2,sizeof(RxBuffer2)); /* Get the received data */
//Sys_Data.Rec_Data Is an array of structures :200 Bytes uint8_t Rec_Data[200]; /* Receive array */, There is no problem with the data received here, so you can enter
/*IIC Receiving function processing */
Rev_Process_Fuction();
Send_Process_Connect_Upload(); /*IIC Send data thread */
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
}
}
interval = gMillisecondTick;
}
}else
{
g_iica0_rx_len = 200;
g_iica0_rx_cnt = 0U;
g_iica0_rx_end = 0;
g_iica0_slave_status_flag = 0U;
IICA->IICA0 = SLVADDR_98U; /* slave address */
}
}
among : It should be noted that , Remember to modify the interrupt function officially given by wechat :
uint8_t RxBuffer2[200]; /* Receive array */
uint16_t RxCounter2 = 0; /* Receive count *//***********************************************************************************************************************
* Function Name: iica0_slavehandler
* @brief This function is IICA0 slave handler.
* @param None
* @return None
***********************************************************************************************************************/
static void iica0_slavehandler(void)
{
/* Control for stop condition */
if (IICA->IICS0 & IICA_IICS0_SPD_Msk)
{
/* Get stop condition */
IICA->IICCTL00 &= ~IICA_IICCTL00_SPIE_Msk; /* SPIE0 = 0: disable */
if(g_iica0_slave_status_flag & _80_IICA_ADDRESS_COMPLETE)
{
if (g_iica0_slave_status_flag & 0x04U) /* send flag */
{
iica0_callback_slave_sendend();
}
if (g_iica0_slave_status_flag & 0x02U) /* receive flag */
{
iica0_callback_slave_receiveend();
}
}
g_iica0_slave_status_flag = 1U;
}
else
{
if ((g_iica0_slave_status_flag & _80_IICA_ADDRESS_COMPLETE) == 0U)
{
if (IICA->IICS0 & IICA_IICS0_COI_Msk)
{
IICA->IICCTL00 |= IICA_IICCTL00_SPIE_Msk; /* SPIE0 = 1: enable */
g_iica0_slave_status_flag = _80_IICA_ADDRESS_COMPLETE;if (IICA->IICS0 & IICA_IICS0_TRC_Msk) /* TRC0 == 1 */
{
IICA->IICCTL00 |= IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 1: interrupt request is generated at the ninth clock's falling edge */if (g_iica0_tx_cnt > 0U)
{
IICA->IICA0 = *gp_iica0_tx_address;
gp_iica0_tx_address++;
g_iica0_tx_cnt--;
}
else
{
iica0_callback_slave_sendend();
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_ACKE_Msk; /* ACKE0 = 1U: enable acknowledgment */
IICA->IICCTL00 &= ~IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 0U: interrupt request is generated at the eighth clock's falling edge */
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
iica0_callback_slave_error(MD_ERROR);
}
}
else
{
if (IICA->IICS0 & IICA_IICS0_TRC_Msk) /* TRC0 == 1 */
{
g_iica0_slave_status_flag |= 4U; /* send flag */
if ((0U == (IICA->IICS0 & IICA_IICS0_ACKD_Msk)) && (g_iica0_tx_cnt != 0U))
{
iica0_callback_slave_error(MD_NACK);
}
else
{
if (g_iica0_tx_cnt > 0U)
{
IICA->IICA0 = *gp_iica0_tx_address;
gp_iica0_tx_address++;
g_iica0_tx_cnt--;
}
else
{
iica0_callback_slave_sendend();
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
}
else
{
g_iica0_slave_status_flag |= 2U; /* receive flag */
if (g_iica0_rx_cnt < g_iica0_rx_len)
{
RxBuffer2[g_iica0_rx_cnt] = IICA->IICA0;
g_iica0_rx_cnt++;if (g_iica0_rx_cnt == g_iica0_rx_len)
{
IICA->IICCTL00 |= IICA_IICCTL00_WTIM_Msk; /* WTIM0 = 1: interrupt request is generated at the ninth clock's falling edge */
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
iica0_callback_slave_receiveend();
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
else
{
IICA->IICCTL00 |= IICA_IICCTL00_WREL_Msk; /* WREL0 = 1U: cancel wait */
}
}
}
}
}
/*
*********************************************************************************
* Letter Count name :Send_Process_Connect_Upload
* Functional specifications : Send data to Bluetooth MCU
* shape ginseng :void
* return return value :void
*********************************************************************************
*/
void Send_Process_Connect_Upload(void)
{
uint16_t value_size = 0;
//uint16_t i = 0;
//uint8_t tmpbuf[10]={0};
static uint8_t value[200] = {0};
/* Judge whether serial port sending is executed immediately */
if((Sys_Data.Com_Stas & COM_SEND_RDY) !=COM_SEND_RDY)
{
return;
}
/* Serial port group packet storage value Value*/
// value_size = SendPacekData(Sys_Data.SendBuf,Sys_Data.SendLen,value);
IICA0_SlaveSend(SLVADDR_98U,value,value_size);
while(g_iica0_tx_end == 0);
#if 0
/* send data */
printf_log("\r\n send_process :");
for(i=0;i<(value_size);i++)
{
memset(tmpbuf,0,10);
u8_T_array(value[i],tmpbuf);
printf_log((const char *)tmpbuf);
printf_log(" ");
}
printf_log("\r\n value_size = %d",value_size);
#endif
//Sys_Data.Com_Stas = 0; /* Command status reset */
// Sys_Data.CmdData = 0; /* Command reset */
// Sys_Data.SendLen = 0; /* The data length is 0*/
// memset(Sys_Data.CmdPara,0,sizeof(Sys_Data.CmdPara)); /* Command packet reset */
}
边栏推荐
- Sword finger offer II 038 Daily temperature
- My NVIDIA developer journey - optimizing graphics card performance
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- AWT常用组件、FileDialog文件选择框
- [microservice] Nacos cluster building and loading file configuration
- The difference between PX EM rem
- Notes and notes
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- Sleep quality today 78 points
- Distributed cap theory
猜你喜欢
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
SQL injection SQL lab 11~22
InputStream/OutputStream(文件的输入输出)
MySQL installation and configuration
buuctf-pwn write-ups (8)
Configure cross compilation tool chain and environment variables
Win10 clear quick access - leave no trace
My NVIDIA developer journey - optimizing graphics card performance
509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
随机推荐
Nexus 6p从8.0降级6.0+root
Which water in the environment needs water quality monitoring
手动对list进行分页(参数list ,当前页,页面大小)
Gridview出现滚动条,组件冲突,如何解决
C language exercises (recursion)
Common JS tool Libraries
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
SQL join, left join, right join usage
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
The width of the picture in rich text used by wechat applet exceeds the problem
JS flattened array of number shape structure
How to solve the component conflicts caused by scrollbars in GridView
Lightroom import picture gray / Black rectangular multi display
The end of the Internet is rural revitalization
安装 Pytorch geometric
Arcpy uses the updatelayer function to change the symbol system of the layer
云原生——上云必读之SSH篇(常用于远程登录云服务器)
[untitled]
JS execution mechanism