void TM_PCD8544_InitIO(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Enable clock for all pins */ RCC_AHB1PeriphClockCmd(PCD8544_RST_RCC | PCD8544_CE_RCC | PCD8544_DC_RCC, ENABLE); /* Common settings for all pins */ GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; /* RST pin */ GPIO_InitStruct.GPIO_Pin = PCD8544_RST_PIN; GPIO_Init(PCD8544_RST_PORT, &GPIO_InitStruct); /* DC pin */ GPIO_InitStruct.GPIO_Pin = PCD8544_DC_PIN; GPIO_Init(PCD8544_DC_PORT, &GPIO_InitStruct); /* CE pin */ GPIO_InitStruct.GPIO_Pin = PCD8544_CE_PIN; GPIO_Init(PCD8544_CE_PORT, &GPIO_InitStruct); /* Reset pin HIGH */ GPIO_SetBits(PCD8544_RST_PORT, PCD8544_RST_PIN); /* CE HIGH */ PCD8544_CE_HIGH; /* Initialize SPI */ TM_SPI_Init(PCD8544_SPI, PCD8544_SPI_PINSPACK); }
/* Public */ TM_L3GD20_Result_t TM_L3GD20_Init(TM_L3GD20_Scale_t scale) { /* Init CS pin */ TM_L3GD20_INT_InitPins(); /* Init SPI */ TM_SPI_Init(L3GD20_SPI, L3GD20_SPI_PINSPACK); /* Check if sensor is L3GD20 */ if (TM_L3GD20_INT_ReadSPI(L3GD20_REG_WHO_AM_I) != L3GD20_WHO_AM_I) { /* Sensor connected is not L3GD20 */ return TM_L3GD20_Result_Error; } /* Enable L3GD20 Power bit */ TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG1, 0xFF); /* Set L3GD20 scale */ if (scale == TM_L3GD20_Scale_250) { TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG4, 0x00); } else if (scale == TM_L3GD20_Scale_500) { TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG4, 0x10); } else if (scale == TM_L3GD20_Scale_2000) { TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG4, 0x20); } /* Save scale */ TM_L3GD20_INT_Scale = scale; /* Set high-pass filter settings */ TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG2, 0x00); /* Enable high-pass filter */ TM_L3GD20_INT_WriteSPI(L3GD20_REG_CTRL_REG5, 0x10); /* Everything OK */ return TM_L3GD20_Result_Ok; }
uint8_t TM_NRF24L01_Init(uint8_t channel, uint8_t payload_size) { //Initialize CE and CSN pins TM_NRF24L01_InitPins(); //Initialize SPI TM_SPI_Init(NRF24L01_SPI, NRF24L01_SPI_PINS); //Max payload is 32bytes if (payload_size > 32) { payload_size = 32; } TM_NRF24L01_Struct.Channel = channel; TM_NRF24L01_Struct.PayloadSize = payload_size; TM_NRF24L01_Struct.OutPwr = TM_NRF24L01_OutputPower_0dBm; TM_NRF24L01_Struct.DataRate = TM_NRF24L01_DataRate_2M; //Reset nRF24L01+ to power on registers values TM_NRF24L01_SoftwareReset(); //Channel select TM_NRF24L01_SetChannel(TM_NRF24L01_Struct.Channel); //Set pipeline to max possible 32 bytes TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P0, TM_NRF24L01_Struct.PayloadSize); // Auto-ACK pipe TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P1, TM_NRF24L01_Struct.PayloadSize); // Data payload pipe TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P2, 0x00); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P3, 0x00); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P4, 0x00); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P5, 0x00); //Set RF settings (2mbps, output power) TM_NRF24L01_SetRF(TM_NRF24L01_Struct.DataRate, TM_NRF24L01_Struct.OutPwr); //Config register TM_NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, NRF24L01_CONFIG); //Enable auto-acknowledgment for all pipes TM_NRF24L01_WriteRegister(NRF24L01_REG_EN_AA, 0xFF); // Enable RX addresses TM_NRF24L01_WriteRegister(NRF24L01_REG_EN_RXADDR, 0xFF); //Auto retransmit delay: 1000 (4x250) us and Up to 15 retransmit trials TM_NRF24L01_WriteRegister(NRF24L01_REG_SETUP_RETR, 0x4F); // Dynamic length configurations: No dynamic length TM_NRF24L01_WriteRegister(NRF24L01_REG_DYNPD, (0 << NRF24L01_DPL_P0) | (0 << NRF24L01_DPL_P1) | (0 << NRF24L01_DPL_P2) | (0 << NRF24L01_DPL_P3) | (0 << NRF24L01_DPL_P4) | (0 << NRF24L01_DPL_P5)); //Clear FIFOs NRF24L01_FLUSH_TX; NRF24L01_FLUSH_RX; //Go to RX mode TM_NRF24L01_PowerUpRx(); return 1; }
void TM_MFRC522_Init(void) { TM_MFRC522_InitPins(); TM_SPI_Init(MFRC522_SPI, MFRC522_SPI_PINSPACK); TM_MFRC522_Reset(); TM_MFRC522_WriteRegister(MFRC522_REG_T_MODE, 0x8D); TM_MFRC522_WriteRegister(MFRC522_REG_T_PRESCALER, 0x3E); TM_MFRC522_WriteRegister(MFRC522_REG_T_RELOAD_L, 30); TM_MFRC522_WriteRegister(MFRC522_REG_T_RELOAD_H, 0); TM_MFRC522_WriteRegister(MFRC522_REG_TX_AUTO, 0x40); TM_MFRC522_WriteRegister(MFRC522_REG_MODE, 0x3D); TM_MFRC522_AntennaOn(); //Open the antenna }
/** ------------------------------------ GRAPHIC LIBRARY FUNCTIONS ------------------------------------ */ TM_GRAPHIC_Result TM_GRAPHICLCDDriver_Init(TM_GRAPHIC_Options_t* LCD_Options) { //Initialize pins used TM_GRAPHIC_ILI9341_InitPins(); //SPI chip select high ILI9341_CS_SET; //Init SPI TM_SPI_Init(ILI9341_SPI, ILI9341_SPI_PINS); //SDram Init TM_SDRAM_Init(); //Initialize LCD for LTDC TM_GRAPHIC_ILI9341_InitLCD(); //Initialize LTDC TM_LCD9341_InitLTDC(); //Initialize LTDC layers TM_GRAPHIC_ILI9341_InitLayers(); //Set options for GRAPHIC library LCD_Options->DefaultWidth = ILI9341_WIDTH; LCD_Options->DefaultHeight = ILI9341_HEIGHT; LCD_Options->PixelsCount = ILI9341_PIXEL; //Current layer is layer 1 ILI9341_LTDC_Opts.CurrentLayer = 0; //Start on the beginning of SDRAM ILI9341_LTDC_Opts.CurrentLayerOffset = 0; //Set layer opacity to maximum, 255 ILI9341_LTDC_Opts.Layer1Opacity = 255; //Hide layer 2, set opacity to 0 ILI9341_LTDC_Opts.Layer2Opacity = 0; //Set layer 1 TM_GRAPHIC_ILI9341_SetLayer1(); //Fill layer 1 with white color TM_GRAPHICLCDDriver_Fill(LCD_Options, GRAPHIC_COLOR_WHITE); //Set layer 2 TM_GRAPHIC_ILI9341_SetLayer2(); //Fill layer 2 with white color TM_GRAPHICLCDDriver_Fill(LCD_Options, GRAPHIC_COLOR_WHITE); //Set back to layer 1 TM_GRAPHIC_ILI9341_SetLayer1(); /* Return OK */ return TM_GRAPHIC_OK; }
void PCD8544_InitIO(void) { GPIO_InitTypeDef GPIO_InitDef; //Enable clock RCC_AHB1PeriphClockCmd(PCD8544_GPIO_RCC, ENABLE); //Common settings for all pins GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitDef.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitDef.GPIO_Pin = PCD8544_GPIO_RST_Pin | PCD8544_GPIO_DC_Pin | PCD8544_GPIO_CE_Pin; GPIO_Init(PCD8544_GPIO_Port, &GPIO_InitDef); GPIO_SetBits(PCD8544_GPIO_Port, PCD8544_GPIO_RST_Pin); TM_SPI_Init(PCD8544_SPIx, PCD8544_SPI_PINSPACK); PCD8544_CE_HIGH; }
void TM_ILI9341_Init() { GPIO_InitTypeDef GPIO_InitDef; RCC_AHB1PeriphClockCmd(ILI9341_WRX_CLK, ENABLE); GPIO_InitDef.GPIO_Pin = ILI9341_WRX_PIN; GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitDef.GPIO_OType = GPIO_OType_PP; GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(ILI9341_WRX_PORT, &GPIO_InitDef); RCC_AHB1PeriphClockCmd(ILI9341_CS_CLK, ENABLE); GPIO_InitDef.GPIO_Pin = ILI9341_CS_PIN; GPIO_Init(ILI9341_CS_PORT, &GPIO_InitDef); RCC_AHB1PeriphClockCmd(ILI9341_RST_CLK, ENABLE); GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitDef.GPIO_Pin = ILI9341_RST_PIN; GPIO_Init(ILI9341_RST_PORT, &GPIO_InitDef); ILI9341_CS_SET; TM_SPI_Init(ILI9341_SPI, ILI9341_SPI_PINS); TM_ILI9341_InitLCD(); ILI9341_x = ILI9341_y = 0; ILI9341_Opts.width = ILI9341_WIDTH; ILI9341_Opts.height = ILI9341_HEIGHT; ILI9341_Opts.orientation = TM_ILI9341_Portrait; TM_ILI9341_Fill(ILI9341_COLOR_WHITE); }
void TM_ILI9341_Init() { //Initialize pins used TM_ILI9341_InitPins(); //SPI chip select high ILI9341_CS_SET; //Init SPI TM_SPI_Init(ILI9341_SPI, ILI9341_SPI_PINS); //Init SDRAM TM_DISCO_LedInit(); if (!TM_SDRAM_Init()) { TM_DISCO_LedOn(LED_RED); } //Initialize LCD for LTDC TM_ILI9341_InitLCD(); //Initialize LTDC TM_LCD9341_InitLTDC(); //Initialize LTDC layers TM_ILI9341_InitLayers(); //Set cursor X and Y ILI9341_x = ILI9341_y = 0; ILI9341_Opts.Width = ILI9341_WIDTH; ILI9341_Opts.Height = ILI9341_HEIGHT; ILI9341_Opts.Orientation = TM_ILI9341_Portrait; ILI9341_Opts.Orient = TM_ILI9341_Orientation_Portrait_1; ILI9341_Opts.CurrentLayer = 0; ILI9341_Opts.CurrentLayerOffset = 0; ILI9341_Opts.Layer1Opacity = 255; ILI9341_Opts.Layer2Opacity = 0; TM_ILI9341_SetLayer1(); TM_ILI9341_Fill(ILI9341_COLOR_WHITE); TM_ILI9341_SetLayer2(); TM_ILI9341_Fill(ILI9341_COLOR_WHITE); TM_ILI9341_SetLayer1(); }
uint8_t TM_NRF24L01_Init(uint8_t channel, uint8_t payload_size) { /* Initialize CE and CSN pins */ TM_NRF24L01_InitPins(); /* Initialize SPI */ TM_SPI_Init(NRF24L01_SPI, NRF24L01_SPI_PINS); /* Max payload is 32bytes */ if (payload_size > 32) { payload_size = 32; } /* Fill structure */ TM_NRF24L01_Struct.Channel = !channel; /* Set channel to some different value for TM_NRF24L01_SetChannel() function */ TM_NRF24L01_Struct.PayloadSize = payload_size; TM_NRF24L01_Struct.OutPwr = TM_NRF24L01_OutputPower_0dBm; TM_NRF24L01_Struct.DataRate = TM_NRF24L01_DataRate_2M; /* Reset nRF24L01+ to power on registers values */ TM_NRF24L01_SoftwareReset(); /* Channel select */ TM_NRF24L01_SetChannel(channel); /* Set pipeline to max possible 32 bytes */ TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P0, TM_NRF24L01_Struct.PayloadSize); // Auto-ACK pipe TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P1, TM_NRF24L01_Struct.PayloadSize); // Data payload pipe TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P2, TM_NRF24L01_Struct.PayloadSize); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P3, TM_NRF24L01_Struct.PayloadSize); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P4, TM_NRF24L01_Struct.PayloadSize); TM_NRF24L01_WriteRegister(NRF24L01_REG_RX_PW_P5, TM_NRF24L01_Struct.PayloadSize); /* Set RF settings (2mbps, output power) */ TM_NRF24L01_SetRF(TM_NRF24L01_Struct.DataRate, TM_NRF24L01_Struct.OutPwr); /* Config register */ TM_NRF24L01_WriteRegister(NRF24L01_REG_CONFIG, NRF24L01_CONFIG); /* Enable auto-acknowledgment for all pipes */ TM_NRF24L01_WriteRegister(NRF24L01_REG_EN_AA, 0x3F); /* Enable RX addresses */ TM_NRF24L01_WriteRegister(NRF24L01_REG_EN_RXADDR, 0x3F); /* Auto retransmit delay: 1000 (4x250) us and Up to 15 retransmit trials */ TM_NRF24L01_WriteRegister(NRF24L01_REG_SETUP_RETR, 0x4F); /* Dynamic length configurations: No dynamic length */ TM_NRF24L01_WriteRegister(NRF24L01_REG_DYNPD, (0 << NRF24L01_DPL_P0) | (0 << NRF24L01_DPL_P1) | (0 << NRF24L01_DPL_P2) | (0 << NRF24L01_DPL_P3) | (0 << NRF24L01_DPL_P4) | (0 << NRF24L01_DPL_P5)); /* Clear FIFOs */ NRF24L01_FLUSH_TX; NRF24L01_FLUSH_RX; /* Clear interrupts */ NRF24L01_CLEAR_INTERRUPTS; /* Go to RX mode */ TM_NRF24L01_PowerUpRx(); /* Return OK */ return 1; }
static void TM_LCD_INT_InitLCD(void) { #if defined(LCD_USE_STM32F429_DISCOVERY) /* CS high */ ILI9341_CS_SET; /* Init SPI */ TM_SPI_Init(ILI9341_SPI, ILI9341_SPI_PINS); /* Initialization sequence */ TM_ILI9341_SendCommand(0xCA); TM_ILI9341_SendData(0xC3); TM_ILI9341_SendData(0x08); TM_ILI9341_SendData(0x50); TM_ILI9341_SendCommand(ILI9341_POWERB); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0xC1); TM_ILI9341_SendData(0x30); TM_ILI9341_SendCommand(ILI9341_POWER_SEQ); TM_ILI9341_SendData(0x64); TM_ILI9341_SendData(0x03); TM_ILI9341_SendData(0x12); TM_ILI9341_SendData(0x81); TM_ILI9341_SendCommand(ILI9341_DTCA); TM_ILI9341_SendData(0x85); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x78); TM_ILI9341_SendCommand(ILI9341_POWERA); TM_ILI9341_SendData(0x39); TM_ILI9341_SendData(0x2C); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x34); TM_ILI9341_SendData(0x02); TM_ILI9341_SendCommand(ILI9341_PRC); TM_ILI9341_SendData(0x20); TM_ILI9341_SendCommand(ILI9341_DTCB); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x00); TM_ILI9341_SendCommand(ILI9341_FRC); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x1B); TM_ILI9341_SendCommand(ILI9341_DFC); TM_ILI9341_SendData(0x0A); TM_ILI9341_SendData(0xA2); TM_ILI9341_SendCommand(ILI9341_POWER1); TM_ILI9341_SendData(0x10); TM_ILI9341_SendCommand(ILI9341_POWER2); TM_ILI9341_SendData(0x10); TM_ILI9341_SendCommand(ILI9341_VCOM1); TM_ILI9341_SendData(0x45); TM_ILI9341_SendData(0x15); TM_ILI9341_SendCommand(ILI9341_VCOM2); TM_ILI9341_SendData(0x90); TM_ILI9341_SendCommand(ILI9341_MAC); TM_ILI9341_SendData(0xC8); TM_ILI9341_SendCommand(ILI9341_3GAMMA_EN); TM_ILI9341_SendData(0x00); TM_ILI9341_SendCommand(ILI9341_RGB_INTERFACE); TM_ILI9341_SendData(0xC2); TM_ILI9341_SendCommand(ILI9341_DFC); TM_ILI9341_SendData(0x0A); TM_ILI9341_SendData(0xA7); TM_ILI9341_SendData(0x27); TM_ILI9341_SendData(0x04); TM_ILI9341_SendCommand(ILI9341_COLUMN_ADDR); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0xEF); TM_ILI9341_SendCommand(ILI9341_PAGE_ADDR); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x01); TM_ILI9341_SendData(0x3F); TM_ILI9341_SendCommand(ILI9341_INTERFACE); TM_ILI9341_SendData(0x01); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x06); TM_ILI9341_SendCommand(ILI9341_GRAM); TM_ILI9341_Delay(1000000); TM_ILI9341_SendCommand(ILI9341_GAMMA); TM_ILI9341_SendData(0x01); TM_ILI9341_SendCommand(ILI9341_PGAMMA); TM_ILI9341_SendData(0x0F); TM_ILI9341_SendData(0x29); TM_ILI9341_SendData(0x24); TM_ILI9341_SendData(0x0C); TM_ILI9341_SendData(0x0E); TM_ILI9341_SendData(0x09); TM_ILI9341_SendData(0x4E); TM_ILI9341_SendData(0x78); TM_ILI9341_SendData(0x3C); TM_ILI9341_SendData(0x09); TM_ILI9341_SendData(0x13); TM_ILI9341_SendData(0x05); TM_ILI9341_SendData(0x17); TM_ILI9341_SendData(0x11); TM_ILI9341_SendData(0x00); TM_ILI9341_SendCommand(ILI9341_NGAMMA); TM_ILI9341_SendData(0x00); TM_ILI9341_SendData(0x16); TM_ILI9341_SendData(0x1B); TM_ILI9341_SendData(0x04); TM_ILI9341_SendData(0x11); TM_ILI9341_SendData(0x07); TM_ILI9341_SendData(0x31); TM_ILI9341_SendData(0x33); TM_ILI9341_SendData(0x42); TM_ILI9341_SendData(0x05); TM_ILI9341_SendData(0x0C); TM_ILI9341_SendData(0x0A); TM_ILI9341_SendData(0x28); TM_ILI9341_SendData(0x2F); TM_ILI9341_SendData(0x0F); TM_ILI9341_SendCommand(ILI9341_SLEEP_OUT); TM_ILI9341_Delay(1000000); TM_ILI9341_SendCommand(ILI9341_DISPLAY_ON); TM_ILI9341_SendCommand(ILI9341_GRAM); #endif }
void init_sd_spi(void){ TM_SPI_Init(SD_CARD_SPI, SD_CARD_SPI_PINS); TM_GPIO_Init(SD_CARD_CS_PORT, SD_CARD_CS_PIN, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Medium); GPIO_SetBits(SD_CARD_CS_PORT, SD_CARD_CS_PIN); //CS to high Delayms(10); }
int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Init SPI */ TM_SPI_Init(SPI1, TM_SPI_PinsPack_1); /* Init SPI DMA */ TM_SPI_DMA_Init(SPI1); /* Set fake SPI TX buffer */ memset(TX_Buffer, 0x00, sizeof(TX_Buffer)); val = TM_GENERAL_DWTCounterEnable(); /* Set CS low first here before you send data over SPI */ TM_GENERAL_DWTCounterSetValue(0); /* Send data over SPI1 with DMA */ /* Exchange data with SPI slave using SPI DMA */ /* Exchange 5 bytes of data */ TM_SPI_DMA_Transmit(SPI1, TX_Buffer, RX_Buffer, sizeof(TX_Buffer)); /* Wait till SPI DMA do it's job */ /* You can do other stuff instead */ while (TM_SPI_DMA_Working(SPI1)); val = TM_GENERAL_DWTCounterGetValue(); /* Little delay, for debug */ Delayms(10); TM_GENERAL_DWTCounterSetValue(0); TM_SPI_SendMulti(SPI1, TX_Buffer, RX_Buffer, sizeof(TX_Buffer)); val = TM_GENERAL_DWTCounterGetValue(); /* Send 5 bytes of data and don't care what you receive back from slave via DMA */ /* RX_Buffer should not be changed in debug window */ TM_SPI_DMA_Transmit(SPI1, TX_Buffer, NULL, 5); /* Wait till SPI DMA do it's job */ /* You can do other stuff instead */ while (TM_SPI_DMA_Working(SPI1)); /* Little delay, for debug */ Delayms(10); /* Receive 5 bytes of data, sent dummy 0x00 bytes to slave via DMA */ TM_SPI_DMA_Transmit(SPI1, NULL, RX_Buffer, 5); /* Wait till SPI DMA do it's job */ /* You can do other stuff instead */ while (TM_SPI_DMA_Working(SPI1)); /* Set CS high here after last byte is done by SPI */ while (1) { } }