123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- #include "stm32f4_discovery.h"
-
-
-
-
-
-
-
-
- #define __STM32F4_DISCO_BSP_VERSION_MAIN (0x02)
- #define __STM32F4_DISCO_BSP_VERSION_SUB1 (0x01)
- #define __STM32F4_DISCO_BSP_VERSION_SUB2 (0x04)
- #define __STM32F4_DISCO_BSP_VERSION_RC (0x00)
- #define __STM32F4_DISCO_BSP_VERSION ((__STM32F4_DISCO_BSP_VERSION_MAIN << 24)\
- |(__STM32F4_DISCO_BSP_VERSION_SUB1 << 16)\
- |(__STM32F4_DISCO_BSP_VERSION_SUB2 << 8 )\
- |(__STM32F4_DISCO_BSP_VERSION_RC))
-
-
-
-
- GPIO_TypeDef* GPIO_PORT[LEDn] = {LED4_GPIO_PORT,
- LED3_GPIO_PORT,
- LED5_GPIO_PORT,
- LED6_GPIO_PORT};
- const uint16_t GPIO_PIN[LEDn] = {LED4_PIN,
- LED3_PIN,
- LED5_PIN,
- LED6_PIN};
- GPIO_TypeDef* BUTTON_PORT[BUTTONn] = {KEY_BUTTON_GPIO_PORT};
- const uint16_t BUTTON_PIN[BUTTONn] = {KEY_BUTTON_PIN};
- const uint8_t BUTTON_IRQn[BUTTONn] = {KEY_BUTTON_EXTI_IRQn};
- #if 0
- uint32_t I2cxTimeout = I2Cx_TIMEOUT_MAX;
- uint32_t SpixTimeout = SPIx_TIMEOUT_MAX;
- static SPI_HandleTypeDef SpiHandle;
- static I2C_HandleTypeDef I2cHandle;
-
-
-
-
- static void I2Cx_Init(void);
- static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value);
- static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg);
- static void I2Cx_MspInit(void);
- static void I2Cx_Error(uint8_t Addr);
- static void SPIx_Init(void);
- static void SPIx_MspInit(void);
- static uint8_t SPIx_WriteRead(uint8_t Byte);
- static void SPIx_Error(void);
- void ACCELERO_IO_Init(void);
- void ACCELERO_IO_ITConfig(void);
- void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
- void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
- void AUDIO_IO_Init(void);
- void AUDIO_IO_DeInit(void);
- void AUDIO_IO_Write(uint8_t Addr, uint8_t Reg, uint8_t Value);
- uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg);
-
- #endif
- uint32_t BSP_GetVersion(void)
- {
- return __STM32F4_DISCO_BSP_VERSION;
- }
- void BSP_LED_Init(Led_TypeDef Led)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
-
- LEDx_GPIO_CLK_ENABLE(Led);
-
- GPIO_InitStruct.Pin = GPIO_PIN[Led];
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
-
- HAL_GPIO_Init(GPIO_PORT[Led], &GPIO_InitStruct);
-
- HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
- }
- void BSP_LED_On(Led_TypeDef Led)
- {
- HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_SET);
- }
- void BSP_LED_Off(Led_TypeDef Led)
- {
- HAL_GPIO_WritePin(GPIO_PORT[Led], GPIO_PIN[Led], GPIO_PIN_RESET);
- }
- void BSP_LED_Toggle(Led_TypeDef Led)
- {
- HAL_GPIO_TogglePin(GPIO_PORT[Led], GPIO_PIN[Led]);
- }
-
-
- void BSP_PB_Init(Button_TypeDef Button, ButtonMode_TypeDef Mode)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
-
- BUTTONx_GPIO_CLK_ENABLE(Button);
-
- if (Mode == BUTTON_MODE_GPIO)
- {
-
- GPIO_InitStruct.Pin = BUTTON_PIN[Button];
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
-
- HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
- }
-
- if (Mode == BUTTON_MODE_EXTI)
- {
-
- GPIO_InitStruct.Pin = BUTTON_PIN[Button];
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- HAL_GPIO_Init(BUTTON_PORT[Button], &GPIO_InitStruct);
-
-
- HAL_NVIC_SetPriority((IRQn_Type)(BUTTON_IRQn[Button]), 0x0F, 0);
- HAL_NVIC_EnableIRQ((IRQn_Type)(BUTTON_IRQn[Button]));
- }
- }
- uint32_t BSP_PB_GetState(Button_TypeDef Button)
- {
- return HAL_GPIO_ReadPin(BUTTON_PORT[Button], BUTTON_PIN[Button]);
- }
- #if 0
-
-
- static void SPIx_Init(void)
- {
- if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
- {
-
- SpiHandle.Instance = DISCOVERY_SPIx;
- SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
- SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
- SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
- SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
- SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
- SpiHandle.Init.CRCPolynomial = 7;
- SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
- SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
- SpiHandle.Init.NSS = SPI_NSS_SOFT;
- SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
- SpiHandle.Init.Mode = SPI_MODE_MASTER;
- SPIx_MspInit();
- HAL_SPI_Init(&SpiHandle);
- }
- }
- static uint8_t SPIx_WriteRead(uint8_t Byte)
- {
- uint8_t receivedbyte = 0;
-
-
-
- if(HAL_SPI_TransmitReceive(&SpiHandle, (uint8_t*) &Byte, (uint8_t*) &receivedbyte, 1, SpixTimeout) != HAL_OK)
- {
- SPIx_Error();
- }
-
- return receivedbyte;
- }
- static void SPIx_Error(void)
- {
-
- HAL_SPI_DeInit(&SpiHandle);
-
-
- SPIx_Init();
- }
- static void SPIx_MspInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- DISCOVERY_SPIx_CLK_ENABLE();
-
-
- DISCOVERY_SPIx_GPIO_CLK_ENABLE();
-
-
- GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MISO_PIN | DISCOVERY_SPIx_MOSI_PIN);
- GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStructure.Pull = GPIO_PULLDOWN;
- GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
- GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
- HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
- }
- static void I2Cx_Init(void)
- {
- if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET)
- {
-
- I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED;
- I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
- I2cHandle.Init.OwnAddress1 = 0x33;
- I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- I2cHandle.Instance = DISCOVERY_I2Cx;
-
-
- I2Cx_MspInit();
- HAL_I2C_Init(&I2cHandle);
- }
- }
- static void I2Cx_WriteData(uint8_t Addr, uint8_t Reg, uint8_t Value)
- {
- HAL_StatusTypeDef status = HAL_OK;
-
- status = HAL_I2C_Mem_Write(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &Value, 1, I2cxTimeout);
-
- if(status != HAL_OK)
- {
-
- I2Cx_Error(Addr);
- }
- }
- static uint8_t I2Cx_ReadData(uint8_t Addr, uint8_t Reg)
- {
- HAL_StatusTypeDef status = HAL_OK;
- uint8_t value = 0;
-
- status = HAL_I2C_Mem_Read(&I2cHandle, Addr, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, &value, 1,I2cxTimeout);
-
-
- if(status != HAL_OK)
- {
-
- I2Cx_Error(Addr);
- }
- return value;
- }
- static void I2Cx_Error(uint8_t Addr)
- {
-
- HAL_I2C_DeInit(&I2cHandle);
-
-
- I2Cx_Init();
- }
- static void I2Cx_MspInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- DISCOVERY_I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
-
- GPIO_InitStruct.Pin = DISCOVERY_I2Cx_SCL_PIN | DISCOVERY_I2Cx_SDA_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Alternate = DISCOVERY_I2Cx_SCL_SDA_AF;
- HAL_GPIO_Init(DISCOVERY_I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
-
- DISCOVERY_I2Cx_CLK_ENABLE();
-
- DISCOVERY_I2Cx_FORCE_RESET();
-
- DISCOVERY_I2Cx_RELEASE_RESET();
-
- HAL_NVIC_SetPriority(DISCOVERY_I2Cx_EV_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_EV_IRQn);
-
- HAL_NVIC_SetPriority(DISCOVERY_I2Cx_ER_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DISCOVERY_I2Cx_ER_IRQn);
- }
- void ACCELERO_IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
-
-
- ACCELERO_CS_GPIO_CLK_ENABLE();
-
-
- GPIO_InitStructure.Pin = ACCELERO_CS_PIN;
- GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
- HAL_GPIO_Init(ACCELERO_CS_GPIO_PORT, &GPIO_InitStructure);
-
-
- ACCELERO_CS_HIGH();
-
- SPIx_Init();
- }
- void ACCELERO_IO_ITConfig(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
-
- ACCELERO_INT_GPIO_CLK_ENABLE();
-
-
- GPIO_InitStructure.Pin = ACCELERO_INT2_PIN;
- GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(ACCELERO_INT_GPIO_PORT, &GPIO_InitStructure);
-
-
- HAL_NVIC_SetPriority((IRQn_Type)ACCELERO_INT2_EXTI_IRQn, 0x0F, 0);
- HAL_NVIC_EnableIRQ((IRQn_Type)ACCELERO_INT2_EXTI_IRQn);
- }
- void ACCELERO_IO_Write(uint8_t *pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
- {
-
- if(NumByteToWrite > 0x01)
- {
- WriteAddr |= (uint8_t)MULTIPLEBYTE_CMD;
- }
-
- ACCELERO_CS_LOW();
-
-
- SPIx_WriteRead(WriteAddr);
-
-
- while(NumByteToWrite >= 0x01)
- {
- SPIx_WriteRead(*pBuffer);
- NumByteToWrite--;
- pBuffer++;
- }
-
-
- ACCELERO_CS_HIGH();
- }
- void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
- {
- if(NumByteToRead > 0x01)
- {
- ReadAddr |= (uint8_t)(READWRITE_CMD | MULTIPLEBYTE_CMD);
- }
- else
- {
- ReadAddr |= (uint8_t)READWRITE_CMD;
- }
-
- ACCELERO_CS_LOW();
-
-
- SPIx_WriteRead(ReadAddr);
-
-
- while(NumByteToRead > 0x00)
- {
-
- *pBuffer = SPIx_WriteRead(DUMMY_BYTE);
- NumByteToRead--;
- pBuffer++;
- }
-
-
- ACCELERO_CS_HIGH();
- }
- void AUDIO_IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
-
- AUDIO_RESET_GPIO_CLK_ENABLE();
-
-
- GPIO_InitStruct.Pin = AUDIO_RESET_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(AUDIO_RESET_GPIO, &GPIO_InitStruct);
-
- I2Cx_Init();
-
-
- HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_RESET);
-
-
- HAL_Delay(5);
-
-
- HAL_GPIO_WritePin(AUDIO_RESET_GPIO, AUDIO_RESET_PIN, GPIO_PIN_SET);
-
-
- HAL_Delay(5);
- }
- void AUDIO_IO_DeInit(void)
- {
-
- }
- void AUDIO_IO_Write (uint8_t Addr, uint8_t Reg, uint8_t Value)
- {
- I2Cx_WriteData(Addr, Reg, Value);
- }
- uint8_t AUDIO_IO_Read(uint8_t Addr, uint8_t Reg)
- {
- return I2Cx_ReadData(Addr, Reg);
- }
- #endif
-
-
-
-
|