谢谢各位,我是用CUBE自动生成的,代码首先程序初始化
int main(void)
{
/* USER CODE BEGIN 1 */
#if (OS_TASK_NAME_EN > 0u)
CPU_INT08U os_err;
#endif
OS_CPU_SR cpu_sr;
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM2_Init();
MX_SPI2_Init();
/* USER CODE BEGIN 2 */
HAL_SPI_TransmitReceive_DMA(&hspi2, (uint8_t *)AD_VALUE_Buff1, (uint8_t* )RX_data,1024);//象串口收发一样初始SPI
LY68_Data_init_Test();//初始我的接收测试数组
然后在进入任务处开始接收测试
void Task_One(void *p_arg)//优先级最高的任务
{
OS_CPU_SR cpu_sr=0;
p_arg=p_arg;
u32 Writ_Add;
Writ_Add=YL68Mem.Star_add; //外设起始地址,我是从02开始,但是从00开始一样不对
HAL_GPIO_WritePin(NSS_GPIO_Port, NSS_Pin, GPIO_PIN_RESET);//拉低外设片选
LY68_Write_Page_writ(Writ_Add);//开始写
delay_us(5);
HAL_GPIO_WritePin(NSS_GPIO_Port, NSS_Pin, GPIO_PIN_SET);
delay_us(5);
HAL_GPIO_WritePin(NSS_GPIO_Port, NSS_Pin, GPIO_PIN_RESET);
delay_us(10);
LY68_Read_Page_writ(); //开始读
delay_us(10);
HAL_GPIO_WritePin(NSS_GPIO_Port, NSS_Pin, GPIO_PIN_SET)
while(1)//进入任务循环
{
//下面是我的具体读写程序
写。。
void LY68_Write_Page_writ(u32 Write_Start_Addr )
{
u16 Len;
Write_Start_Addr=0; //写地址
Send_Write_Command(LY_SPI_Write_COMMAND); //发02写命令
SPI2_ReadWriteByte((u8)((Write_Start_Addr)>>16));//地址 23:16 ·
SPI2_ReadWriteByte((u8)((Write_Start_Addr)>>8)); //地址 15:8
SPI2_ReadWriteByte((u8)(Write_Start_Addr)); //地址 15:8
HAL_SPI_TransmitReceive_DMA(&hspi2, (uint8_t *)AD_VALUE_Buff1, (uint8_t* )RX_data,1024);
//HAL_SPI_Transmit(&hspi2, (uint8_t *)AD_VALUE_Buff1,1024,0XFFFF);//用这个就对
return;
}
//读
void LY68_Read_Page_writ()
{
u16 Read_ADDR;
Read_ADDR=0;
Send_Write_Command(LY_SPI_Fast_Read_COMMAND); //·发读命令
SPI2_ReadWriteByte((u8)((Read_ADDR)>>16));//µØÖ· 23:16 //·¢ËͶÁÊý¾ÝÆðʼµØÖ·
SPI2_ReadWriteByte((u8)((Read_ADDR)>>8)); //µØÖ· 15:8
SPI2_ReadWriteByte((u8)(Read_ADDR)); //µØÖ· 15:8
HAL_SPI_TransmitReceive(&hspi2, (uint8_t *)AD_VALUE_Buff1, (uint8_t* )RX_data,1020,0XFFFFF);
//HAL_SPI_Receive_IT(&hspi2,(uint8_t* )RX_data, 1020);
}
SPI及DMA初始化。。。。。
void MX_SPI2_Init(void)
{
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}///////////*********************************************
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(spiHandle->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */
/* SPI2 clock enable */
__HAL_RCC_SPI2_CLK_ENABLE();
/**SPI2 GPIO Configuration
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* SPI2 DMA Init */
/* SPI2_RX Init */
hdma_spi2_rx.Instance = DMA1_Channel4;
hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_rx.Init.Mode = DMA_NORMAL;
hdma_spi2_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(spiHandle,hdmarx,hdma_spi2_rx);
/* SPI2_TX Init */
hdma_spi2_tx.Instance = DMA1_Channel5;
hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_tx.Init.Mode = DMA_NORMAL;
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(spiHandle,hdmatx,hdma_spi2_tx);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */
}
}
|