diff --git a/bsp/includes/BEAGLESYNTH/bsp.h b/bsp/includes/BEAGLESYNTH/bsp.h --- a/bsp/includes/BEAGLESYNTH/bsp.h +++ b/bsp/includes/BEAGLESYNTH/bsp.h @@ -57,6 +57,7 @@ extern void bsp_uart_init(); extern void bsp_iic_init(); extern void bsp_spi_init(); extern void bsp_SD_init(); +extern void bsp_FSMC_init(); /* VS1053 */ extern void clearXCS(); diff --git a/bsp/src/BEAGLESYNTH/bsp.c b/bsp/src/BEAGLESYNTH/bsp.c --- a/bsp/src/BEAGLESYNTH/bsp.c +++ b/bsp/src/BEAGLESYNTH/bsp.c @@ -25,6 +25,7 @@ #include #include #include +#include uint32_t OSC0 =8000000; uint32_t INTOSC =16000000; uint32_t RTCOSC =32768; @@ -81,15 +82,32 @@ void bsp_uart_init() { if(__opnfiles__[1]==NULL) { - uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); + //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); - uartopenandconfig(0,uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); - uartmkstreamdev(uart1,fd1); + uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); + uartmkstreamdev(uart,fd1); __opnfiles__[1] = fd1; } else { - uartopenandconfig(0,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); + uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); + } +} + + +void 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,PE11,PE12,PE13,PE14,PE15\ + ,PD8,PD9,PD10,PD4,PD5,PD7,PE4}; + for(int i=0;i<20;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); } } diff --git a/bsp/src/M4Stick/bsp.c b/bsp/src/M4Stick/bsp.c --- a/bsp/src/M4Stick/bsp.c +++ b/bsp/src/M4Stick/bsp.c @@ -70,15 +70,15 @@ void bsp_uart_init() { if(__opnfiles__[1]==NULL) { - uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); + //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); - uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); - uartmkstreamdev(uart1,fd1); + uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); + uartmkstreamdev(uart,fd1); __opnfiles__[1] = fd1; } else { - uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); + uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); } } diff --git a/bsp/src/SOLAR_LFR_PSU/bsp.c b/bsp/src/SOLAR_LFR_PSU/bsp.c --- a/bsp/src/SOLAR_LFR_PSU/bsp.c +++ b/bsp/src/SOLAR_LFR_PSU/bsp.c @@ -59,29 +59,33 @@ void bsp_GPIO_init() gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2); gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2); gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2); + gpio_t dacRst=gpioopen(PD4); gpiosetspeed(&gpio1,gpiohighspeed); gpiosetspeed(&gpio2,gpiohighspeed); gpiosetspeed(&gpio3,gpiohighspeed); gpiosetspeed(&gpio4,gpiohighspeed); + gpiosetspeed(&dacRst,gpiohighspeed); gpiosetdir(&gpio1,gpiooutdir); gpiosetdir(&gpio3,gpiooutdir); gpiosetdir(&gpio2,gpiooutdir); gpiosetdir(&gpio4,gpiooutdir); + gpiosetdir(&dacRst,gpiooutdir); + gpioset(dacRst); } void bsp_uart_init() { if(__opnfiles__[1]==NULL) { - uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); + //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); - uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); - uartmkstreamdev(uart1,fd1); + uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); + uartmkstreamdev(uart,fd1); __opnfiles__[1] = fd1; } else { - uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); + uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); } } @@ -95,6 +99,8 @@ void bsp_iic_init() { if(0==i2copenandconfig(i2c1,0,400000,PB9,PB6))printf("I2C1 opened\n\r"); i2cenable(i2c1); + if(0==i2copenandconfig(i2c3,0,400000,PC9,PA8))printf("I2C3 opened\n\r"); + i2cenable(i2c3); } void bsp_SD_init() diff --git a/bsp/src/STM32F4Discovery/bsp.c b/bsp/src/STM32F4Discovery/bsp.c --- a/bsp/src/STM32F4Discovery/bsp.c +++ b/bsp/src/STM32F4Discovery/bsp.c @@ -70,16 +70,16 @@ void bsp_uart_init() { if(__opnfiles__[1]==NULL) { - uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); + //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); - uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,19200,PB10,PB11,-1,-1); - uartmkstreamdev(uart1,fd1); + uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,19200,PB10,PB11,-1,-1); + uartmkstreamdev(uart,fd1); __opnfiles__[1] = fd1; //stdo __opnfiles__[0] = fd1; //stdi } else { - uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); + uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); } } diff --git a/examples/BeagleSynthHello/main.c b/examples/BeagleSynthHello/main.c --- a/examples/BeagleSynthHello/main.c +++ b/examples/BeagleSynthHello/main.c @@ -11,16 +11,6 @@ extern streamdevice* __opnfiles__[]; int main() { - //gpioset(PSU_DISABLE); - gpio_t dacRst=gpioopen(PD4); - gpiosetspeed(&dacRst,gpiohighspeed); - gpiosetdir(&dacRst,gpiooutdir); - gpioset(dacRst); - printf("i2C Control register 1 (I2C_CR1) = %x\n\r",I2C1->CR1); - printf("i2C Control register 2 (I2C_CR2) = %x\n\r",I2C1->CR2); - printf("i2C Status register 1 (I2C_SR1) = %x\n\r",I2C1->SR1); - printf("i2C Status register 2 (I2C_SR2) = %x\n\r",I2C1->SR2); - printf("i2C Clock control register (I2C_CCR) = %x\n\r",I2C1->CCR); while(1) { for(volatile int i=0;i<1024*2048;i++); diff --git a/examples/SDCARD/main.c b/examples/SDCARD/main.c --- a/examples/SDCARD/main.c +++ b/examples/SDCARD/main.c @@ -18,11 +18,10 @@ int main() //sdcardmakeblkdev(&dev); // mbropen(&dev,&part,1); - spi_t spi1; - spiopenandconfig(1,&spi1,spi8bits | spimaster |spiclkinhlow | spiclkfirstedge | spimsbfirst,10000,PB15,PB14,PB13,-1); + spi_t spi = spiopenandconfig(spi2,spi8bits | spimaster |spiclkinhlow | spiclkfirstedge | spimsbfirst,10000,PB15,PB14,PB13,-1); while(1) { - spiputw(&spi1,0x15); + spiputw(spi,0x15); for(volatile int i=0;i<1024*1024;i++); } printf("Hello world from project 'SDCARD'! %f\n\r",2.986*4.565); diff --git a/lib/includes/spi.h b/lib/includes/spi.h --- a/lib/includes/spi.h +++ b/lib/includes/spi.h @@ -34,13 +34,25 @@ extern "C" { #endif - +/* typedef struct spi_t { void* _dev; int cfg; int speed; -}spi_t; +}spi_t;*/ + +typedef int spi_t; + + #define spi1 0 + #define spi2 1 + #define spi3 2 + #define spi4 3 + #define spi5 4 + #define spi6 5 + #define spi7 6 + #define spi8 7 + #define spi9 8 typedef enum @@ -96,27 +108,27 @@ typedef enum #define SPIBITORDERMASK 0x80 -extern int spiopen(int count,spi_t* spidev); -extern int spiopenandconfig(int count,spi_t* spidev,uint32_t cfg,uint32_t speed,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); -extern int spiclose(spi_t* spidev); -extern int spisetpins(spi_t* spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); -extern int spienable(spi_t* spidev); -extern int spidisable(spi_t* spidev); -extern int spisetconfig(spi_t* spidev); -extern int spisetspeed(spi_t* spidev, uint32_t speed); -extern int spisetbitorder(spi_t* spidev,spibitorder_t order); -extern int spisetdatabits(spi_t* spidev,spibits_t bitscnt); -extern int spisetclkinhlevel(spi_t* spidev,spiclkinhlvl_t level); -extern int spisetclkphase(spi_t* spidev,spiclkphase_t phase); -extern int spiputw(spi_t* spidev,uint16_t data); -extern uint16_t spigetw(spi_t* spidev); -extern int spiputs(spi_t* spidev,char* s); -extern int spigets(spi_t* spidev,char* s); -extern int spiputnw(spi_t* spidev,uint16_t* w,int n); -extern int spigetnw(spi_t* spidev,uint16_t* w,int n); -extern int spiputnc(spi_t* spidev,char* c,int n); -extern int spigetnc(spi_t* spidev,char* c,int n); -extern int spiavailiabledata(spi_t* spidev); +extern spi_t spiopen(int count); +extern spi_t spiopenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); +extern int spiclose(spi_t spidev); +extern int spisetpins(spi_t spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin); +extern int spienable(spi_t spidev); +extern int spidisable(spi_t spidev); +extern int spisetconfig(spi_t spidev,uint32_t config,uint32_t speed); +extern int spisetspeed(spi_t spidev, uint32_t speed); +extern int spisetbitorder(spi_t spidev,spibitorder_t order); +extern int spisetdatabits(spi_t spidev,spibits_t bitscnt); +extern int spisetclkinhlevel(spi_t spidev,spiclkinhlvl_t level); +extern int spisetclkphase(spi_t spidev,spiclkphase_t phase); +extern int spiputw(spi_t spidev,uint16_t data); +extern uint16_t spigetw(spi_t spidev); +extern int spiputs(spi_t spidev,char* s); +extern int spigets(spi_t spidev,char* s); +extern int spiputnw(spi_t spidev,uint16_t* w,int n); +extern int spigetnw(spi_t spidev,uint16_t* w,int n); +extern int spiputnc(spi_t spidev,char* c,int n); +extern int spigetnc(spi_t spidev,char* c,int n); +extern int spiavailiabledata(spi_t spidev); #ifdef __cplusplus } diff --git a/lib/includes/uart.h b/lib/includes/uart.h --- a/lib/includes/uart.h +++ b/lib/includes/uart.h @@ -26,31 +26,44 @@ #include #ifdef __cplusplus - extern "C" { +extern "C" { #endif - +/* typedef volatile struct uart_t { volatile void* _dev; volatile int cfg; volatile int speed; }uart_t; + */ + +typedef int uart_t; +#define uart1 0 +#define uart2 1 +#define uart3 2 +#define uart4 3 +#define uart5 4 +#define uart6 5 +#define uart7 6 +#define uart8 7 +#define uart9 8 + typedef enum { - uartparitynone = 0x1, - uartparityeven = 0x2, - uartparityodd = 0x3 + uartparitynone = 0x1, + uartparityeven = 0x2, + uartparityodd = 0x3 }uartparity_t; #define UARTPARITYMASK 0x3 typedef enum { - uart7bits = 0x4, - uart8bits = 0x8, - uart9bits = 0xC + uart7bits = 0x4, + uart8bits = 0x8, + uart9bits = 0xC }uartbits_t; #define UARTBITSMASK 0xC @@ -58,37 +71,37 @@ typedef enum typedef enum { - uarthalfstop = 0x10, - uartonestop = 0x20, - uartonehalfstop = 0x30, - uarttwostop = 0x40 + uarthalfstop = 0x10, + uartonestop = 0x20, + uartonehalfstop = 0x30, + uarttwostop = 0x40 }uartstopbits_t; #define UARTSTOPBITSMASK 0x70 -extern int uartopen(int count ,uart_t* uart); -extern int uartopenandconfig(int count ,uart_t* uart,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); -extern int uartclose(uart_t* uart); -extern int uartsetpins(uart_t* uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); -extern int uartenable(uart_t* uart); -extern int uartdisable(uart_t* uart); -extern int uartsetconfig(uart_t* uart); -extern int uartsetspeed(uart_t* uart,int speed); -extern int uartsetparity(uart_t* uart,uartparity_t parity); -extern int uartsetdatabits(uart_t* uart,uartbits_t databits); -extern int uartsetstopbits(uart_t* uart,uartstopbits_t stopbits); -extern int uartputc(uart_t* uart,char c); -extern char uartgetc(uart_t* uart); -extern int uartputs(uart_t* uart,char* s); -extern int uartgets(uart_t* uart,char* s); -extern int uartputnc(uart_t* uart,char* c,int n); -extern int uartgetnc(uart_t* uart,char* c,int n); -extern int uartavailiabledata(uart_t* uart); +extern uart_t uartopen(int count); +extern uart_t uartopenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); +extern int uartclose(uart_t uart); +extern int uartsetpins(uart_t uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin); +extern int uartenable(uart_t uart); +extern int uartdisable(uart_t uart); +extern int uartsetconfig(uart_t uart,uint32_t cfg,uint32_t speed); +extern int uartsetspeed(uart_t uart,uint32_t speed); +extern int uartsetparity(uart_t uart,uartparity_t parity); +extern int uartsetdatabits(uart_t uart,uartbits_t databits); +extern int uartsetstopbits(uart_t uart,uartstopbits_t stopbits); +extern int uartputc(uart_t uart,char c); +extern char uartgetc(uart_t uart); +extern int uartputs(uart_t uart,char* s); +extern int uartgets(uart_t uart,char* s); +extern int uartputnc(uart_t uart,char* c,int n); +extern int uartgetnc(uart_t uart,char* c,int n); +extern int uartavailiabledata(uart_t uart); -extern int uartmkstreamdev(uart_t* uart,streamdevice* strdev); +extern int uartmkstreamdev(uart_t uart,streamdevice* strdev); #ifdef __cplusplus - } +} #endif #endif //SPI_H diff --git a/lib/src/common/common.pro b/lib/src/common/common.pro --- a/lib/src/common/common.pro +++ b/lib/src/common/common.pro @@ -1,6 +1,7 @@ TEMPLATE = dir SUBDIRS += HEXVIEWER \ - FILE_SYSTEM + FILE_SYSTEM \ + CS43L22 diff --git a/lib/src/lib.pro b/lib/src/lib.pro --- a/lib/src/lib.pro +++ b/lib/src/lib.pro @@ -3,7 +3,8 @@ CONFIG += ordered SUBDIRS += stm32f4 -SUBDIRS += common/FILE_SYSTEM +SUBDIRS += common/FILE_SYSTEM \ + common/AUDIO # common/STREAMDEVICES \ # common/STRINGS \ diff --git a/lib/src/stm32f4/SPI/spi.c b/lib/src/stm32f4/SPI/spi.c --- a/lib/src/stm32f4/SPI/spi.c +++ b/lib/src/stm32f4/SPI/spi.c @@ -27,340 +27,396 @@ #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) -int spiopen(int count,spi_t* spidev) + +SPI_TypeDef* _spi_dev_table[3]={SPI1,SPI2,SPI3}; + +spi_t spiopen(int count) { - - switch(count) - { - case 0: - spidev->_dev = (void*)SPI1; - RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); - RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); - RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); - break; - case 1: - spidev->_dev = (void*)SPI2; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); - break; - case 2: - spidev->_dev = (void*)SPI3; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE); - RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); - break; - default: - break; - } - spienable(spidev); - return 1; +#define _INIT_DEV(_RCC_) \ + RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \ + RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \ + RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \ + RCC_APB1PeriphClockCmd(_RCC_, ENABLE); + + switch(count) + { + case 0: + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); + RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); + return (spi_t) 0; + break; + case 1: + _INIT_DEV(RCC_APB1Periph_SPI2); + return (spi_t) 1; + break; + case 2: + _INIT_DEV(RCC_APB1Periph_SPI3); + return (spi_t) 2; + break; + default: + break; + } + return -1; } -int spiopenandconfig(int count,spi_t* spidev,uint32_t cfg,uint32_t speed,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin) + +spi_t spiopenandconfig(int count, uint32_t cfg, uint32_t speed, uint32_t MOSIpin, uint32_t MISOpin, uint32_t SCKpin, uint32_t SCSpin) { - spiopen(count ,spidev); - spidev->cfg = cfg; - spidev->speed = speed; - spidisable(spidev); - spisetpins(spidev,MOSIpin, MISOpin, SCKpin, SCSpin); - spienable(spidev); - spisetconfig(spidev); - return 1; + spi_t dev = spiopen(count); + if(dev!=-1) + { + spidisable(dev); + spisetpins(dev,MOSIpin, MISOpin, SCKpin, SCSpin); + spienable(dev); + spisetconfig(dev,cfg,speed); + } + return dev; } -int spiclose(spi_t* spidev) +int spiclose(spi_t spidev) { - switch((int)spidev->_dev) - { - case (int)(void*)SPI1: - spidev->_dev = (void*)SPI1; - RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); - break; - case (int)(void*)SPI2: - spidev->_dev = (void*)SPI2; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); - break; - case (int)(void*)SPI3: - spidev->_dev = (void*)SPI3; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); - break; - default: - break; - } - return 1; + if((spidev<3)&&(spidev>=0)) + { + switch(spidev) + { + case 0: + RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); + break; + case 1: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); + break; + case 2: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); + break; + default: + return -1; + break; + } + return 1; + } + return -1; } -int spisetpins(spi_t* spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin) +int spisetpins(spi_t spidev,uint32_t MOSIpin,uint32_t MISOpin,uint32_t SCKpin,uint32_t SCSpin) { - gpio_t MISO,MOSI,SCK,SCS; - uint8_t gpioAFspix = GPIO_AF_SPI1; - switch((int)spidev->_dev) - { - case (int)(void*)SPI1: - gpioAFspix = GPIO_AF_SPI1; - break; - case (int)(void*)SPI2: - gpioAFspix = GPIO_AF_SPI2; - break; - case (int)(void*)SPI3: - gpioAFspix = GPIO_AF_SPI3; - break; - default: - break; - } - if(MISOpin!=-1) - { - MISO = gpioopen(MISOpin); - MISO |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&MISO); - GPIO_PinAFConfig(GPIOGETPORT(MISO), (uint8_t)(MISO & 0xF), gpioAFspix); - } - if(MOSIpin!=-1) - { - MOSI = gpioopen(MOSIpin); - MOSI |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&MOSI); - GPIO_PinAFConfig(GPIOGETPORT(MOSI), (uint8_t)(MOSI & 0xF), gpioAFspix); - } - - if(SCKpin!=-1) - { - SCK = gpioopen(SCKpin); - SCK |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&SCK); - GPIO_PinAFConfig(GPIOGETPORT(SCK), (uint8_t)(SCK & 0xF), gpioAFspix); - } + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + gpio_t MISO,MOSI,SCK,SCS; + uint8_t gpioAFspix = GPIO_AF_SPI1; + switch(spidev) + { + case 0: + gpioAFspix = GPIO_AF_SPI1; + break; + case 1: + gpioAFspix = GPIO_AF_SPI2; + break; + case 2: + gpioAFspix = GPIO_AF_SPI3; + break; + default: + break; + } + if(MISOpin!=-1) + { + MISO = gpioopen(MISOpin); + MISO |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&MISO); + GPIO_PinAFConfig(GPIOGETPORT(MISO), (uint8_t)(MISO & 0xF), gpioAFspix); + } + if(MOSIpin!=-1) + { + MOSI = gpioopen(MOSIpin); + MOSI |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&MOSI); + GPIO_PinAFConfig(GPIOGETPORT(MOSI), (uint8_t)(MOSI & 0xF), gpioAFspix); + } - if(SCSpin!=-1) - { - SCS = gpioopen(SCSpin); - SCS |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&SCS); - GPIO_PinAFConfig(GPIOGETPORT(SCS), (uint8_t)(SCS & 0xF), gpioAFspix); - ((SPI_TypeDef *)(spidev->_dev))->CR2 |= (1<<2); - } - else - { - ((SPI_TypeDef *)(spidev->_dev))->CR2 &= ~(1<<2); - } - return 1; + if(SCKpin!=-1) + { + SCK = gpioopen(SCKpin); + SCK |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&SCK); + GPIO_PinAFConfig(GPIOGETPORT(SCK), (uint8_t)(SCK & 0xF), gpioAFspix); + } + + if(SCSpin!=-1) + { + SCS = gpioopen(SCSpin); + SCS |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&SCS); + GPIO_PinAFConfig(GPIOGETPORT(SCS), (uint8_t)(SCS & 0xF), gpioAFspix); + _dev_->CR2 |= (1<<2); + } + else + { + _dev_->CR2 &= ~(1<<2); + } + return 1; + } + return -1; } -int spienable(spi_t* spidev) +int spienable(spi_t spidev) { - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= (1<<6); - return 1; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + _dev_->CR1 |= (1<<6); + return 1; + } + return -1; } -int spidisable(spi_t* spidev) +int spidisable(spi_t spidev) { - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= ~(1<<6); - return 1; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + _dev_->CR1 &= ~(1<<6); + return 1; + } + return -1; } -int spisetconfig(spi_t* spidev) +int spisetconfig(spi_t spidev, uint32_t config, uint32_t speed) { - ((SPI_TypeDef *)(spidev->_dev))->CR2 |= (1<<2); - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= (1<<2); - - spisetspeed(spidev,spidev->speed); - spisetdatabits(spidev,spidev->cfg & SPIBITSMASK); - spisetbitorder(spidev,spidev->cfg & SPIBITORDERMASK); - spisetclkinhlevel(spidev,spidev->cfg & SPICLKINHLVLMASK); - spisetclkphase(spidev,spidev->cfg & SPICLKPHASEMASK); - - return 1; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + _dev_->CR2 |= (1<<2); + _dev_->CR1 |= (1<<2); + spisetspeed(spidev,speed); + spisetdatabits(spidev,config & SPIBITSMASK); + spisetbitorder(spidev,config & SPIBITORDERMASK); + spisetclkinhlevel(spidev,config & SPICLKINHLVLMASK); + spisetclkphase(spidev,config & SPICLKPHASEMASK); + return 0; + } + return 1; } -int spisetspeed(spi_t* spidev, uint32_t speed) +int spisetspeed(spi_t spidev, uint32_t speed) { - spidev->speed = speed; - uint32_t apbclock = 0x00; - /*uint32_t tmpreg = 0x00; - uint32_t integerdivider = 0x00; - uint32_t fractionaldivider = 0x00;*/ - RCC_ClocksTypeDef RCC_ClocksStatus; - RCC_GetClocksFreq(&RCC_ClocksStatus); - if ((SPI_TypeDef *)(spidev->_dev) == SPI1) - { - apbclock = RCC_ClocksStatus.PCLK2_Frequency; - } - else - { - apbclock = RCC_ClocksStatus.PCLK1_Frequency; - } - int32_t speederror = 0x7FFFFFFF; //max error - int32_t prev_speederror = 0x7FFFFFFF; - int32_t realspeed = 0; - unsigned char divider = 0; - do - { - divider ++; - prev_speederror = speederror; - realspeed = apbclock>>(divider); - speederror = realspeed - speed; - if(speederror<0)speederror=-speederror; - if(divider>8)break; - }while(speederrorspeed = apbclock>>(divider-1); - divider-=2; - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= 0xFFD7; // clear prescaler bits 3:5 - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= ((0x7 & divider)<<3); - //((SPI_TypeDef *)(spidev->_dev))->CR1 |= ((0x7 & 7)<<3); - return 1; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + uint32_t apbclock = 0x00; + RCC_ClocksTypeDef RCC_ClocksStatus; + RCC_GetClocksFreq(&RCC_ClocksStatus); + if (_dev_ == SPI1) + { + apbclock = RCC_ClocksStatus.PCLK2_Frequency; + } + else + { + apbclock = RCC_ClocksStatus.PCLK1_Frequency; + } + int32_t speederror = 0x7FFFFFFF; //max error + int32_t prev_speederror = 0x7FFFFFFF; + int32_t realspeed = 0; + unsigned char divider = 0; + do + { + divider ++; + prev_speederror = speederror; + realspeed = apbclock>>(divider); + speederror = realspeed - speed; + if(speederror<0)speederror=-speederror; + if(divider>8)break; + }while(speederror>(divider-1); + divider-=2; + _dev_->CR1 &= 0xFFD7; // clear prescaler bits 3:5 + _dev_->CR1 |= ((0x7 & divider)<<3); + return 1; + } + return -1; } -int spisetdatabits(spi_t* spidev,spibits_t bitscnt) +int spisetdatabits(spi_t spidev,spibits_t bitscnt) { - int result = 0; - switch(bitscnt) - { - case spi8bits: - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= ~(1<<11); - result = 1; - break; - case spi16bits: - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= (1<<11); - result = 1; - break; - default: - break; - } - return result; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + int result = 0; + switch(bitscnt) + { + case spi8bits: + _dev_->CR1 &= ~(1<<11); + result = 1; + break; + case spi16bits: + _dev_->CR1 |= (1<<11); + result = 1; + break; + default: + result =-1; + break; + } + return result; + } + return -1; } -int spisetbitorder(spi_t* spidev,spibitorder_t order) +int spisetbitorder(spi_t spidev,spibitorder_t order) { - if(order==spimsbfirst) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= ~(1<<7); - return 1; - } - else - { - if(order==spilsbfirst) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= (1<<7); - return 1; - } - else return 0; - } - + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + if(order==spimsbfirst) + { + _dev_->CR1 &= ~(1<<7); + return 1; + } + else + { + if(order==spilsbfirst) + { + _dev_->CR1 |= (1<<7); + return 1; + } + else return -1; + } + } + return -1; } -int spisetclkinhlevel(spi_t* spidev,spiclkinhlvl_t level) +int spisetclkinhlevel(spi_t spidev,spiclkinhlvl_t level) { - if(level==spiclkinhlow) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= ~(1<<1); - return 1; - } - else - { - if(level==spiclkinhhigh) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= (1<<1); - return 1; - } - else return 0; - } -} - -int spisetclkphase(spi_t* spidev,spiclkphase_t phase) -{ - if(phase==spiclkfirstedge) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 &= ~1; - return 1; - } - else - { - if(phase==spiclksecondedge) - { - ((SPI_TypeDef *)(spidev->_dev))->CR1 |= 1; - return 1; - } - else return 0; - } + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + if(level==spiclkinhlow) + { + _dev_->CR1 &= ~(1<<1); + return 1; + } + else + { + if(level==spiclkinhhigh) + { + _dev_->CR1 |= (1<<1); + return 1; + } + else return -1; + } + } + return -1; } -int spiputw(spi_t* spidev,uint16_t data) +int spisetclkphase(spi_t spidev,spiclkphase_t phase) { - ((SPI_TypeDef *)(spidev->_dev))->DR = data; - while((((SPI_TypeDef *)(spidev->_dev))->SR & (1<<7)) == (1<<7)); - return 1; -} -uint16_t spigetw(spi_t* spidev) -{ - while((((SPI_TypeDef *)(spidev->_dev))->SR & (1<<7)) == (1<<7)); - ((SPI_TypeDef *)(spidev->_dev))->DR = 0xFFFF; - while((((SPI_TypeDef *)(spidev->_dev))->SR & (1<<0)) == 0); - return ((SPI_TypeDef *)(spidev->_dev))->DR; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + if(phase==spiclkfirstedge) + { + _dev_->CR1 &= ~1; + return 1; + } + else + { + if(phase==spiclksecondedge) + { + _dev_->CR1 |= 1; + return 1; + } + else return -1; + } + } + return -1; } -int spiputs(spi_t* spidev,char* s) +int spiputw(spi_t spidev,uint16_t data) { - while (*s) spiputw(spidev,*s++); - return 1; + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + _dev_->DR = data; + while((_dev_->SR & (1<<7)) == (1<<7)); + return 1; + } + return -1; +} +uint16_t spigetw(spi_t spidev) +{ + if((spidev<3)&&(spidev>=0)) + { + SPI_TypeDef* _dev_ = _spi_dev_table[(int)spidev]; + while((_dev_->SR & (1<<7)) == (1<<7)); + _dev_->DR = 0xFFFF; + while((_dev_->SR & (1<<0)) == 0); + return _dev_->DR; + } + return -1; } -int spigets(spi_t* spidev,char* s) +int spiputs(spi_t spidev,char* s) { - do - { - (*s) = spigetw(spidev); - } - while(*s++); - return 1; + while (*s) spiputw(spidev,*s++); + return 1; } -int spiputnw(spi_t* spidev,uint16_t* w,int n) +int spigets(spi_t spidev,char* s) { - while(n!=0) - { - spiputw(spidev,*w++); - n--; - } - return 1; + do + { + (*s) = spigetw(spidev); + } + while(*s++); + return 1; } -int spigetnw(spi_t* spidev,uint16_t* w,int n) +int spiputnw(spi_t spidev,uint16_t* w,int n) { - while(n!=0) - { - *w++=spigetw(spidev); - n--; - } - return 1; + while(n!=0) + { + spiputw(spidev,*w++); + n--; + } + return 1; } -int spiputnc(spi_t* spidev,char* c,int n) +int spigetnw(spi_t spidev,uint16_t* w,int n) { - while(n!=0) - { - spiputw(spidev,*c++); - n--; - } - return 1; + while(n!=0) + { + *w++=spigetw(spidev); + n--; + } + return 1; } -int spigetnc(spi_t* spidev,char* c,int n) +int spiputnc(spi_t spidev,char* c,int n) { - while(n!=0) - { - *c++=spigetw(spidev); - n--; - } - return 1; + while(n!=0) + { + spiputw(spidev,*c++); + n--; + } + return 1; } -int spiavailiabledata(spi_t*spidev) +int spigetnc(spi_t spidev,char* c,int n) { - return 0; + while(n!=0) + { + *c++=spigetw(spidev); + n--; + } + return 1; +} + +int spiavailiabledata(spi_t spidev) +{ + return 0; } diff --git a/lib/src/stm32f4/UART/uart.c b/lib/src/stm32f4/UART/uart.c --- a/lib/src/stm32f4/UART/uart.c +++ b/lib/src/stm32f4/UART/uart.c @@ -28,323 +28,351 @@ #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) -int uartopen(int count ,uart_t* uart) +USART_TypeDef* _uart_dev_table[6]={USART1,USART2,USART3,UART4,UART5,USART6}; + + +uart_t uartopen(int count) { switch(count) { case 0: - uart->_dev = (void*)USART1; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); - ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9)); - //uartenable(uart); + USART1->CR3 &= ~((1<<8) + (1<<9)); + return (uart_t)0; break; case 1: - uart->_dev = (void*)USART2; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); - ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9)); - //uartenable(uart); + USART2->CR3 &= ~((1<<8) + (1<<9)); + return (uart_t)1; break; case 2: - uart->_dev = (void*)USART3; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); - ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9)); - //uartenable(uart); + USART3->CR3 &= ~((1<<8) + (1<<9)); + return (uart_t)2; break; case 3: - uart->_dev = (void*)UART4; RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); - ((USART_TypeDef *)(uart->_dev))->CR3 &= ~((1<<8) + (1<<9)); - //uartenable(uart); + UART4->CR3 &= ~((1<<8) + (1<<9)); + return (uart_t)3; break; case 4: - uart->_dev = (void*)UART5; RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); + return (uart_t)4; break; case 5: - uart->_dev = (void*)USART6; RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE); RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); + return (uart_t)5; break; default: break; } + return -1; +} + +uart_t uartopenandconfig(int count, uint32_t cfg, uint32_t speed, uint32_t TXpin, uint32_t RXpin, uint32_t RTSpin, uint32_t CTSpin) +{ + uart_t dev= uartopen(count); + uartsetconfig(dev,cfg,speed); + uartsetpins(dev,TXpin,RXpin,RTSpin,CTSpin); + return 1; +} + + +int uartclose(uart_t uart) +{ + switch(uart) + { + case 0: + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); + break; + case 1: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); + break; + case 2: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); + break; + case 3: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); + break; + case 4: + RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); + break; + case 5: + RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE); + break; + default: + return -1; + break; + } return 1; } -int uartopenandconfig(int count ,uart_t* uart,uint32_t cfg,uint32_t speed,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin) +int uartsetpins(uart_t uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin) { - uartopen(count ,uart); - uart->cfg = cfg; - uart->speed = speed; - uartsetconfig(uart); - uartsetpins(uart,TXpin,RXpin,RTSpin,CTSpin); - return 1; -} - - -int uartclose(uart_t* uart) -{ - switch((int)uart->_dev) + if(uart >5)return -1; + if(uart <0)return -1; + gpio_t TX,RX,CTS,RTS; + TX = gpioopen(TXpin); + RX = gpioopen(RXpin); + TX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; + RX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&TX); + gpiosetconfig(&RX); + uint8_t gpioAFuartx = GPIO_AF_USART1; + switch(uart) { - case (int)(void*)USART1: - uart->_dev = (void*)USART1; - RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE); - break; - case (int)(void*)USART2: - uart->_dev = (void*)USART2; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE); - break; - case (int)(void*)USART3: - uart->_dev = (void*)USART3; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE); - break; - case (int)(void*)UART4: - uart->_dev = (void*)UART4; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE); - break; - case (int)(void*)UART5: - uart->_dev = (void*)UART5; - RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE); - break; - case (int)(void*)USART6: - uart->_dev = (void*)USART6; - RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE); - break; - default: - break; - } - return 1; -} - -int uartsetpins(uart_t* uart,uint32_t TXpin,uint32_t RXpin,uint32_t RTSpin,uint32_t CTSpin) -{ - gpio_t TX,RX,CTS,RTS; - TX = gpioopen(TXpin); - RX = gpioopen(RXpin); - TX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; - RX |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&TX); - gpiosetconfig(&RX); - uint8_t gpioAFuartx = GPIO_AF_USART1; - switch((int)uart->_dev) - { - case (int)(void*)USART1: + case 0: gpioAFuartx = GPIO_AF_USART1; break; - case (int)(void*)USART2: + case 1: gpioAFuartx = GPIO_AF_USART2; break; - case (int)(void*)USART3: + case 2: gpioAFuartx = GPIO_AF_USART3; break; - case (int)(void*)UART4: + case 3: gpioAFuartx = GPIO_AF_UART4; break; - case (int)(void*)UART5: + case 4: gpioAFuartx = GPIO_AF_UART5; break; - case (int)(void*)USART6: + case 5: gpioAFuartx = GPIO_AF_USART6; break; default: + return -1; break; } - GPIO_PinAFConfig(GPIOGETPORT(TX), (uint8_t)(TX & 0xF), gpioAFuartx); - GPIO_PinAFConfig(GPIOGETPORT(RX), (uint8_t)(RX & 0xF), gpioAFuartx); - if((gpioAFuartx!=GPIO_AF_UART5) && (gpioAFuartx!=GPIO_AF_UART4)) - { + GPIO_PinAFConfig(GPIOGETPORT(TX), (uint8_t)(TX & 0xF), gpioAFuartx); + GPIO_PinAFConfig(GPIOGETPORT(RX), (uint8_t)(RX & 0xF), gpioAFuartx); + if((gpioAFuartx!=GPIO_AF_UART5) && (gpioAFuartx!=GPIO_AF_UART4)) + { if(CTSpin!=-1) { CTS = gpioopen(CTSpin); CTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; gpiosetconfig(&CTS); - GPIO_PinAFConfig(GPIOGETPORT(CTS), (uint8_t)(CTS & 0xF), gpioAFuartx); + GPIO_PinAFConfig(GPIOGETPORT(CTS), (uint8_t)(CTS & 0xF), gpioAFuartx); } - if(RTSpin!=-1) - { - RTS = gpioopen(RTSpin); - RTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; - gpiosetconfig(&RTS); - GPIO_PinAFConfig(GPIOGETPORT(RTS), (uint8_t)(RTS & 0xF), gpioAFuartx); - } + if(RTSpin!=-1) + { + RTS = gpioopen(RTSpin); + RTS |= gpiolowspeed | gpioaf | gpiopushpulltype | gpionopulltype; + gpiosetconfig(&RTS); + GPIO_PinAFConfig(GPIOGETPORT(RTS), (uint8_t)(RTS & 0xF), gpioAFuartx); } - return 1; + } + return 1; } -int uartsetconfig(uart_t* uart) +int uartsetconfig(uart_t uart, uint32_t cfg, uint32_t speed) { int res=1; uartdisable(uart); - uartsetspeed(uart,uart->speed); - uartsetparity(uart,uart->cfg & UARTPARITYMASK); - uartsetdatabits(uart,uart->cfg & UARTBITSMASK); - uartsetstopbits(uart,uart->cfg & UARTSTOPBITSMASK); + uartsetspeed(uart,speed); + uartsetparity(uart,cfg & UARTPARITYMASK); + uartsetdatabits(uart,cfg & UARTBITSMASK); + uartsetstopbits(uart,cfg & UARTSTOPBITSMASK); uartenable(uart); return res; } -int uartenable(uart_t* uart) +int uartenable(uart_t uart) { - ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<13); - ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<2) + (1<<3); - ((USART_TypeDef *)(uart->_dev))->DR = ' '; - return 1; + if((uart<6)&&(uart>=0)) + { + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + _dev_->CR1 |= (1<<13); + _dev_->CR1 |= (1<<2) + (1<<3); + _dev_->DR = ' '; + return 1; + } + return -1; } -int uartdisable(uart_t* uart) +int uartdisable(uart_t uart) { - if((((USART_TypeDef *)(uart->_dev))->CR1 & ((1<<3) +(1<<13)))==((1<<3) +(1<<13))) + if((uart<6)&&(uart>=0)) { - while((((USART_TypeDef *)(uart->_dev))->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7)); + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + if((_dev_->CR1 & ((1<<3) +(1<<13)))==((1<<3) +(1<<13))) + { + while((_dev_->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7)); + } + _dev_->CR1 &= ~((1<<2) + (1<<3) +(1<<13)); + return 1; } - ((USART_TypeDef *)(uart->_dev))->CR1 &= ~((1<<2) + (1<<3) +(1<<13)); - return 1; + return -1; } -int uartsetspeed(uart_t* uart,int speed) +int uartsetspeed(uart_t uart,uint32_t speed) { - uart->speed = speed; - uint32_t tmpreg = 0x00, apbclock = 0x00; - uint32_t integerdivider = 0x00; - uint32_t fractionaldivider = 0x00; - RCC_ClocksTypeDef RCC_ClocksStatus; - RCC_GetClocksFreq(&RCC_ClocksStatus); - - if (((USART_TypeDef *)(uart->_dev) == USART1) || (((USART_TypeDef *)(uart->_dev) == USART6))) - { - apbclock = RCC_ClocksStatus.PCLK2_Frequency; - } - else - { - apbclock = RCC_ClocksStatus.PCLK1_Frequency; - } - if (((((USART_TypeDef *)(uart->_dev))->CR1) & USART_CR1_OVER8) != (uint16_t)0) + if((uart<6)&&(uart>=0)) { - integerdivider = ((25 * apbclock) / (2 * (speed))); - } - else - { - integerdivider = ((25 * apbclock) / (4 * (speed))); + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + uint32_t tmpreg = 0x00, apbclock = 0x00; + uint32_t integerdivider = 0x00; + uint32_t fractionaldivider = 0x00; + RCC_ClocksTypeDef RCC_ClocksStatus; + RCC_GetClocksFreq(&RCC_ClocksStatus); + + if ((_dev_ == USART1) || ((_dev_ == USART6))) + { + apbclock = RCC_ClocksStatus.PCLK2_Frequency; + } + else + { + apbclock = RCC_ClocksStatus.PCLK1_Frequency; + } + if (((_dev_->CR1) & USART_CR1_OVER8) != (uint16_t)0) + { + integerdivider = ((25 * apbclock) / (2 * (speed))); + } + else + { + integerdivider = ((25 * apbclock) / (4 * (speed))); + } + tmpreg = (integerdivider / 100) << 4; + fractionaldivider = integerdivider - (100 * (tmpreg >> 4)); + if ((_dev_->CR1 & USART_CR1_OVER8) != (uint16_t)0) + { + tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07); + } + else + { + tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F); + } + _dev_->BRR = (uint16_t)tmpreg; + return 1; } - tmpreg = (integerdivider / 100) << 4; - fractionaldivider = integerdivider - (100 * (tmpreg >> 4)); - if ((((USART_TypeDef *)(uart->_dev))->CR1 & USART_CR1_OVER8) != (uint16_t)0) - { - tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07); - } - else - { - tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F); - } - ((USART_TypeDef *)(uart->_dev))->BRR = (uint16_t)tmpreg; - return 1; + return -1; } -int uartsetparity(uart_t* uart,uartparity_t parity) +int uartsetparity(uart_t uart,uartparity_t parity) { - uart->cfg &= ~(UARTPARITYMASK); - uart->cfg |= parity; - ((USART_TypeDef *)(uart->_dev))->CR1 &= ~(((1<<9)+(1<<10))); - switch(parity) + if((uart<6)&&(uart>=0)) { - case uartparityeven: - ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10); - break; - case uartparityodd: - ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<10) + (1<<9); - break; - case uartparitynone: - break; - default : - return 0; - break; + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + _dev_->CR1 &= ~(((1<<9)+(1<<10))); + switch(parity) + { + case uartparityeven: + _dev_->CR1 |= (1<<10); + break; + case uartparityodd: + _dev_->CR1 |= (1<<10) + (1<<9); + break; + case uartparitynone: + break; + default : + return 0; + break; + } + return 1; } - return 1; + return -1; +} + +int uartsetdatabits(uart_t uart,uartbits_t databits) +{ + if((uart<6)&&(uart>=0)) + { + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + _dev_->CR1 &= ~(((1<<12))); + switch(databits) + { + case uart7bits: + return 0; + break; + case uart8bits: + break; + case uart9bits: + _dev_->CR1 |= (1<<12); + break; + default : + return 0; + break; + } + return 1; + } + return -1; } -int uartsetdatabits(uart_t* uart,uartbits_t databits) +int uartsetstopbits(uart_t uart,uartstopbits_t stopbits) { - uart->cfg &= ~UARTBITSMASK; - uart->cfg |= databits; - ((USART_TypeDef *)(uart->_dev))->CR1 &= ~(((1<<12))); - switch(databits) + if((uart<6)&&(uart>=0)) { - case uart7bits: - return 0; - break; - case uart8bits: - break; - case uart9bits: - ((USART_TypeDef *)(uart->_dev))->CR1 |= (1<<12); - break; - default : - return 0; - break; + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + _dev_->CR2 &= ~(((1<<12)+(1<<13))); + switch(stopbits) + { + case uarthalfstop: + _dev_->CR2 |= (1<<12); + break; + case uartonestop: + break; + case uartonehalfstop: + _dev_->CR2 |= (1<<12) + (1<<13); + break; + case uarttwostop: + _dev_->CR2 |= (1<<13); + break; + default : + return 0; + break; + } + return 1; } - return 1; + return -1; } -int uartsetstopbits(uart_t* uart,uartstopbits_t stopbits) +int uartputc(uart_t uart,char c) { - uart->cfg &= ~UARTSTOPBITSMASK; - uart->cfg |= stopbits; - ((USART_TypeDef *)(uart->_dev))->CR2 &= ~(((1<<12)+(1<<13))); - switch(stopbits) + if((uart<6)&&(uart>=0)) { - case uarthalfstop: - ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12); - break; - case uartonestop: - break; - case uartonehalfstop: - ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<12) + (1<<13); - break; - case uarttwostop: - ((USART_TypeDef *)(uart->_dev))->CR2 |= (1<<13); - break; - default : - return 0; - break; + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + while((_dev_->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7)); + _dev_->DR = c; + return 1; } - return 1; + return -1; } -int uartputc(uart_t* uart,char c) +char uartgetc(uart_t uart) { - while((((USART_TypeDef *)(uart->_dev))->SR & (uint16_t)(1<<7))!=(uint16_t)(1<<7)); - ((USART_TypeDef *)(uart->_dev))->DR = c; - return 1; + if((uart<6)&&(uart>=0)) + { + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + while(!(_dev_->SR & (1<<5))); + return (char)_dev_->DR; + } + return -1; } -char uartgetc(uart_t* uart) -{ - while(!(((USART_TypeDef *)(uart->_dev))->SR & (1<<5))); - return (char)((USART_TypeDef *)(uart->_dev))->DR; -} - -int uartputs(uart_t* uart,char* s) +int uartputs(uart_t uart,char* s) { while (*s) uartputc(uart,*s++); return 1; } -int uartgets(uart_t* uart,char* s) +int uartgets(uart_t uart,char* s) { do { @@ -354,7 +382,7 @@ int uartgets(uart_t* uart,char* s) return 1; } -int uartputnc(uart_t* uart,char* c,int n) +int uartputnc(uart_t uart,char* c,int n) { int l=0; while(l_dev))->SR & (1<<5))) - return 0; - else - return 1; + if((uart<6)&&(uart>=0)) + { + USART_TypeDef* _dev_ = _uart_dev_table[(int)uart]; + if(!(_dev_->SR & (1<<5))) + return 0; + else + return 1; + } + return -1; } int _uartstrwrite(streamdevice* device,void* data,int size, int n) { - return uartputnc((uart_t*) device->_stream,(char*) data,size*n); + return uartputnc((uart_t) device->_stream,(char*) data,size*n); } int _uartstrread(streamdevice* device,void* data,int size, int n) { - return uartgetnc((uart_t*) device->_stream,(char*) data,size*n); + return uartgetnc((uart_t) device->_stream,(char*) data,size*n); } int _uartstrsetpos(streamdevice* device,int pos) @@ -400,7 +433,7 @@ int _uartstrsetpos(streamdevice* device, return 1; } -int uartmkstreamdev(uart_t* uart,streamdevice* strdev) +int uartmkstreamdev(uart_t uart,streamdevice* strdev) { strdev->_stream = (UHANDLE)uart; strdev->write = (write_t)&_uartstrwrite; diff --git a/rules/stm32f4-arm-none-eabi-gcc/syscalls.c b/rules/stm32f4-arm-none-eabi-gcc/syscalls.c --- a/rules/stm32f4-arm-none-eabi-gcc/syscalls.c +++ b/rules/stm32f4-arm-none-eabi-gcc/syscalls.c @@ -100,11 +100,12 @@ int _open(const char *name, int flags, i { if(!strncmp("UART", name, 4) && ((name[4] & 0x30)==0x30)) { - uart_t* uart1 = malloc(sizeof(uart_t)); + //uart_t* uart1 = malloc(sizeof(uart_t)); streamdevice* fd1 = malloc(sizeof(streamdevice)); - if(uartopen((name[4] & 0xF)-1,uart1)); + uart_t uart=uartopen((name[4] & 0xF)-1); + if(uart!=-1); { - uartmkstreamdev(uart1,fd1); + uartmkstreamdev(uart,fd1); int i=2; while(i<__max_opened_files__ && __opnfiles__[i]!=NULL)i++; if(i!=__max_opened_files__)