bsp.c
524 lines
| 14.1 KiB
| text/x-c
|
CLexer
r60 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the libuc, microcontroler library | ||||
-- Copyright (C) 2011, Alexis Jeandet | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 3 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
------------------------------------------------------------------------------- | ||||
-- Author : Alexis Jeandet | ||||
Jeandet Alexis
|
r64 | -- Mail : alexis.jeandet@member.fsf.org | ||
r60 | -------------------------------------------------------------------------------*/ | |||
#include "bsp.h" | ||||
#include <streamdevices.h> | ||||
#include <malloc.h> | ||||
#include <gpio.h> | ||||
#include <uart.h> | ||||
#include <stdio.h> | ||||
#include <stm32f4xx_gpio.h> | ||||
#include <stm32f4xx_fsmc.h> | ||||
#include <i2c.h> | ||||
#include <core.h> | ||||
#include <terminal.h> | ||||
uint32_t OSC0 =8000000; | ||||
uint32_t INTOSC =16000000; | ||||
uint32_t RTCOSC =32768; | ||||
uint32_t currentCpuFreq=0; | ||||
extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; | ||||
LCD_IF_t lcdIF0={ | ||||
.init = &bsp_FSMC_init, | ||||
.writereg = &bsp_lcd0_write_reg, | ||||
.readreg = &bsp_lcd0_read_reg, | ||||
.writeGRAM = &bsp_lcd0_writeGRAM, | ||||
.readGRAM = &bsp_lcd0_readGRAM | ||||
}; | ||||
LCD_t lcd0={ | ||||
.interface = &lcdIF0, | ||||
.init = &ili9328init, | ||||
.paint = &ili9328paint, | ||||
.paintText = &ili9328paintText, | ||||
.paintFilRect = &ili9328paintFilRect, | ||||
.refreshenable = &ili9328refreshenable, | ||||
.width= 240, | ||||
.height = 320 | ||||
}; | ||||
terminal_t terminal0; | ||||
r63 | volatile uint8_t* lcd0_CMD=(volatile uint8_t*) 0x60000000; | |||
volatile uint8_t* lcd0_DATA=(volatile uint8_t*)(0x61FFFFF0); | ||||
r60 | ||||
float VREF0 =(float)3.3; | ||||
volatile vs10XXDev audioCodec0; | ||||
r62 | sdcardDev sdcard2; | |||
blkdevice sdcard2blkdev; | ||||
dikpartition sdcard2Part1; | ||||
FAT32fs sdcard2FAT32part1; | ||||
dikpartition sdcard2Part2; | ||||
FAT32fs sdcard2FAT32part2; | ||||
dikpartition sdcard2Part3; | ||||
FAT32fs sdcard2FAT32part3; | ||||
dikpartition sdcard2Part4; | ||||
FAT32fs sdcard2FAT32part4; | ||||
r60 | int bsp_init() | |||
{ | ||||
int i=0; | ||||
for(i=0;i<__MAX_OPENED_FILES__;i++) | ||||
{ | ||||
__opnfiles__[i] = NULL; | ||||
} | ||||
bsp_GPIO_init(); | ||||
bsp_uart_init(); | ||||
bsp_iic_init(); | ||||
bsp_FSMC_init(); | ||||
bsp_GTerm_init(); | ||||
bsp_spi_init(); | ||||
bsp_SD_init(); | ||||
bsp_Audio_init(); | ||||
printf("\r=====================\n\r"); | ||||
printf( "=====================\n\r"); | ||||
printf(BSP); | ||||
printf(" initialised\n\r"); | ||||
printf( "=====================\n\r"); | ||||
return 1; | ||||
} | ||||
void bsp_GPIO_init() | ||||
{ | ||||
gpio_t GPIO_Out_init_List[]={LED1,LED2,LCD_RESET,LCD_BACKL,VS1053xCS,VS1053xDCS,VS1053xRESET,SDCARD2CS}; | ||||
for(int i=0;i<8;i++) | ||||
{ | ||||
gpio_t GPIO_init = gpioopen(GPIO_Out_init_List[i]); | ||||
GPIO_init |= gpiohighspeed | gpiooutdir | gpiopushpulltype; | ||||
gpiosetconfig(&GPIO_init); | ||||
} | ||||
r62 | gpio_t GPIO_In_init_List[]={VS1053DREQ,SDCARD2CD,BP3}; | |||
for(int i=0;i<3;i++) | ||||
r60 | { | |||
gpio_t GPIO_init = gpioopen(GPIO_In_init_List[i]); | ||||
GPIO_init |= gpiohighspeed | gpioindir; | ||||
gpiosetconfig(&GPIO_init); | ||||
} | ||||
gpioclr(VS1053xRESET); | ||||
gpioset(VS1053xCS); | ||||
gpioset(VS1053xDCS); | ||||
gpioset(SDCARD2CS); | ||||
gpioclr(LCD_RESET); | ||||
gpioclr(LCD_BACKL); | ||||
} | ||||
void bsp_uart_init() | ||||
{ | ||||
r63 | // if(__opnfiles__[1]==NULL) | |||
// { | ||||
// //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); | ||||
// streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); | ||||
// uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PD8,PD9,-1,-1); | ||||
// uartmkstreamdev(uart,fd1); | ||||
// __opnfiles__[1] = fd1; | ||||
// } | ||||
// else | ||||
// { | ||||
// uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PD8,PD9,-1,-1); | ||||
// } | ||||
r60 | } | |||
/* | ||||
D0 PD14 D1 PD15 D2 PD0 D3 PD1 D4 PE7 | ||||
D5 PE8 D6 PE9 D7 PE10 | ||||
A20 PE4 = RS FSMC_NE1 PD7 CS FSMC_NWE PD5 W/S | ||||
FSMC_NOE PD4 RD | ||||
*/ | ||||
int bsp_FSMC_init() | ||||
{ | ||||
#define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) | ||||
#define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) | ||||
gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10\ | ||||
,PD4,PD5,PD7,PE4}; | ||||
for(int i=0;i<12;i++) | ||||
{ | ||||
gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); | ||||
LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | ||||
gpiosetconfig(&LCD_DBx); | ||||
GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); | ||||
} | ||||
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; | ||||
FSMC_NORSRAMTimingInitTypeDef p; | ||||
/* Enable FSMC clock */ | ||||
RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); | ||||
/*-- FSMC Configuration ------------------------------------------------------*/ | ||||
/*----------------------- SRAM Bank 3 ----------------------------------------*/ | ||||
/* FSMC_Bank1_NORSRAM4 configuration */ | ||||
//p.FSMC_AddressSetupTime = 5; | ||||
//p.FSMC_AddressSetupTime = 1; | ||||
p.FSMC_AddressSetupTime = 3; | ||||
p.FSMC_AddressHoldTime = 3; | ||||
//p.FSMC_DataSetupTime = 9; | ||||
//ili9328 -> data setup time > 10ns | ||||
p.FSMC_DataSetupTime = 1; | ||||
if(getCpuFreq()>100*1000*1000) | ||||
p.FSMC_DataSetupTime = 2;// 11; | ||||
p.FSMC_BusTurnAroundDuration = 0; | ||||
p.FSMC_CLKDivision = 0; | ||||
p.FSMC_DataLatency = 0; | ||||
//ili9328 -> data hold time > 15ns | ||||
if(getCpuFreq()>66*1000*1000) | ||||
r63 | p.FSMC_DataLatency = 1; | |||
r60 | p.FSMC_AccessMode = FSMC_AccessMode_A; | |||
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; | ||||
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; | ||||
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; | ||||
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; | ||||
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; | ||||
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; | ||||
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; | ||||
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; | ||||
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; | ||||
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); | ||||
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); | ||||
gpioset(LCD_RESET); | ||||
gpioclr(LCD_RESET); | ||||
delay_100us(500); | ||||
gpioset(LCD_RESET); | ||||
delay_100us(500); | ||||
lcd0.init(&lcd0); | ||||
gpioset(LCD_BACKL); | ||||
return 1; | ||||
} | ||||
void bsp_spi_init() | ||||
{ | ||||
r62 | gpio_t VSSPI_DBxList[]={VS1053SCK,VS1053MOSI,VS1053MISO}; | |||
for(int i=0;i<3;i++) | ||||
{ | ||||
gpio_t SPI_DBx = gpioopen(VSSPI_DBxList[i]); | ||||
SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | ||||
gpiosetconfig(&SPI_DBx); | ||||
GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI1); | ||||
} | ||||
spiopenandconfig(VS1053SPI,spi8bits|spimaster|spimsbfirst,2*1000*1000,VS1053MOSI,VS1053MISO,VS1053SCK,-1); | ||||
r60 | ||||
gpio_t SDSPI_DBxList[]={SDCARD2SCK,SDCARD2MOSI,SDCARD2MISO}; | ||||
for(int i=0;i<3;i++) | ||||
{ | ||||
gpio_t SPI_DBx = gpioopen(SDSPI_DBxList[i]); | ||||
SPI_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | ||||
gpiosetconfig(&SPI_DBx); | ||||
GPIO_PinAFConfig(GPIOGETPORT(SPI_DBx), (uint8_t)(SPI_DBx & 0xF), GPIO_AF_SPI3); | ||||
} | ||||
spiopenandconfig(SDCARD2SPI,spi8bits|spimaster|spimsbfirst,400*1000,SDCARD2MOSI,SDCARD2MISO,SDCARD2SCK,-1); | ||||
} | ||||
void bsp_iic_init() | ||||
{ | ||||
// i2copenandconfig(i2c2,0,10000,PF0,PF1); | ||||
} | ||||
void bsp_Audio_init() | ||||
{ | ||||
vs10XXopen(&audioCodec0,VS1053SPI,vs1052setXCS,vs1052setRST,vs1052setXDCS,vs10XXDREQ); | ||||
r62 | if(audioCodec0.VERSION!=UNKNOWN) | |||
{ | ||||
printf("detected Audio codec "); | ||||
switch (audioCodec0.VERSION) { | ||||
case VS1001: | ||||
printf("VS1001\n"); | ||||
break; | ||||
case VS1011: | ||||
printf("VS1011\n"); | ||||
break; | ||||
case VS1002: | ||||
printf("VS1002\n"); | ||||
break; | ||||
case VS1003: | ||||
printf("VS1003\n"); | ||||
break; | ||||
case VS1053: | ||||
printf("VS1053\n"); | ||||
break; | ||||
case VS1033: | ||||
printf("VS1033\n"); | ||||
break; | ||||
case VS1103: | ||||
printf("VS1103\n"); | ||||
break; | ||||
default: | ||||
printf("Unknown device\n"); | ||||
break; | ||||
} | ||||
} | ||||
r60 | } | |||
void bsp_SD_init() | ||||
{ | ||||
r62 | if(bspsdcardpresent()) | |||
{ | ||||
sdcardspimake(&sdcard2,(UHANDLE)SDCARD2SPI,spigetnc,spiputnc,spisetspeed,spigetspeed); | ||||
sdcardspimakeblkdev(&sdcard2blkdev,&sdcard2,bspsdcardselect,bsppowersdcard,bspsdcardpresent,bspsdcardwriteprotected); | ||||
if(sdcard2blkdev.initialize(&sdcard2blkdev)!=STA_NOINIT) | ||||
{ | ||||
if(mbropen(&sdcard2blkdev,&sdcard2Part1,1)==MBRnoErr) | ||||
{ | ||||
if(FATnoErr!=fat32open(&sdcard2FAT32part1,&sdcard2Part1)) | ||||
printf("Can't open fat32 partition 1\n"); | ||||
} | ||||
else | ||||
{ | ||||
printf("Can't open or read MBR\n"); | ||||
} | ||||
Jeandet Alexis
|
r64 | delay_100us(10000); | ||
r62 | if(mbropen(&sdcard2blkdev,&sdcard2Part2,2)==MBRnoErr) | |||
{ | ||||
if(FATnoErr!=fat32open(&sdcard2FAT32part2,&sdcard2Part2)) | ||||
printf("Can't open fat32 partition 2\n"); | ||||
} | ||||
else | ||||
{ | ||||
printf("Can't open or read MBR\n"); | ||||
} | ||||
Jeandet Alexis
|
r64 | delay_100us(10000); | ||
r62 | if(mbropen(&sdcard2blkdev,&sdcard2Part3,3)==MBRnoErr) | |||
{ | ||||
if(FATnoErr!=fat32open(&sdcard2FAT32part3,&sdcard2Part3)) | ||||
printf("Can't open fat32 partition 3\n"); | ||||
} | ||||
else | ||||
{ | ||||
printf("Can't open or read MBR\n"); | ||||
} | ||||
Jeandet Alexis
|
r64 | delay_100us(10000); | ||
r62 | if(mbropen(&sdcard2blkdev,&sdcard2Part4,4)==MBRnoErr) | |||
{ | ||||
if(FATnoErr!=fat32open(&sdcard2FAT32part4,&sdcard2Part4)) | ||||
printf("Can't open fat32 partition 4\n"); | ||||
} | ||||
else | ||||
{ | ||||
printf("Can't open or read MBR\n"); | ||||
} | ||||
} | ||||
else | ||||
{ | ||||
printf("Can't initialize SDCARD\n"); | ||||
} | ||||
} | ||||
Jeandet Alexis
|
r64 | delay_100us(20000); | ||
r60 | } | |||
void vs1052setXCS(char val) | ||||
{ | ||||
gpiosetval(VS1053xCS,(int)val); | ||||
} | ||||
void vs1052setXDCS(char val) | ||||
{ | ||||
r62 | //gpiosetval(LED1,(int)val); | |||
r60 | gpiosetval(VS1053xDCS,(int)val); | |||
} | ||||
void vs1052setRST(char val) | ||||
{ | ||||
if(val) | ||||
gpioset(VS1053xRESET); | ||||
else | ||||
gpioclr(VS1053xRESET); | ||||
} | ||||
int vs10XXDREQ() | ||||
{ | ||||
return gpiogetval(VS1053DREQ); | ||||
} | ||||
void bsppowersdcard(char onoff) //always ON | ||||
{ | ||||
} | ||||
char bspsdcardpresent() | ||||
{ | ||||
return gpiogetval(SDCARD2CD); | ||||
} | ||||
char bspsdcardwriteprotected() | ||||
{ | ||||
return 0; | ||||
} | ||||
void bspsdcardselect(char YESNO) | ||||
{ | ||||
r62 | // gpiosetval(LED1,(int)YESNO); | |||
r60 | if(YESNO) | |||
gpioclr(SDCARD2CS); | ||||
else | ||||
gpioset(SDCARD2CS); | ||||
} | ||||
void bsp_lcd0_write_reg(uint32_t reg,uint32_t data) | ||||
{ | ||||
uint8_t* pt8 = (uint8_t*)(void*)® | ||||
#if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | ||||
*lcd0_CMD=pt8[3]; | ||||
*lcd0_CMD=pt8[2]; | ||||
pt8 = (uint8_t*)(void*)&data; | ||||
*lcd0_DATA=pt8[3]; | ||||
*lcd0_DATA=pt8[2]; | ||||
#else | ||||
*lcd0_CMD=pt8[1]; | ||||
*lcd0_CMD=pt8[0]; | ||||
pt8 = (uint8_t*)(void*)&data; | ||||
*lcd0_DATA=pt8[1]; | ||||
*lcd0_DATA=pt8[0]; | ||||
#endif | ||||
} | ||||
uint32_t bsp_lcd0_read_reg(uint32_t reg) | ||||
{ | ||||
uint8_t* pt8 = (uint8_t*)(void*)® | ||||
uint32_t DATA=0; | ||||
#if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | ||||
*lcd0_CMD=pt8[3]; | ||||
*lcd0_CMD=pt8[2]; | ||||
pt8 = (uint8_t*)(void*)&DATA; | ||||
pt8[3]=*lcd0_DATA; | ||||
pt8[2]=*lcd0_DATA; | ||||
#else | ||||
*lcd0_CMD=pt8[1]; | ||||
*lcd0_CMD=pt8[0]; | ||||
pt8 = (uint8_t*)(void*)&DATA; | ||||
pt8[1]=*lcd0_DATA; | ||||
pt8[0]=*lcd0_DATA; | ||||
#endif | ||||
return DATA; | ||||
} | ||||
void bsp_lcd0_writeGRAM(void* buffer,uint32_t count) | ||||
{ | ||||
uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; | ||||
uint8_t* pt8 = (uint8_t*)(void*)® | ||||
#if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | ||||
*lcd0_CMD=pt8[3]; | ||||
*lcd0_CMD=pt8[2]; | ||||
pt8 = (uint8_t*)(void*)buffer; | ||||
for(int i=0;i<(int)count;i++) | ||||
{ | ||||
*lcd0_DATA=pt8[(2*i) +1]; | ||||
*lcd0_DATA=pt8[2*i]; | ||||
} | ||||
#else | ||||
*lcd0_CMD=pt8[1]; | ||||
*lcd0_CMD=pt8[0]; | ||||
pt8 = (uint8_t*)(void*)buffer; | ||||
for(int i=0;i<(int)count;i++) | ||||
{ | ||||
*lcd0_DATA=pt8[(2*i) +1]; | ||||
*lcd0_DATA=pt8[2*i]; | ||||
} | ||||
#endif | ||||
} | ||||
void bsp_lcd0_readGRAM(void* buffer,uint32_t count) | ||||
{ | ||||
r63 | // uint32_t reg =ILI9328_REGISTER_WRITEDATATOGRAM; | |||
// int8_t* pt8 = (int8_t*)(void*)® | ||||
r60 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ | |||
*lcd0_CMD=pt8[3]; | ||||
*lcd0_CMD=pt8[2]; | ||||
pt8 = (uint8_t*)(void*)buffer; | ||||
for(int i=0;i<(int)count;i++) | ||||
{ | ||||
pt8[(2*i) +1]=*lcd0_DATA; | ||||
r63 | __asm__("nop"); | |||
__asm__("nop"); | ||||
__asm__("nop"); | ||||
__asm__("nop"); | ||||
__asm__("nop"); | ||||
r60 | pt8[2*i]=*lcd0_DATA; | |||
} | ||||
#else | ||||
r63 | // lcd0_CMD=pt8[1]; | |||
// lcd0_CMD=pt8[0]; | ||||
// pt8 = (int8_t*)(void*)buffer; | ||||
// for(int i=0;i<(int)count;i++) | ||||
// { | ||||
// pt8[(2*i) +1]=*lcd0_DATA; | ||||
// __asm__("nop"); | ||||
// __asm__("nop"); | ||||
// __asm__("nop"); | ||||
// __asm__("nop"); | ||||
// __asm__("nop"); | ||||
// pt8[2*i]=*lcd0_DATA; | ||||
//pt8[(2*i) +1]=(uint8_t)0; | ||||
//pt8[(2*i)]=(uint8_t)0; | ||||
// } | ||||
r60 | #endif | |||
} | ||||
void bsp_GTerm_init() | ||||
{ | ||||
if(__opnfiles__[1]==NULL) | ||||
{ | ||||
streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); | ||||
Jeandet Alexis
|
r64 | terminal_init(&terminal0 ,&lcd0,&ComicSansMS_8,fd1); | ||
r60 | __opnfiles__[1] = fd1; | |||
} | ||||
else | ||||
{ | ||||
} | ||||
} | ||||