Skip to content

Commit

Permalink
i2c ll transmit block
Browse files Browse the repository at this point in the history
  • Loading branch information
gyxkgz committed Apr 18, 2022
1 parent 7bfbaf9 commit fb98208
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
3 changes: 2 additions & 1 deletion 2.Firmware/ServoDrive-fw-ll/include/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ extern uint8_t i2cDataTx[8];

/* USER CODE BEGIN Prototypes */
void MY_I2C1_Init(uint32_t _id);
void set_id(uint8_t _id);
void Set_ID(uint8_t _id);
ErrorStatus Slave_Transmit(uint8_t *pdata,uint16_t size,uint32_t timeout);
void I2C_SlaveDMARxCpltCallback();
/* USER CODE END Prototypes */

Expand Down
6 changes: 6 additions & 0 deletions 2.Firmware/ServoDrive-fw-ll/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
这个是智能舵机的LL库版本,代码体积得到优化,使用vscode和platformio开发,开发环境和编译参考[这个视频](https://www.bilibili.com/video/BV1US4y1P7Xr/)

## **2022.04.18**

1. 将i2c部分由hal库改为ll库实现;
2. 设置i2c地址立刻生效,不需要重启;
71 changes: 49 additions & 22 deletions 2.Firmware/ServoDrive-fw-ll/src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,6 @@ void MY_I2C1_Init(uint32_t _id)
LL_DMA_SetMemoryAddress(DMA1,LL_DMA_CHANNEL_3,(uint32_t)i2cDataRx);
LL_DMA_SetPeriphAddress(DMA1,LL_DMA_CHANNEL_3,LL_I2C_DMA_GetRegAddr(I2C1,LL_I2C_DMA_REG_DATA_RECEIVE));
LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_3);

/* I2C1_TX Init */
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);

LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_LOW);

LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MODE_NORMAL);

LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);

LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);

LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);

LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,5);
LL_DMA_SetMemoryAddress(DMA1,LL_DMA_CHANNEL_2,(uint32_t)i2cDataTx);
LL_DMA_SetPeriphAddress(DMA1,LL_DMA_CHANNEL_2,LL_I2C_DMA_GetRegAddr(I2C1,LL_I2C_DMA_REG_DATA_TRANSMIT));
LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_2);

/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);

Expand Down Expand Up @@ -127,16 +107,63 @@ void MY_I2C1_Init(uint32_t _id)
LL_I2C_Enable(I2C1);
LL_I2C_EnableIT_ADDR(I2C1);
LL_I2C_EnableDMAReq_RX(I2C1);
LL_I2C_EnableDMAReq_TX(I2C1);
/* USER CODE END I2C1_Init 2 */
}

void set_id(uint8_t _id)
void Set_ID(uint8_t _id)
{
LL_I2C_DisableOwnAddress1(I2C1);
LL_I2C_SetOwnAddress1(I2C1,_id,LL_I2C_OWNADDRESS1_7BIT);
LL_I2C_EnableOwnAddress1(I2C1);
}

ErrorStatus Slave_Transmit(uint8_t *pdata,uint16_t size,uint32_t timeout)
{
uint32_t Timeout = timeout;
while(!LL_I2C_IsActiveFlag_ADDR(I2C1))
{
/* Check Systick counter flag to decrement the time-out value */
if (LL_SYSTICK_IsActiveCounterFlag())
{
if(Timeout-- == 0)
{
/* Time-out occurred. return error */
return ERROR;
}
}
}
/*Clear ADDR flag and loop until end of transfer*/
/* Clear ADDR flag value in ISR register */
LL_I2C_ClearFlag_ADDR(I2C1);

if(LL_I2C_GetTransferDirection(I2C1) == LL_I2C_DIRECTION_READ)
{
/* Loop until TXE flag is raised */
while(size > 0)
{
/* Transmit data (TXE flag raised) **********************************/
/* Check TXE flag value in ISR register */
if(LL_I2C_IsActiveFlag_TXE(I2C1))
{
/* Write data in Transmit Data register.
TXE flag is cleared by writing data in TXDR register */
LL_I2C_TransmitData8(I2C1, (*pdata++));
size--;
Timeout = timeout;
}
/* Check Systick counter flag to decrement the time-out value */
if (LL_SYSTICK_IsActiveCounterFlag())
{
if(Timeout-- == 0)
{
/* Time-out occurred. return error */
return ERROR;
}
}
}
}
return SUCCESS;
}
/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
4 changes: 3 additions & 1 deletion 2.Firmware/ServoDrive-fw-ll/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ int main(void)
/* System interrupt init*/

/* USER CODE BEGIN Init */

#if (PREFETCH_ENABLE != 0)
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
#endif /* PREFETCH_ENABLE */
/* USER CODE END Init */

/* Configure the system clock */
Expand Down
9 changes: 8 additions & 1 deletion 2.Firmware/ServoDrive-fw-ll/src/main1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void Main()
// // Command handler
void I2C_SlaveDMARxCpltCallback()
{
ErrorStatus state;

float valF = *((float*) (i2cDataRx + 1));

Expand Down Expand Up @@ -133,7 +134,7 @@ void I2C_SlaveDMARxCpltCallback()
{
boardConfig.nodeId = i2cDataRx[1];
boardConfig.configStatus = CONFIG_COMMIT;
set_id(boardConfig.nodeId);
Set_ID(boardConfig.nodeId);
auto* b = (unsigned char*) &(motor.angle);
for (int i = 0; i < 4; i++)
i2cDataTx[i + 1] = *(b + i);
Expand Down Expand Up @@ -203,7 +204,13 @@ void I2C_SlaveDMARxCpltCallback()
break;
default:
break;

}
do
{
state = Slave_Transmit(i2cDataTx,5,5000);
} while (state != SUCCESS);

}


Expand Down
17 changes: 3 additions & 14 deletions 2.Firmware/ServoDrive-fw-ll/src/stm32f0xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,7 @@ void DMA1_Channel2_3_IRQHandler(void)
I2C_SlaveDMARxCpltCallback();
LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_3);
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_3,5);
}
else if (LL_DMA_IsActiveFlag_TC2(DMA1))
{
LL_DMA_ClearFlag_TC2(DMA1);
LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_2);
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,5);
}

}
/* USER CODE END DMA1_Channel2_3_IRQn 0 */

/* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */
Expand Down Expand Up @@ -221,13 +214,9 @@ void I2C1_IRQHandler(void)
if(LL_I2C_GetTransferDirection(I2C1) == LL_I2C_DIRECTION_WRITE)
{
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_3);
LL_I2C_ClearFlag_ADDR(I2C1);
}
else
{
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_2);
}
/* Clear ADDR flag value in ISR register */
LL_I2C_ClearFlag_ADDR(I2C1);

}

/* USER CODE END I2C1_IRQn 0 */
Expand Down

0 comments on commit fb98208

Please sign in to comment.