初级会员
- 积分
- 69
- 金钱
- 69
- 注册时间
- 2013-9-21
- 在线时间
- 6 小时
|
发表于 2016-12-28 15:59:47
|
显示全部楼层
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hspi->Instance==SPI2)
{
/* Peripheral clock enable */
__SPI2_CLK_ENABLE();
/**SPI2 GPIO Configuration
PB12 ------> SPI2_NSS
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_14;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
__GPIOB_CLK_ENABLE();
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{
if(hspi->Instance==SPI2)
{
/* Peripheral clock disable */
__SPI2_CLK_DISABLE();
/**SPI2 GPIO Configuration
PB12 ------> SPI2_NSS
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
}
}
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI2->CR1&=0XFFC7;
SPI2->CR1|=SPI_BaudRatePrescaler; //éèÖÃSPI2Ëù¶è
HAL_SPI_Init(&hspi2);
}
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; // êÖ2áòaÇó£¬ÏèlsbÔúmsb
hspi2.Init.TIMode = SPI_TIMODE_DISABLED;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hspi2.Init.CRCPolynomial = 7;
// HAL_SPI_MspDeInit(&hspi2);
HAL_SPI_MspInit(&hspi2);
HAL_SPI_Init(&hspi2);
// SPI2_ReadWriteByte(0xff);//Æô¶ˉ′«êä
__SPI2_CLK_ENABLE();
}
/**
* @brief SPI error treatment function.
* @param None
* @retval None
*/
static void SPI2_Error (void)
{
/* De-Initialize the SPI comunication BUS */
HAL_SPI_DeInit(&hspi2);
/* Re-Initiaize the SPI comunication BUS */
MX_SPI2_Init();
}
static void ADXL362_Write_Reg(u8 address ,u8 data)
{
uint8_t xdata[3];
SPI2_NSS_LOW();
xdata[0] = 0x0a;
xdata[1] = address;
xdata[2] = data;
HAL_SPI_Transmit(&hspi2, (uint8_t*) xdata, 3, 0x1000);
SPI2_NSS_HIGH();
HAL_Delay(1);
}
static u8 ADXL362_Read_Reg(u8 address)
{
u8 value[3],revalue[3],x;
uint32_t readvalue = 0;
SPI2_NSS_LOW();
value[0] = 0X0B;
value[1] = address;
value[2] = 0x00;
revalue[0] = 0;
revalue[1] = 0;
revalue[2] = 0;
HAL_SPI_TransmitReceive(&hspi2, value, revalue, 3, 0xFFFF);
SPI2_NSS_HIGH();
// HAL_Delay(1);
// readvalue = revalue[0];
// readvalue =(readvalue<<8)+revalue[1];
// readvalue =(readvalue<<8)+revalue[2];
return revalue[2];
}
static u8 ADXL362_FIFO_read_byte(u8 address)
{
u8 value;
SPI2_NSS_LOW();
SPI2_ReadWriteByte(0X0D);
SPI2_ReadWriteByte(address);
value = SPI2_ReadWriteByte(0x00);
SPI2_NSS_HIGH();
return value;
}
void ADXL362Init(void)
{
uint8_t readreg[10];
ADXL362_Write_Reg(0x1F,0x52); // software reset
HAL_Delay(20);
readreg[0] = ADXL362_Read_Reg(XL362_DEVID_AD);
readreg[1] = ADXL362_Read_Reg(XL362_DEVID_MST);
readreg[2] = ADXL362_Read_Reg(XL362_PARTID);
ADXL362_Write_Reg(XL362_FILTER_CTL,0x02); // 0x2d POWER_CTL Measurement mode.
ADXL362_Write_Reg(XL362_POWER_CTL,0x02); // 0x2d POWER_CTL Measurement mode.
HAL_UART_SendBytes("/***reg 20-2a***/ \r\n",25);
HAL_UART_SendBytes(readreg,5);
//ADXL362_Write_Reg(0x2D,0x00); //
} |
|