Commit 16baf393 authored by Iulian Gheorghiu's avatar Iulian Gheorghiu

Fix the setup of bitrate after initialization.

parent d06f518b
......@@ -14,6 +14,8 @@
#include <sys/core_init.h>
#include <io_defs.h>
extern unsigned long FCPU;
#if (USE_DRIVER_SEMAPHORE == true)
volatile bool spi_semaphore[SPI_INTERFACE_COUNT];
#endif
......@@ -46,7 +48,6 @@ GI::Dev::Spi::Spi(ioSettings *cfg):
disableCsHandle(false),
oldCsSelect(false),
wordSize(8),
speed(4000000),
slave(false),
lsbFirst(false),
cPha(0),
......@@ -83,10 +84,6 @@ GI::Dev::Spi::Spi(ioSettings *cfg):
this->cfg = cfg;
CfgSpi *int_cfg = (CfgSpi *)cfg->cfg;
/*volatile FPGA_PORT_t *BaseAddr = GPIO_BASE_PTRS[int_cfg->cs >> 5];
BaseAddr->OUTSET = pgm_read_byte(&BIT_MASK_TABLE[int_cfg->cs % 8]);
BaseAddr->DIRSET = pgm_read_byte(&BIT_MASK_TABLE[int_cfg->cs % 8]);*/
unsigned char tmp = FPGA_SPI_MODE_gm & (int_cfg->spiMode << FPGA_SPI_MODE_gp);
u32 new_speed = 0;
......@@ -112,6 +109,7 @@ GI::Dev::Spi::Spi(ioSettings *cfg):
tmp |= FPGA_SPI_ENABLE_bm | FPGA_SPI_MASTER_bm;
SPI_BASE_PTRS[unitNr]->CTRL = tmp;
speed = int_cfg->speed;
}
/**
......@@ -141,9 +139,6 @@ int GI::Dev::Spi::assert()
err = SYS_ERR_INVALID_HANDLER;
return SYS_ERR_INVALID_HANDLER;
}
#if (USE_DRIVER_SEMAPHORE == true)
while (spi_semaphore[unitNr]);
#endif
setSpeed(speed);
#if (USE_DRIVER_SEMAPHORE == true)
spi_semaphore[unitNr] = true;
......@@ -350,28 +345,28 @@ SysErr GI::Dev::Spi::setSpeed(unsigned long baud)
while (spi_semaphore[unitNr]);
#endif
FPGA_SPI_t *port = (FPGA_SPI_t *) userData;
//SPI_SetBaudRate(hspi, FCPU/2, int_cfg->speed);
CfgSpi *int_cfg = (CfgSpi *)cfg->cfg;
u32 new_speed = 0;
if(int_cfg->speed > FCPU/2)
if(baud > FCPU/2)
new_speed = FPGA_SPI_CLK2X_bm | FPGA_SPI_PRESCALER_DIV4_gc;
else if(int_cfg->speed > FCPU/4)
else if(baud > FCPU/4)
new_speed = FPGA_SPI_PRESCALER_DIV4_gc;
else if(int_cfg->speed > FCPU/8)
else if(baud > FCPU/8)
new_speed = FPGA_SPI_CLK2X_bm | FPGA_SPI_PRESCALER_DIV16_gc;
else if(int_cfg->speed > FCPU/16)
else if(baud > FCPU/16)
new_speed = FPGA_SPI_PRESCALER_DIV16_gc;
else if(int_cfg->speed > FCPU/32)
else if(baud > FCPU/32)
new_speed = FPGA_SPI_CLK2X_bm | FPGA_SPI_PRESCALER_DIV64_gc;
else if(int_cfg->speed > FCPU/64)
else if(baud > FCPU/64)
new_speed = FPGA_SPI_PRESCALER_DIV64_gc;
else if(int_cfg->speed > FCPU/128)
else if(baud > FCPU/128)
new_speed = FPGA_SPI_PRESCALER_DIV128_gc;
else
new_speed = FPGA_SPI_PRESCALER_DIV128_gc;
port->CTRL = (port->CTRL & ~(FPGA_SPI_CLK2X_bm | FPGA_SPI_PRESCALER_gm)) | new_speed;
speed = baud;
err = SYS_ERR_OK;
return SYS_ERR_OK;
}
......
......@@ -155,7 +155,7 @@ SysErr GI::Dev::Uart::setSpeed(unsigned long baudRate)
if(!this || !udata)
return SYS_ERR_INVALID_HANDLER;
CfgUart *int_cfg = (CfgUart *)cfg->cfg;
unsigned int ubrr = ((FCPU / 8) / int_cfg->speed) -1;
unsigned int ubrr = ((FCPU / 8) / baudRate) -1;
((FPGA_USART_t*)udata)->BAUDCTRLA = (unsigned char)ubrr;
((FPGA_USART_t*)udata)->BAUDCTRLB = (unsigned char)((ubrr>>8) & 0x0F);
return SYS_ERR_OK;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment