void ADT7310_Setup(bool IntEn) { // setup CS_n pin as output ADT7310_CS_DIR |= ADT7310_CS_PIN; ADT7310_CS_OUT |= ADT7310_CS_PIN; // low active --> deactivate // CPOL = 1, CPHA = 1 SPI_Config(true, true, 2, 2, IntEn); }
/******************************************************************************* MicroSD initialization return: 0x00=success *******************************************************************************/ u8 MSD_Init(void) { u32 i=0; SPI_Config(); MSD_CS_HIGH(); for(i=0; i <10; i++) MSD_WriteByte(DUMMY); //Send dummy byte: 8 Clock pulses of delay return MSD_GoIdleState(); }
BOOL CPU_SPI_Xaction_Start(const SPI_CONFIGURATION& Configuration) { if (Configuration.SPI_mod >= TOTAL_SPI_PORT) return FALSE; LPC_SSP_T *spi = SPI_REG(Configuration.SPI_mod); int Bits, Mode; // Configure options and clock Bits = (Configuration.MD_16bits) ? 16 : 8; Mode = (Configuration.MSK_IDLE) ? 2 : 0; // ToDo: Check Mode |= (!Configuration.MSK_SampleEdge) ? 1 : 0; SPI_Config(spi, Bits, Mode, 0); SPI_Frequency(spi, (1000 * Configuration.Clock_RateKHz)); // I/O setup GPIO_PIN msk, miso, mosi; CPU_SPI_GetPins(Configuration.SPI_mod, msk, miso, mosi); UINT32 alternate = 0x252; // AF5 = SPI1/SPI2 if (Configuration.SPI_mod == 2) alternate = 0x262; // AF6 = SPI3, speed = 2 (50MHz) CPU_GPIO_DisablePin(msk, RESISTOR_DISABLED, 1, (GPIO_ALT_MODE)alternate); CPU_GPIO_DisablePin(miso, RESISTOR_DISABLED, 0, (GPIO_ALT_MODE)alternate); CPU_GPIO_DisablePin(mosi, RESISTOR_DISABLED, 1, (GPIO_ALT_MODE)alternate); // CS setup CPU_GPIO_EnableOutputPin(Configuration.DeviceCS, Configuration.CS_Active); if(Configuration.CS_Setup_uSecs) { HAL_Time_Sleep_MicroSeconds_InterruptEnabled(Configuration.CS_Setup_uSecs); } return TRUE; }
void nRF905_PortInit(void) { GPIO_InitTypeDef GPIO_InitStructure; //enable GPIO clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC,ENABLE); //Configure TX_EN , TRX_CE , PWR_UP ,pin as output: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_Init(GPIOA,&GPIO_InitStructure); //Configure AM , DR , CM pins as input pin to be detected GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_2 | GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //推挽输出 GPIO_Init(GPIOC,&GPIO_InitStructure); // enable spi SPI_Config(); //NRF905初始化 CSN = 1 ; // Spi disable PWR_UP = 1 ; // nRF905 power on TRX_CE = 0; // Set nRF905 in standby mode TX_EN = 0; // set radio in Rx mode }
int main(void) { int temp = 5; //configures the priority grouping NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); //start time start_timer(); //configure spi SPI_Config(); //ETHERNET INITIALIZATION ETH_BSP_Config(); /* Initilaize the LwIP stack */ LwIP_Init(); //udp initialization if (!udp_echoserver_init()) return 1; while (1) { //PROCESSING OF PEREODIC TIMERS FOR LWIP LwIP_Periodic_Handle(gettime()); //PROCESSING OF INCOMING PACKET if (ETH_CheckFrameReceived()) { /* process received ethernet packet */ LwIP_Pkt_Handle(); } trans_Control((void*)&temp, sizeof(temp)); } }
uint32_t f415_SPI_Config(void) { SPI_Config(); //SPI_TxDma_Config(); SPI_RxDma_Config(ENABLE); return 0; }
void MPU6500_init() { SPI_Config(); MPU_Init(); Delay_ms(1500); Zero_Offset_Config(); LPF_Initial(); }
/******************************************************************************************************************* ** 函数名称: void SD_HardWareInit() Name: void SD_HardWareInit() ** 功能描述: 初始化访问SD卡的硬件条件 Function: initialize the hardware condiction that access sd card ** 输 入: 无 Input: NULL ** 输 出: 无 Output: NULL ********************************************************************************************************************/ void SD_HardWareInit(void) { SPI_Config(); /* 配置,初始化,SPI接口和SD片选 */ MSD_CS_HIGH(); // SPI_Clk400k(); /* 配置BSPI波特率为400k */ }
/*系统初始化*/ void Init_sys(void) { RCC_Config(); NVIC_Config(); GPIO_Config(); USART_Config(); SPI_Config(SPI1); app_tim(); //应用程序的定时器配置 }
void if_spiInit(hwInterface *iface) { euint8 i; //硬件SPI初始化 SPI_Config(); MSD_CS_HIGH(); /* Send 20 spi commands with card not selected */ for (i = 0;i < 21;i++) my_if_spiSend(iface, 0xff); }
void if_spiInit(hwInterface *iface) { euint8 i; SPI_Config(); MSD_CS_HIGH(); /* Send 20 spi commands with card not selected */ for(i=0;i<21;i++) my_if_spiSend(iface,0xff); /* Speed up */ if_spiSetSpeed(SPI_BaudRatePrescaler_2); }
/** * @brief Main program * @param None * @retval None */ int main(void) { STM32_Command.WaveType = WAVE_Sine; STM32_Command.Amplitude = 2048; STM32_Command.DCOffset = 4095; STM32_Command.DDS_PhaseFrq = 858993; STM32_Command.SweepMode = Sweep_Off; STM32_Command.SweepTime = 9; STM32_Command.SweepStep = 1718; STM32_Command.SweepMin = 687193; STM32_Command.SweepMax = 1030793; DDS_Config(); SPI_Config(); TIM_Config(); DDS_MakeWave(); DDS_WaveGenerator(); }
void ProcessInit() { delayByTIM_Init(72); SysTick_Config(SystemCoreClock/1000); Init_Rs485(); SPI_Config(); GPIO_buffer(); CAN_Mode_Init(1,7,10,10,0); flashFileInfo_ID.F_Start = FileInfo_485ID; flashFileInfo_ID.F_Size = 1; F_Open_Flash(&flashFileInfo_ID); sFLASH_ReadBuffer(&gDevice485Addr,flashFileInfo_ID.F_Start,flashFileInfo_ID.F_Size); flashFileInfo_ID.F_Start = FileInfo_CANID; flashFileInfo_ID.F_Size = 1; F_Open_Flash(&flashFileInfo_ID); sFLASH_ReadBuffer(&gDeviceCANAddr,flashFileInfo_ID.F_Start,flashFileInfo_ID.F_Size); }
int main(void) { volatile unsigned long int i; int temperatura; unsigned char temperaturaTekst[8]={" 0,0 C\0"}; static const unsigned char stopienSymbol[8] = {0x06,0x09,0x09,0x06,0x00,0x00,0x00,0}; //symbol stopnia //konfiguracja systemu RCC_Config(); GPIO_Config(); NVIC_Config(); SPI_Config(); /*Tu nalezy umiescic ewentualne dalsze funkcje konfigurujace system*/ GPIO_ResetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); LCD_Initialize(); //Inicjalizacja wysietlacza LCD_SetUserChar(1, 1, stopienSymbol); //Umiesc symbol stopnia pod kodem =1 LCD_WriteCommand(HD44780_CLEAR); //Wyczysc wyswietlacz LCD_WriteText("Temp.:\0"); temperaturaTekst[5]=1; LCD_WriteTextXY(temperaturaTekst,7,0); //Wstaw do tekstu znak stopnia #define SPI_Mode_Slave_Mask ((unsigned short int)0xFEFB) //Maska pozwalajaca wyzerowac bity trybu pracy wprost w rejestrzez SPIx->CR1 while (1) { /*Tu nalezy umiescic glowny kod programu*/ SPI1->CR1 |= SPI_Mode_Master; //Ustaw tryb master - wymusi to zmiane stanu NSS na niski while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //Czekaj na dane temperatura = SPI_I2S_ReceiveData(SPI1); //Odczytaj dane if ((temperatura&0x04)==0){ //Sprawdz, czy zakonczono juz pierwszy pomiar po wlaczeniu ukladu TC77 temperatura=0; //Jesli nie, ustaw temp=0, wprzeciwnym razie wynik bedzie bledny (>500stC) } temperatura = temperatura >> 3; //Usun 3 LSB SPI1->CR1 &= SPI_Mode_Slave_Mask; //Ustaw tryb slave - wymusi to zmiane stanu NSS na wysoki temperatura = (temperatura * 625)/100; //1 bit temperatury odpowiada 0,0625 stopnia sprintf((char *)temperaturaTekst, "%2d,%d C ", temperatura / 100, (temperatura % 100)/10 ); temperaturaTekst[4]=1; //Wstaw do tekstu znak stopnia LCD_WriteTextXY(temperaturaTekst,7,0); for (i=0;i<4500000ul;i++); GPIO_WriteBit(GPIOB, GPIO_Pin_15, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_15))); }; return 0; }
int main (void) { uint32_t n; MscDevInfo.idVendor = USB_VENDOR_ID; MscDevInfo.idProduct = USB_PROD_ID; MscDevInfo.bcdDevice = USB_DEVICE; MscDevInfo.StrDescPtr = (uint32_t)&USB_StringDescriptor[0]; MscDevInfo.MSCInquiryStr = (uint32_t)&InquiryStr[0]; MscDevInfo.BlockSize = MSC_BlockSize; MscDevInfo.BlockCount = MSC_BlockCount; MscDevInfo.MemorySize = MSC_MemorySize; MscDevInfo.MSC_Read = MSC_MemoryRead; MscDevInfo.MSC_Write = MSC_MemoryWrite; DeviceInfo.DevType = USB_DEVICE_CLASS_STORAGE; DeviceInfo.DevDetailPtr = (uint32_t)&MscDevInfo; /* Enable Timer32_1 and IOCON blocks */ LPC_SYSCON->SYSAHBCLKCTRL |= (EN_TIMER32_1 | EN_IOCON | EN_USBREG); initializeBoard(); SPI_Config(); (*rom)->pUSBD->init_clk_pins(); /* Use pll and pin init function in rom */ /* insert a delay between clk init and usb init */ for (n = 0; n < 75; n++) { } (*rom)->pUSBD->init(&DeviceInfo); /* USB Initialization */ init_msdstate(); /* Initialize Storage state machine */ (*rom)->pUSBD->connect(TRUE); /* USB Connect */ while (1); /* Loop forever */ }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* SPI configuration ------------------------------------------------------*/ SPI_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM320518-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Master board configuration ------------------------------------------------*/ #ifdef SPI_MASTER /* Initialize push-buttons mounted on STM320518-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); /* TIM configuration ------------------------------------------------------*/ TIM_Config(); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Enable NSS output for master mode */ SPI_SSOutputCmd(SPIx, ENABLE); /* TIM Capture Compare DMA Request enable */ TIM_DMACmd(TIMx, TIMx_DMA_CHANNEL, ENABLE); while (1) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandReceived; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA TIM trigger channel Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandTransmitted; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(TIMx_CHANNEL_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); CommandTransmitted = 0x00; CommandReceived = 0x00; /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); PressedButton = Read_Joystick(); while (PressedButton == JOY_NONE) { PressedButton = Read_Joystick(); } switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CommandTransmitted = CMD_RIGHT; NumberOfByte = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CommandTransmitted = CMD_LEFT; NumberOfByte = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CommandTransmitted = CMD_UP; NumberOfByte = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CommandTransmitted = CMD_DOWN; NumberOfByte = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CommandTransmitted = CMD_SEL; NumberOfByte = CMD_SEL_SIZE; break; default: break; } /* Enable the DMA channel */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); /* Enable DMA1 TIM Trigger Channel */ DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, ENABLE); /* TIM enable counter */ TIM_Cmd(TIMx, ENABLE); /* Wait the SPI DMA Rx transfer complete or time out*/ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags*/ DMA_ClearFlag(TIMx_CHANNEL_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, DISABLE); /* disable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); /* TIM disable counter */ TIM_Cmd(TIMx, DISABLE); if (CommandReceived == CMD_ACK) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(TIMx_CHANNEL_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); /* Enable the DMA channel */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); /* Enable DMA1 TIM Trigger Channel */ DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, ENABLE); /* TIM enable counter */ TIM_Cmd(TIMx, ENABLE); /* Wait the SPI Rx DMA transfer complete or time out */ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags */ DMA_ClearFlag(TIMx_CHANNEL_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); /* TIM disable counter */ TIM_Cmd(TIMx, DISABLE); switch (NumberOfByte) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } } } #endif /* SPI_MASTER */ /* Slave board configuration -----------------------------------------------*/ #ifdef SPI_SLAVE /* Initializes the SPI communication */ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); CommandTransmitted = CMD_ACK; /* Infinite Loop */ while (1) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandReceived; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandTransmitted; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(SPIx_TX_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); CommandReceived = 0x00; /* Enable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE); /* Wait the SPI DMA transfers complete or time out */ while (DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET) {} TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags */ DMA_ClearFlag(SPIx_TX_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE); switch (CommandReceived) { /* CMD_RIGHT command received */ case CMD_RIGHT: NumberOfByte = CMD_RIGHT_SIZE; break; /* CMD_LEFT command received */ case CMD_LEFT: NumberOfByte = CMD_LEFT_SIZE; break; /* CMD_UP command received */ case CMD_UP: NumberOfByte = CMD_UP_SIZE; break; /* CMD_DOWN command received */ case CMD_DOWN: NumberOfByte = CMD_DOWN_SIZE; break; /* CMD_SEL command received */ case CMD_SEL: NumberOfByte = CMD_SEL_SIZE; break; default: break; } /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(SPIx_TX_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Enable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE); /* Wait the SPI DMA transfers complete or time out */ while (DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET) {} TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } switch (NumberOfByte) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } /* Clear DMA1 global flags */ DMA_ClearFlag(SPIx_TX_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE); } #endif /* SPI_SLAVE */ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SPI configuration */ SPI_Config(); /* SysTick configuration */ SysTickConfig(); /* LEDs configuration */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); #ifdef SPI_MASTER /* Master board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI interrupt routine */ /* Configure the Tamper Button */ STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO); /* Wait until Tamper Button is pressed */ while (STM_EVAL_PBGetState(BUTTON_TAMPER)); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Initialize Buffer counters */ ubTxIndex = 0; ubRxIndex = 0; /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); #endif /* SPI_MASTER */ #ifdef SPI_SLAVE /* Slave board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI interrupt routine */ /* Initialize Buffer counters */ ubTxIndex = 0; ubRxIndex = 0; /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the Tx empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); #endif /* SPI_SLAVE */ /* Waiting the end of Data transfer */ while ((ubTxIndex < BUFFERSIZE) && (ubRxIndex < BUFFERSIZE)) { } /* Disable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, DISABLE); /* Disable the Tx empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); if (Buffercmp(aTxBuffer, aRxBuffer, BUFFERSIZE) != FAILED) { /* Turn ON LED1 and LED3 */ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } else { /* Turn OFF LED1 and LED3 */ STM_EVAL_LEDOff(LED1); STM_EVAL_LEDOff(LED3); /* Turn ON LED2 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED4); } /* Infinite Loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32l1xx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32l1xx.c file */ /* SPI configuration ------------------------------------------------------*/ SPI_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM32L152-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Master board configuration ------------------------------------------------*/ #ifdef SPI_MASTER /* Initialize push-buttons mounted on STM32L152-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the SPI Error interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE); /* Data transfer is performed in the SPI interrupt routine */ /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); while (1) { CmdTransmitted = 0x00; CmdReceived = 0x00; CmdStatus = 0x00; Tx_Idx = 0x00; Rx_Idx = 0x00; /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); PressedButton = Read_Joystick(); while (PressedButton == JOY_NONE) { PressedButton = Read_Joystick(); } switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CmdTransmitted = CMD_RIGHT; NumberOfByte = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CmdTransmitted = CMD_LEFT; NumberOfByte = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CmdTransmitted = CMD_UP; NumberOfByte = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CmdTransmitted = CMD_DOWN; NumberOfByte = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CmdTransmitted = CMD_SEL; NumberOfByte = CMD_SEL_SIZE; break; default: break; } if (CmdTransmitted != 0x00) { /* Enable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Wait until end of data transfer or time out*/ TimeOut = USER_TIMEOUT; while ((Rx_Idx < GetVar_NbrOfData())&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } } switch (Rx_Idx) { /* Right button pressed */ case CMD_RIGHT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED4); } break; /* Left button pressed*/ case CMD_LEFT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* Up button pressed */ case CMD_UP_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* Down button pressed */ case CMD_DOWN_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* Sel button pressed */ case CMD_SEL_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) == PASSED) && (CmdReceived == CMD_ACK)) { /* Turn ON all LEDs */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } } #endif /* SPI_MASTER */ /* Slave board configuration ----------------------------------------------*/ #ifdef SPI_SLAVE /* Initializes the SPI communication */ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the SPI Error interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Infinite Loop */ while (1) { CmdStatus = 0x00; CmdReceived = 0x00; Rx_Idx = 0x00; Tx_Idx = 0x00; /* Write the first data in SPI shift register before enabling the interrupt this data will be transmitted when the Slave receive the generated clock by the Master */ /* Enable the Tx buffer empty interrupt */ SPI_I2S_SendData(SPIx, CMD_ACK); SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Waiting Transaction code Byte */ while (CmdStatus == 0x00) {} switch (CmdReceived) { /* CMD_RIGHT command received or time out*/ case CMD_RIGHT: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_RIGHT_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received or time out */ case CMD_LEFT: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_LEFT_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received or time out*/ case CMD_UP: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_UP_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received or time out */ case CMD_DOWN: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_DOWN_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received or time out */ case CMD_SEL: TimeOut = USER_TIMEOUT; while ((Rx_Idx < CMD_SEL_SIZE)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) != FAILED) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } #endif /* SPI_SLAVE */ }
/******************************************************************************* * Function Name :void TaskStart(void) * Description :任务启动 * Input : * Output : * Other : * Date :2012.04.18 11:48:23 *******************************************************************************/ static void TaskStart(void) { OS_ERR err; led_init(); usart1_init(115200); SPI_Config() ; SysTickInit(); OSTaskCreate( (OS_TCB *)&task1TCB, (CPU_CHAR *)"Task1", (OS_TASK_PTR)Task1, (void *)0, (OS_PRIO )TASK1_PRIO, (CPU_STK *)&task1_stk[0], (CPU_STK_SIZE)TASK1_STK_SIZE / 10, (CPU_STK_SIZE)TASK1_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSTaskCreate( (OS_TCB *)&task2TCB, (CPU_CHAR *)"Task2", (OS_TASK_PTR)Task2, (void *)0, (OS_PRIO ) TASK2_PRIO, (CPU_STK *)&task2_stk[0], (CPU_STK_SIZE)TASK2_STK_SIZE / 10, (CPU_STK_SIZE)TASK2_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSTaskCreate( (OS_TCB *)&task3TCB, (CPU_CHAR *)"Task3", (OS_TASK_PTR)Task3, (void *)0, (OS_PRIO )TASK3_PRIO, (CPU_STK *)&task3_stk[0], (CPU_STK_SIZE)TASK3_STK_SIZE / 10, (CPU_STK_SIZE)TASK3_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSTaskCreate( (OS_TCB *)&dispTCB, (CPU_CHAR *)"LCD display", (OS_TASK_PTR)MainTask, (void *)0, (OS_PRIO )Disp_PRIO, (CPU_STK *)&dispStk[0], (CPU_STK_SIZE)TASK4_STK_SIZE / 10, (CPU_STK_SIZE)TASK4_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSTaskCreate( (OS_TCB *)&schedTaskEx1TCB, (CPU_CHAR *)"Sched Task Example1", (OS_TASK_PTR)SchedEx1Task, (void *)0, (OS_PRIO )Disp_PRIO, (CPU_STK *)&dispStk[0], (CPU_STK_SIZE)SCHED_TASK_STK_SIZE / 10, (CPU_STK_SIZE)SCHED_TASK_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSTaskCreate( (OS_TCB *)&schedTaskEx2TCB, (CPU_CHAR *)"Sched Task Example1", (OS_TASK_PTR)SchedEx2Task, (void *)0, (OS_PRIO )Disp_PRIO, (CPU_STK *)&dispStk[0], (CPU_STK_SIZE)SCHED_TASK_STK_SIZE / 10, (CPU_STK_SIZE)SCHED_TASK_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void *)0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); OSSemCreate( (OS_SEM *)&taskSem, (CPU_CHAR *)"taskSem", (OS_SEM_CTR )0, (OS_ERR *)err); OSTaskDel( (OS_TCB *)&taskStartTCB, (OS_ERR *)&err); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. */ /* SPI configuration */ SPI_Config(); /* SysTick configuration */ SysTickConfig(); /* Initialize LEDs mounted on EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); #ifdef SPI_MASTER /* Master board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI using Direct Memory Access */ /* Enable DMA SPI TX Stream */ DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE); /* Enable DMA SPI RX Stream */ DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE); /* Enable SPI DMA TX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable SPI DMA RX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); /* Configure the Tamper Button */ STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO); /* Wait until Tamper Button is pressed */ while (STM_EVAL_PBGetState(BUTTON_TAMPER)); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); #endif /* SPI_MASTER */ #ifdef SPI_SLAVE /* Slave board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* Enable DMA SPI TX Stream */ DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE); /* Enable DMA SPI RX Stream */ DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE); /* Enable SPI DMA TX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable SPI DMA RX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); #endif /* SPI_SLAVE */ /* Waiting the end of Data transfer */ while (DMA_GetFlagStatus(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF)==RESET); while (DMA_GetFlagStatus(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF)==RESET); /* Clear DMA Transfer Complete Flags */ DMA_ClearFlag(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF); DMA_ClearFlag(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF); /* Disable DMA SPI TX Stream */ DMA_Cmd(SPIx_TX_DMA_STREAM,DISABLE); /* Disable DMA SPI RX Stream */ DMA_Cmd(SPIx_RX_DMA_STREAM,DISABLE); /* Disable SPI DMA TX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE); /* Disable SPI DMA RX Requsts */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); if (Buffercmp(aTxBuffer, aRxBuffer, BUFFERSIZE) != FAILED) { /* Turn ON LED1 and LED3 */ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } else { /* Turn OFF LED1 and LED3 */ STM_EVAL_LEDOff(LED1); STM_EVAL_LEDOff(LED3); /* Turn ON LED2 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED4); } /* Infinite Loop */ while (1) { } }
mdev_t *ssp_drv_open(SSP_ID_Type ssp_id, SSP_FrameFormat_Type format, SSP_MS_Type mode, SSP_DMA dma, int cs, bool level) { int ret; SSP_CFG_Type sspCfgStruct; SSP_FIFO_Type sspFifoCfg; SPI_Param_Type spiParaStruct; SSP_NWK_Type sspNetworkCfg; PSP_Param_Type pspParaStruct; sspdev_data_t *ssp_data_p; mdev_t *mdev_p = mdev_get_handle(mdev_ssp_name[ssp_id]); ssp_data_p = (sspdev_data_t *) mdev_p->private_data; if (mdev_p == NULL) { SSP_LOG("Unable to open device %s\r\n", mdev_ssp_name[ssp_id]); return NULL; } ssp_data_p->slave = mode; ret = os_mutex_get(&ssp_mutex[mdev_p->port_id], OS_WAIT_FOREVER); if (ret == -WM_FAIL) { SSP_LOG("failed to get mutex\r\n"); return NULL; } /* If ringbuffer size is not set by user then set to default size */ if (GET_RX_BUF_SIZE(ssp_data_p) == 0) { SET_RX_BUF_SIZE(ssp_data_p, SSP_RX_BUF_SIZE); } if (rx_buf_init(ssp_data_p)) { SSP_LOG("Unable to allocate ssp software ring buffer\r\n"); return NULL; } /* If clk is not set by user then set it to default */ if (ssp_data_p->freq == 0) { ret = ssp_drv_set_clk(mdev_p->port_id, DEFAULT_SSP_FREQ); } /* Configure the pinmux for ssp pins */ if (cs >= 0 && mode == SSP_MASTER) { board_ssp_pin_config(mdev_p->port_id, 0); /* Use user specified chip select pin */ ssp_data_p->cs = cs; ssp_data_p->cs_level = level; GPIO_PinMuxFun(cs, PINMUX_FUNCTION_0); GPIO_SetPinDir(cs, GPIO_OUTPUT); /* Initially keep slave de-selected */ GPIO_WritePinOutput(cs, !level); } else { board_ssp_pin_config(mdev_p->port_id, 1); } /* Configure SSP interface */ sspCfgStruct.mode = SSP_NORMAL; sspCfgStruct.masterOrSlave = mode; sspCfgStruct.trMode = SSP_TR_MODE; sspCfgStruct.dataSize = SSP_DATASIZE_8; sspCfgStruct.sfrmPola = SSP_SAMEFRM_PSP; sspCfgStruct.slaveClkRunning = SSP_SLAVECLK_TRANSFER; sspCfgStruct.txd3StateEnable = ENABLE; /* RXFIFO inactivity timeout, [timeout = 100 / 26MHz] (Bus clock) */ sspCfgStruct.timeOutVal = 100; switch (format) { case SSP_FRAME_SPI: sspCfgStruct.frameFormat = SSP_FRAME_SPI; sspCfgStruct.txd3StateType = SSP_TXD3STATE_ELSB; break; case SSP_FRAME_PSP: sspCfgStruct.frameFormat = SSP_FRAME_PSP; sspCfgStruct.txd3StateType = SSP_TXD3STATE_12SLSB; break; case SSP_FRAME_SSP: SSP_LOG("Frame Format not implemented.\r\n"); return NULL; } /* Configure SSP Fifo */ sspFifoCfg.fifoPackMode = DISABLE; /* See if dma needs to be enabled */ if (dma == DMA_ENABLE) { /* Enable DMA controller clock */ CLK_ModuleClkEnable(CLK_DMAC); sspFifoCfg.rxFifoFullLevel = SSP_DMA_FIFO_RX_THRESHOLD; sspFifoCfg.txFifoEmptyLevel = SSP_DMA_FIFO_TX_THRESHOLD; sspFifoCfg.rxDmaService = ENABLE; sspFifoCfg.txDmaService = ENABLE; ssp_data_p->dma = 1; sspCfgStruct.trailByte = SSP_TRAILBYTE_DMA; } else { sspFifoCfg.rxFifoFullLevel = SSP_FIFO_RX_THRESHOLD; sspFifoCfg.txFifoEmptyLevel = SSP_FIFO_TX_THRESHOLD; sspFifoCfg.rxDmaService = DISABLE; sspFifoCfg.txDmaService = DISABLE; sspCfgStruct.trailByte = SSP_TRAILBYTE_CORE; } /* Let the settings take effect */ SSP_Disable(mdev_p->port_id); SSP_Init(mdev_p->port_id, &sspCfgStruct); SSP_FifoConfig(mdev_p->port_id, &sspFifoCfg); /* Do frame format config */ switch (format) { case SSP_FRAME_SPI: spiParaStruct.spiClkPhase = SPI_SCPHA_1; spiParaStruct.spiClkPolarity = SPI_SCPOL_LOW; SPI_Config(mdev_p->port_id, &spiParaStruct); break; case SSP_FRAME_PSP: pspParaStruct.pspFsrtType = 0; pspParaStruct.pspClkMode = PSP_DRIVFALL_SAMPRISE_IDLELOW; pspParaStruct.pspFrmPola = PSP_SFRMP_LOW; pspParaStruct.pspEndTransState = PSP_ENDTRANS_LOW; pspParaStruct.startDelay = 0; pspParaStruct.dummyStart = 0; pspParaStruct.dummyStop = 0; pspParaStruct.frmDelay = 0; pspParaStruct.frmLength = 8; PSP_Config(mdev_p->port_id, &pspParaStruct); sspNetworkCfg.frameRateDiv = 1; sspNetworkCfg.txTimeSlotActive = 3; sspNetworkCfg.rxTimeSlotActive = 3; SSP_NwkConfig(mdev_p->port_id, &sspNetworkCfg); break; case SSP_FRAME_SSP: SSP_LOG("Frame Format not implemented.\r\n"); return NULL; } /* Enable read interrupts only for slave when dma is disabled*/ if (mode == SSP_SLAVE && dma == DMA_DISABLE) { install_int_callback(SSP_INT_BASE + mdev_p->port_id, SSP_INT_RFFI, ssp_read_irq_handler[mdev_p->port_id]); NVIC_EnableIRQ(SSP_IRQn_BASE + mdev_p->port_id); NVIC_SetPriority(SSP_IRQn_BASE + mdev_p->port_id, 0xF); SSP_IntMask(mdev_p->port_id, SSP_INT_RFFI, UNMASK); } SSP_Enable(mdev_p->port_id); return mdev_p; }
/** * @brief Main program. * @param None * @retval None */ void main(void) { uint32_t index = 0; /* CLK configuration -------------------------------------------*/ CLK_Config(); /* Set the MOSI and SCK at high level */ GPIO_ExternalPullUpConfig(GPIOB, GPIO_Pin_6 | GPIO_Pin_5, ENABLE); #ifdef USE_STM8L1526_EVAL /* Set USART CK at high level */ GPIO_ExternalPullUpConfig(GPIOC, GPIO_Pin_4, ENABLE); #else /* USE_STM8L1528_EVAL is defined */ /* Set USART CK at high level */ GPIO_ExternalPullUpConfig(GPIOH, GPIO_Pin_6, ENABLE); #endif /* USE_STM8L1526_EVAL */ /* USART configuration -------------------------------------------*/ USART_Config(); /* SPI configuration -------------------------------------------*/ SPI_Config(); /*Disable USART the master*/ USART_Cmd(EVAL_COM1, DISABLE); /* DMA configuration -------------------------------------------*/ DMA_Config(); /* Enable USART */ USART_Cmd(EVAL_COM1, ENABLE); /* Insert Delay to ensure Synchro w/ SPI */ for (index = 0; index < 0xFF; index++); /* Enable SPI */ SPI_Cmd(SPI1, ENABLE); USART_ITConfig(EVAL_COM1, USART_IT_TC, ENABLE); /* Wait until Data transmitted to SPI*/ while (UsartTransferStatus != 2); /* Wait the SPI DMA Rx transfer complete */ while (DMA_GetFlagStatus((DMA_FLAG_TypeDef)SPI_DMA_FlagTCRx) == RESET); /* Check the correctness of written dada */ TransferStatus1 = Buffercmp((uint8_t*)RxBuffer1, SPIBuffer_Rx, RX_BUFFER_SIZE); /* TransferStatus1 = PASSED, if the received data by USART and received data by SPI1 are the same */ /* TransferStatus1 = FAILED, if the received data by USART and received data by SPI1 are different */ if (TransferStatus1 != FAILED) { for (index = 0; index < MESSAGE2_SIZE; index++) { /* Wait while USART TC = 0 */ while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET); /* Send on byte from stm8l15x USART to HyperTerminal */ USART_SendData8(EVAL_COM1, Message2[index]); } } else { for (index = 0; index < MESSAGE3_SIZE; index++) { /* Wait while USART TC = 0 */ while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET); /* Send on byte from stm8l15x USART to HyperTerminal */ USART_SendData8(EVAL_COM1, Message3[index]); } } while (1) {} }
int main(void) { FRESULT fresult; NVIC_InitTypeDef nvicStructure; SystemInit(); GPIO_Config(); SPI_Config(); /* for(i1=0;i1<TABLE_SIZE;i1++){ buffer[0][i1] = 4*i1; } for(i1=0;i1<TABLE_SIZE;i1++){ buffer[1][i1] = 2047*sin(currentPhase)+2047; currentPhase += phaseIncrement; } */ /* Configure LED3 and LED4 on STM32L100C-Discovery */ STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* SysTick end of count event each 1ms */ //RCC_GetClocksFreq(&RCC_Clocks); // SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); //delay_init(72);// delay_init(16); Lcd_Init2(); Lcd_Clear2(BLACK); /* RTC configuration */ //RTC_Config(); RTC_Config32768Internal(); /* Configure RTC alarm A register to generate 8 interrupts per 1 Second */ RTC_AlarmConfig(); tft_puts(45, 20, "12:00", white, black); RCC_ClocksTypeDef RCC_Clocks; /* Initialize User_Button on STM32L100C-Discovery */ STM_EVAL_PBInit(BUTTON_MODE, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); //STM_EVAL_PBInit(BUTTON_MODE, BUTTON_MODE_EXTI); /* SysTick end of count event each 1ms */ // RCC_GetClocksFreq(&RCC_Clocks); // SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); /*************************/ //TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); nvicStructure.NVIC_IRQChannel = TIM2_IRQn; nvicStructure.NVIC_IRQChannelPreemptionPriority = 0; nvicStructure.NVIC_IRQChannelSubPriority = 1; nvicStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicStructure); TIM_TimeBaseInitTypeDef timerInitStructure; timerInitStructure.TIM_Prescaler = 3200; //32 MHz/3200 = 10000 timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //timerInitStructure.TIM_Period = 3332; timerInitStructure.TIM_Period = 100; //10 000/100 = 100 Hz - sampling frequency of buttons timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //timerInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &timerInitStructure); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_Cmd(TIM2, ENABLE); /*************************/ //GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); fresult = f_mount(&g_sFatFs, "0:0", 1); //LCD_BMP("kasia.bmp"); //Gui_DrawFont_GBK24_bk(20,120, BLUE, WHITE, "1234 abcd"); //playWav("rct3.wav"); // fsamp 22050 Hz, 8bit /* tests */ //playWav("m8m.wav"); // fsamp 44100 Hz, 8 bit //playWav("bj8.wav"); // fsamp 44100 Hz, 8 bit //playWav("im16.wav"); // fsamp 44100 Hz, 16 bit //playWavFromIntMemory(rooster3); displayDate(); while (1) { if (updated) { displayTime(); updated = false; if(updateDate){ updateAndDisplayDate(); updateDate = false; } } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { __IO uint16_t i; __IO uint16_t *ptr; __IO uint16_t tmp1; __IO uint16_t tmp2; uint32_t scpcnt; uint32_t scpwait; uint32_t scptpos; /* RCC Configuration */ RCC_Config(); /* GPIO Configuration */ GPIO_Config(); /* TIM Configuration */ TIM_Config(); /* NVIC Configuration */ NVIC_Config(); /* DAC Configuration */ DAC_Config(); /* SPI Configuration */ SPI_Config(); /* USART Configuration */ USART_Config(115200); /* Calibrate LC Meter */ LCM_Calibrate(); // /* Update bluetooth baudrate */ // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // USART3_puts("AT\0"); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount) // { // if ((USART3->SR & USART_FLAG_RXNE) != 0) // { // STM32_CMD.BTBuff[i] = USART3->DR; // i++; // } // } // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount); // USART3_puts("AT+BAUD8\0"); // STM32_CMD.Cmd = STM32_CMD.TickCount; // while (STM32_CMD.Cmd == STM32_CMD.TickCount) // { // if ((USART3->SR & USART_FLAG_RXNE) != 0) // { // STM32_CMD.BTBuff[i] = USART3->DR; // i++; // } // } // while (1) // { // } while (1) { USART3_getdata((uint8_t *)&STM32_CMD.Cmd,4); switch (STM32_CMD.Cmd) { case CMD_LCMCAL: LCM_Calibrate(); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_LCMCAP: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_LCMIND: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_6); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_putdata((uint8_t *)&STM32_CMD.STM32_LCM.FrequencyCal0,sizeof(STM32_LCMTypeDef)); break; case CMD_FRQCH1: GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_FRQCH2: GPIO_ResetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_1); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_FRQCH3: GPIO_ResetBits(GPIOD, GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_SCPSET: USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP.ADC_Prescaler,sizeof(STM32_SCPTypeDef)); /* Scope magnify */ i = (STM32_CMD.STM32_SCP.ScopeMag & 0x07) << 3; GPIO_SetBits(GPIOE,(i ^ 0x38)); GPIO_ResetBits(GPIOE,i); /* Set V-Pos */ DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeVPos); /* Set Trigger level */ DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP.ScopeTriggerLevel); if (STM32_CMD.STM32_SCP.ADC_TripleMode) { /* DMA Configuration */ DMA_TripleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize); /* ADC Configuration */ ADC_TripleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler, STM32_CMD.STM32_SCP.ADC_TwoSamplingDelay); } else { /* DMA Configuration */ DMA_SingleConfig(STM32_CMD.STM32_SCP.ADC_SampleSize); /* ADC Configuration */ ADC_SingleConfig(STM32_CMD.STM32_SCP.ADC_Prescaler,STM32_CMD.STM32_SCP.ADC_SampleTime); } if (STM32_CMD.STM32_SCP.ScopeTrigger == 2) { /* Rising edge */ TIM5->CCER &= ~0x2; } else { /* Falling edge */ TIM5->CCER |= 0x2; } if (STM32_CMD.STM32_SCP.ScopeTrigger) { /* Wait for trigger */ scpcnt = TIM5->CNT; scpwait = STM32_CMD.TickCount + 2; while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount); } /* Start ADC1 Software Conversion */ ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART; i = 0; while (i < 30000) { i++; } /* Since BlueTooth is slower than the lowest sampling rate there is no need to wait */ SendCompressedBuffer((uint32_t *)SCOPE_DATAPTR, STM32_CMD.STM32_SCP.ADC_SampleSize / 4); /* Done */ ADC->CCR=0; ADC1->CR2=0; ADC2->CR2=0; ADC3->CR2=0; break; case CMD_SCP2SET: STM_EVAL_LEDToggle(LED4); USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); USART3_getdata((uint8_t *)&STM32_CMD.STM32_SCP2.SampleRateSet,sizeof(STM32_SCP2TypeDef)); /* Scope magnify */ i = ((uint32_t)STM32_CMD.STM32_SCP2.Mag & 0x07) << 3; GPIO_SetBits(GPIOE,(i ^ 0x38)); GPIO_ResetBits(GPIOE,i); /* Set V-Pos */ DAC_SetChannel1Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.VPos); /* Set Trigger level */ DAC_SetChannel2Data(DAC_Align_12b_R, STM32_CMD.STM32_SCP2.TriggerLevel); /* Get number of samples needed */ SampleSize = GetScopeSampleSize(); if (STM32_CMD.STM32_SCP2.Triple) { /* DMA Configuration */ DMA_TripleConfig(SampleSize); /* ADC Configuration */ ADC_TripleConfig((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 4,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0xF); } else { /* DMA Configuration */ DMA_SingleConfig(SampleSize); /* ADC Configuration */ ADC_SingleConfig(((uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet >> 3) & 0x3,(uint32_t)STM32_CMD.STM32_SCP2.SampleRateSet & 0x7); } /* Trigger configuration */ if (STM32_CMD.STM32_SCP2.Trigger == 2) { /* Rising edge */ TIM5->CCER &= ~0x2; } else { /* Falling edge */ TIM5->CCER |= 0x2; } if (STM32_CMD.STM32_SCP2.Trigger) { /* Wait for trigger */ scpcnt = TIM5->CNT; scpwait = STM32_CMD.TickCount + 2; while (scpcnt == TIM5->CNT && scpwait != STM32_CMD.TickCount); } /* Start ADC1 Software Conversion */ ADC1->CR2 |= (uint32_t)ADC_CR2_SWSTART; /* Reset wavedata */ ScopeResetWave((uint32_t *)SCOPE_WAVEPTR,SCOPE_MAXWAVESIZE); ScopeResetWave((uint32_t *)SCOPE_COUNTPTR,SCOPE_MAXWAVESIZE); /* Wait until DMA transfer complete */ while (DMA_GetFlagStatus(DMA2_Stream0, DMA_FLAG_TCIF0) == RESET); /* Done sampling */ ADC->CCR=0; ADC1->CR2=0; ADC2->CR2=0; ADC3->CR2=0; ScopeSetWaveData((uint32_t *)SCOPE_WAVEPTR, (uint32_t *)SCOPE_COUNTPTR, (uint16_t *)SCOPE_DATAPTR); scptpos = 0; if (STM32_CMD.STM32_SCP2.Trigger) { scptpos = ScopeFindTrigger((uint16_t *)SCOPE_DATAPTR); } /* Send wave data */ SendCompressedBuffer((uint32_t *)(SCOPE_DATAPTR + scptpos * 2), (uint32_t)STM32_CMD.STM32_SCP2.PixDiv * (uint32_t)STM32_CMD.STM32_SCP2.nDiv * 2 / 4); STM_EVAL_LEDToggle(LED4); break; case CMD_HSCSET: GPIO_ResetBits(GPIOD, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6 | GPIO_Pin_7); GPIO_SetBits(GPIOD, GPIO_Pin_0); USART3_getdata((uint8_t *)&STM32_CMD.STM32_HSC.HSCSet,sizeof(STM32_HSCTypeDef)); TIM4->ARR = STM32_CMD.STM32_HSC.HSCSet; TIM4->CCR2 = (STM32_CMD.STM32_HSC.HSCSet+1) / 2; TIM4->PSC = STM32_CMD.STM32_HSC.HSCDiv; USART3_putdata((uint8_t *)&STM32_CMD.STM32_FRQ.Frequency,sizeof(STM32_FRQTypeDef)); break; case CMD_DDSSET: USART3_getdata((uint8_t *)&STM32_CMD.STM32_DDS.DDS_Cmd,sizeof(STM32_DDSTypeDef)); if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_PHASESET) { SPISendData(DDS_PHASESET); SPISendData32(STM32_CMD.STM32_DDS.DDS__PhaseAdd); } else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_WAVESET) { SPISendData(DDS_WAVESET); SPISendData(STM32_CMD.STM32_DDS.DDS_Wave); SPISendData(STM32_CMD.STM32_DDS.DDS_Amplitude); SPISendData(STM32_CMD.STM32_DDS.DDS_DCOffset); } else if (STM32_CMD.STM32_DDS.DDS_Cmd == DDS_SWEEPSET) { SPISendData(DDS_SWEEPSET); SPISendData(STM32_CMD.STM32_DDS.SWEEP_Mode); SPISendData(STM32_CMD.STM32_DDS.SWEEP_Time); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Step); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Min); SPISendData32(STM32_CMD.STM32_DDS.SWEEP_Max); } break; case CMD_LGASET: USART3_getdata((uint8_t *)&STM32_CMD.STM32_LGA.DataBlocks,sizeof(STM32_LGATypeDef)); /* Set the Prescaler value */ TIM8->PSC = STM32_CMD.STM32_LGA.LGASampleRateDiv; /* Set the Autoreload value */ TIM8->ARR = STM32_CMD.STM32_LGA.LGASampleRate; TIM8->CNT = STM32_CMD.STM32_LGA.LGASampleRate-1; DMA_LGAConfig(); TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE); /* DMA2_Stream1 enable */ DMA_Cmd(DMA2_Stream1, ENABLE); /* Enable timer */ TIM8->CR1 |= TIM_CR1_CEN; while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_HTIF1) == RESET); /* Half done */ USART3_putdata((uint8_t *)LGA_DATAPTR, STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2); while (DMA_GetFlagStatus(DMA2_Stream1,DMA_FLAG_TCIF1) == RESET); /* Done */ USART3_putdata((uint8_t *)(LGA_DATAPTR + STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2), STM32_CMD.STM32_LGA.DataBlocks * 1024 / 2); TIM_Cmd(TIM8, DISABLE); DMA_DeInit(DMA2_Stream1); break; case CMD_WAVEUPLOAD: USART3_getdata((uint8_t *)WAVE_DATAPTR,4096); SPISendData(DDS_WAVEUPLOAD); ptr = (uint16_t *)WAVE_DATAPTR; i = 2048; while (i--) { SPISendData(*ptr); ptr++; } STM_EVAL_LEDToggle(LED4); break; }
int main( void ) { uart_config_t uart0; gpio_config_t key1; i2c_config_t i2c0; i2c_command_t command; spi_config_t spi0; spi_command_t spi_cmd; adc_config_t adc5; dac_config_t dac0; error_code_t error; uart0.baudrate = B115200; uart0.block_type = NON_BLOCKING; uart0.buffer = 0; uart0.irqhandler = 0; uart0.parity = UART_PARITY_NONE; uart0.uart_port = COM0; uart0.wordlen = UART_WORDLEN8; uart0.stopbit = STOP_BIT_1_BIT; key1.Direction = INPUT; key1.Initial_Value = LO; key1.Interrupt_Type = INTERRUPT_ENABLED_BOTH; key1.Pin = PIN11; key1.Port= PORT2; key1.Pin_Mode = PULLUP_PULLDOWN_DISABLED; key1.Pin_Mode_OD = PIN_MODE_OPEN_DRAIN_NORMAL; key1.Pin_Typedef = 0; key1.irqhandler = 0; i2c0.i2c_port = I2C0; i2c0.i2c_mode = MASTER; i2c0.datarate = STANDARD; i2c0.buffer = 0; i2c0.irqhandler = 0; spi0.bits = SPI_8_BITS; spi0.buffer = 0; spi0.clk_phase = SPI_PHASE_INPHASE; spi0.clk_polarity = SPI_CLK_RISING; spi0.freq = 5e6; spi0.irqhandler = 0; spi0.lsbf = MSB_FIRST; spi0.mode = SPI_MASTER; spi0.port = SPI0; spi0.dummyData = 0x00; adc5.channel = ADC_CHANNEL5; adc5.rate = 200e3; adc5.trigger_mode = BURST; adc5.irqhandler = 0; dac0.sampling_rate = 32e3; if ( (error = GPIO_Config(&key1)) != NO_ERROR ) { while (1); } signal_level_t trigger_mode; GPIO_GetLevel(&key1, &trigger_mode); if (trigger_mode == HI) { adc5.trigger_mode = BURST; } else { adc5.trigger_mode = MANUAL; } if ( (error = UART_Config(&uart0)) != NO_ERROR) { while (1); } SetDebug_Port(COM0); if ( (error = I2C_Config(&i2c0)) != NO_ERROR ) { while (1); } if ( (error = SPI_Config(&spi0)) != NO_ERROR ) { while (1); } if ( (error = ADC_Config(&adc5)) != NO_ERROR ) { while (1); } if ( (error = DAC_Config(&dac0)) != NO_ERROR ) { while (1); } uc_printf ("HardWare Initialized\n\r"); /* READ ID USING READ-ID */ //Write Register. spi_cmd.writeReg = 0x90; spi_cmd.writeRegValid = 1; uint16_t write_data[] = {0x00, 0x00, 0x00 }; spi_cmd.writeBuffer = write_data; spi_cmd.writeDataSize = sizeof(write_data); //Read Register. uint16_t read_data[4]; spi_cmd.readReg = 0x00; spi_cmd.readRegValid = 0; spi_cmd.readBuffer = read_data; spi_cmd.readDataSize = sizeof(read_data); spi_cmd.operation = SPI_WRITE | SPI_READ; //back to back. error = SPI_Write(SPI0, &spi_cmd); if (error != NO_ERROR) { while (1); } uc_printf ("SPI Flash Device ID(READ-ID) %d %d %d %d\n\r", read_data[0], read_data[1], read_data[2], read_data[3]); /* READ ID USING JEDEC READ */ uint16_t read_data_jedec[4]; spi_cmd.readReg = 0x9F; spi_cmd.readRegValid = 1; spi_cmd.readBuffer = read_data_jedec; spi_cmd.readDataSize = sizeof(read_data_jedec); error = SPI_Read(SPI0, &spi_cmd); if (error != NO_ERROR) { while (1); } uc_printf ("SPI Flash Device ID(Jedec) %d %d %d %d\n\r", read_data_jedec[0], read_data_jedec[1], read_data_jedec[2], read_data_jedec[3]); int size = 18; char fw_version[18] = "PowerAvrVersion5.5"; char fw_version_read[18] = { 0 }; command.address = 0x50; command.data = fw_version; command.operation = WRITE; command.reg = 0x00; command.size = 1; //11; int i, j, k; for (j=0; j<1; j++) { for (i=0; i<size;i++) { //UART_PutChars(COM0,"Writing", 9); command.reg = 0x00 + i; command.size = 1; command.data = &(fw_version[i]); error = I2C_Write(I2C0, &command); if (error != NO_ERROR) { break; } //This is to cause a delay after write. This should be replaced with the wait in the I2C_Write Operation. for (k=0; k<16; k++) { uc_printf("."); uc_printf("\b"); } } if (error != NO_ERROR) { while (1); } } uc_printf("I2C Write Done\n\r"); command.operation = READ; command.data = fw_version_read; command.size = 1 ; for (j=0; j<1; j++) { for (i=0; i<size;i++) { fw_version_read[i] = 'X'; } for (i=0; i<size;i++) { command.reg = 0x00 + i; command.size = 1; command.data = &(fw_version_read[i]); error = I2C_Read(I2C0, &command); if (error != NO_ERROR) { break; } } if (error != NO_ERROR) { while (1); } } uc_printf("I2C Read Done\n\r"); for (i=0; i<size; i++) { uc_printf("%c",fw_version_read[i]); } uc_printf("\n\r"); pin_interrupt_type_t key1_status; float vol=0; int timeout = 0; while (1) { error = GPIO_GetIRQ(&key1, &key1_status); if (error != NO_ERROR) { while (1); } if (key1_status == INTERRUPT_ENABLED_FALLING) { uc_printf ("Falling Edge\n\r"); error = GPIO_ClrIRQ(&key1); if (error != NO_ERROR) { while (1); } if ( (error = ADC_Read(&adc5)) != NO_ERROR ) { while (1); } } else if (key1_status == INTERRUPT_ENABLED_RISING) { uc_printf ("Rising Edge\n\r"); error = GPIO_ClrIRQ(&key1); if (error != NO_ERROR) { while (1); } } //#if (DISPLAY_ADC == 1) //if (adc5.trigger_mode == BURST) { if (timeout == 20000) { ADC_Read(&adc5); timeout = 0; } else { timeout++; } //} if (adc5.done) { vol =0 ; int i=0; int result = adc5.result; for (i=0; i<4096; i+=256) { vol++; if (result >= i && result <= (i + 256) ) { break; } } /* for (i=0; i<adc5.result/33; i++) { uc_printf("*"); } uc_printf("\r"); */ //uc_printf ("%d\n\r", adc5.result); adc5.done = 0; } //#endif //#if (PLAY_PIANO == 1) if (vol == 0) { vol = 1; } else if (vol > 1) { //vol = vol ; //1 + (vol - 1)*0.2; } Play_Piano(&dac0, vol ); //#endif } #if (UART_TEST == 1) char rx_data; char tx_data[] = "This is x nnnnn\n\r"; while (1) { //if ( UART_GetChar(COM0, &rx_data) != FIFO_EMPTY ) { tx_data[8] = 'a'; //UART_PutChar(COM0, rx_data); UART_PutChars(COM0, tx_data, 17 ); int i; tx_data[8] = 'b'; for (i=0; i<17; i++) { UART_PutChar(COM0, tx_data[i]); } //} } #endif return 0; }