Browse Source

reformat source files

Signed-off-by: surenyi <surenyi82@163.com>
master
surenyi 5 years ago
parent
commit
bb15e11cf2
  1. 70
      lib/fifo.c
  2. 114
      lib/flash_if.c
  3. 18
      lib/gcc_stubs.c
  4. 44
      lib/iic.c
  5. 630
      lib/serial.c
  6. 6
      lib/spi.c
  7. 8
      lib/tick.c
  8. 100
      lib/utils.c
  9. 346
      lib/wm8978.c
  10. 796
      lib/ymodem.c

70
lib/fifo.c

@ -12,20 +12,20 @@
static unsigned int roundup_pow_of_two(unsigned int x)
{
int i = 0;
if (x != 0) {
if (is_power_of_2(x)) {
return x;
}
do{
++i;
x >>= 1;
} while(x > 0);
}
return 1UL << i;
int i = 0;
if (x != 0) {
if (is_power_of_2(x)) {
return x;
}
do{
++i;
x >>= 1;
} while(x > 0);
}
return 1UL << i;
}
int fifo_init(fifo_t fifo, void *buf, unsigned int len)
@ -46,48 +46,48 @@ int fifo_init(fifo_t fifo, void *buf, unsigned int len)
unsigned int fifo_put(fifo_t fifo, const unsigned char *buffer, unsigned int len)
{
unsigned int l;
unsigned int l;
len = min(len, fifo->size - fifo->in + fifo->out);
l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
memcpy(fifo->buffer, buffer + l, len - l);
fifo->in += len;
len = min(len, fifo->size - fifo->in + fifo->out);
l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
memcpy(fifo->buffer, buffer + l, len - l);
fifo->in += len;
return len;
return len;
}
unsigned int fifo_get(fifo_t fifo, unsigned char *buffer, unsigned int len)
{
unsigned int l;
unsigned int l;
len = min(len, fifo->in - fifo->out);
l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
memcpy(buffer + l, fifo->buffer, len - l);
fifo->out += len;
len = min(len, fifo->in - fifo->out);
l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
memcpy(buffer + l, fifo->buffer, len - l);
fifo->out += len;
return len;
return len;
}
void fifo_reset(fifo_t fifo)
{
fifo->in = fifo->out = 0;
fifo->in = fifo->out = 0;
}
unsigned int fifo_len(struct fifo *fifo)
{
unsigned int ret;
unsigned int ret;
ret = fifo->in - fifo->out;
ret = fifo->in - fifo->out;
return ret;
return ret;
}
unsigned int fifo_empty(struct fifo *fifo)
{
unsigned int ret;
ret = fifo->size - (fifo->in - fifo->out);
return ret;
unsigned int ret;
ret = fifo->size - (fifo->in - fifo->out);
return ret;
}

114
lib/flash_if.c

@ -107,13 +107,13 @@ int flash_if_erase(uint32_t start_address, uint32_t length)
for (i = start; i <= end; i += 8) {
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE) {
/* Error occurred while page erase */
return (1);
}
}
if (FLASH_EraseSector(i, VoltageRange_3) != FLASH_COMPLETE) {
/* Error occurred while page erase */
return (1);
}
}
return (0);
return (0);
}
/**
@ -128,28 +128,30 @@ int flash_if_erase(uint32_t start_address, uint32_t length)
*/
int flash_if_write(uint32_t flash_address, const void *data ,uint32_t length)
{
uint32_t i = 0;
const uint32_t *ptr = data;
length /= 4;
for (i = 0; (i < length) && (flash_address <= (USER_FLASH_END_ADDRESS-4)); i++) {
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (FLASH_ProgramWord(flash_address, ptr[i]) == FLASH_COMPLETE) {
/* Check the written value */
if (*(volatile uint32_t*)flash_address != ptr[i]) {
/* Flash content doesn't match SRAM content */
return (2);
}
/* Increment FLASH destination address */
flash_address += 4;
} else {
/* Error occurred while writing data in Flash memory */
return (1);
}
}
return (0);
uint32_t i = 0;
const uint32_t *ptr = data;
length /= 4;
for (i = 0; (i < length) && (flash_address <= (USER_FLASH_END_ADDRESS-4)); i++) {
/*
* Device voltage range supposed to be [2.7V to 3.6V], the operation will
* be done by word
*/
if (FLASH_ProgramWord(flash_address, ptr[i]) == FLASH_COMPLETE) {
/* Check the written value */
if (*(volatile uint32_t*)flash_address != ptr[i]) {
/* Flash content doesn't match SRAM content */
return (2);
}
/* Increment FLASH destination address */
flash_address += 4;
} else {
/* Error occurred while writing data in Flash memory */
return (1);
}
}
return (0);
}
/**
@ -160,18 +162,18 @@ int flash_if_write(uint32_t flash_address, const void *data ,uint32_t length)
*/
int flash_if_get_write_protection_status(uint32_t address)
{
uint32_t UserStartSector;
/* Get the sector where start the user flash area */
UserStartSector = get_sector(address);
/* Check if there are write protected sectors inside the user flash area */
if ((FLASH_OB_GetWRP() >> (UserStartSector/8)) == (0xFFF >> (UserStartSector/8))) {
/* No write protected sectors inside the user flash area */
return 1;
} else { /* Some sectors inside the user flash area are write protected */
return 0;
}
uint32_t UserStartSector;
/* Get the sector where start the user flash area */
UserStartSector = get_sector(address);
/* Check if there are write protected sectors inside the user flash area */
if ((FLASH_OB_GetWRP() >> (UserStartSector/8)) == (0xFFF >> (UserStartSector/8))) {
/* No write protected sectors inside the user flash area */
return 1;
} else { /* Some sectors inside the user flash area are write protected */
return 0;
}
}
/**
@ -182,27 +184,27 @@ int flash_if_get_write_protection_status(uint32_t address)
*/
int flash_if_disable_write_protection(uint32_t address)
{
__IO uint32_t UserStartSector, UserWrpSectors = OB_WRP_Sector_1;
__IO uint32_t UserStartSector, UserWrpSectors = OB_WRP_Sector_1;
/* Get the sector where start the user flash area */
UserStartSector = get_sector(address);
/* Get the sector where start the user flash area */
UserStartSector = get_sector(address);
/* Mark all sectors inside the user flash area as non protected */
UserWrpSectors = 0xFFF-((1 << (UserStartSector/8))-1);
/* Mark all sectors inside the user flash area as non protected */
UserWrpSectors = 0xFFF-((1 << (UserStartSector/8))-1);
/* Unlock the Option Bytes */
FLASH_OB_Unlock();
/* Unlock the Option Bytes */
FLASH_OB_Unlock();
/* Disable the write protection for all sectors inside the user flash area */
FLASH_OB_WRPConfig(UserWrpSectors, DISABLE);
/* Disable the write protection for all sectors inside the user flash area */
FLASH_OB_WRPConfig(UserWrpSectors, DISABLE);
/* Start the Option Bytes programming process. */
if (FLASH_OB_Launch() != FLASH_COMPLETE) {
/* Error: Flash write unprotection failed */
return (2);
}
/* Start the Option Bytes programming process. */
if (FLASH_OB_Launch() != FLASH_COMPLETE) {
/* Error: Flash write unprotection failed */
return (2);
}
/* Write Protection successfully disabled */
return (1);
/* Write Protection successfully disabled */
return (1);
}

18
lib/gcc_stubs.c

@ -63,10 +63,10 @@ int _execve(char *name, char **argv, char **env)
*/
void _exit( int code )
{
/* Should we force a system reset? */
while (1) {
;
}
/* Should we force a system reset? */
while (1) {
;
}
}
/*
@ -180,7 +180,7 @@ int _write_r( void * reent, int file, char * ptr, int len )
* Increase program data space. As malloc and related functions depend on this,
* it is useful to have a working implementation. The following suffices for a
* standalone system; it exploits the symbol _end automatically defined by the
* GNU linker.
* GNU linker.
*/
static char __heap[TARGET_HEAP_SIZE] __attribute__((section(".heap._sbrk,\"aw\",%nobits@")));
@ -188,13 +188,13 @@ caddr_t _sbrk(int incr)
{
static char *heap_start = __heap;
char *prev_heap_end;
prev_heap_end = heap_start;
if (heap_start + incr > (__heap + TARGET_HEAP_SIZE)) {
_write_r ((void *)0, 1, "heap and stack collision\r\n", 26);
return (caddr_t)NULL;
_write_r ((void *)0, 1, "heap and stack collision\r\n", 26);
return (caddr_t)NULL;
}
heap_start += incr;
return (caddr_t) prev_heap_end;

44
lib/iic.c

@ -12,14 +12,14 @@ struct iic_descr {
};
#define recovery(i2c) do {\
I2C_SoftwareResetCmd(i2c->base, ENABLE); \
I2C_SoftwareResetCmd(i2c->base, ENABLE); \
delay_microseconds(1); \
I2C_SoftwareResetCmd(i2c->base, DISABLE); \
I2C_SoftwareResetCmd(i2c->base, DISABLE); \
} while (0)
int iic_setup(struct iic_descr *i2c, int speed)
{
I2C_InitTypeDef i2c_conf;
I2C_InitTypeDef i2c_conf;
/* enable APB1 peripheral clock for I2C*/
RCC_APB1PeriphClockCmd(i2c->rcc, ENABLE);
@ -60,7 +60,7 @@ int iic_start(struct iic_descr *i2c)
while(!I2C_CheckEvent(i2c->base, I2C_EVENT_MASTER_MODE_SELECT)) {
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) {
recovery(i2c);
recovery(i2c);
return -1;
}
}
@ -70,7 +70,7 @@ int iic_start(struct iic_descr *i2c)
int iic_ack(struct iic_descr *i2c, int enable)
{
/* Prepare an NACK for the next data received */
I2C_AcknowledgeConfig(i2c->base, enable ? ENABLE : DISABLE);
I2C_AcknowledgeConfig(i2c->base, enable ? ENABLE : DISABLE);
return 0;
}
@ -91,10 +91,10 @@ int iic_send_slave_address(struct iic_descr *i2c, uint8_t address)
while(!I2C_CheckEvent(i2c->base, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) {
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) {
recovery(i2c);
recovery(i2c);
return -1;
}
}
}
} else { /* write */
/* Send Memory device slave Address for write */
I2C_Send7bitAddress(i2c->base, address, I2C_Direction_Transmitter);
@ -102,7 +102,7 @@ int iic_send_slave_address(struct iic_descr *i2c, uint8_t address)
while(!I2C_CheckEvent(i2c->base, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) {
recovery(i2c);
recovery(i2c);
return -1;
}
}
@ -120,7 +120,7 @@ int iic_send_byte(struct iic_descr *i2c, uint8_t data)
while(!I2C_CheckEvent(i2c->base, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) {
recovery(i2c);
recovery(i2c);
return -1;
}
}
@ -134,7 +134,7 @@ uint8_t iic_recv_byte(struct iic_descr *i2c)
while (!I2C_CheckEvent(i2c->base, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
/* If the timeout delay is exeeded, exit with error code */
if ((timeout--) == 0) {
recovery(i2c);
recovery(i2c);
return 0xff;
}
}
@ -146,13 +146,13 @@ static void i2c1_setup(struct iic_descr *i2c)
{
GPIO_InitTypeDef gpio_conf;
/*
/*
* enable the peripheral clock for the pins used by
* PB6 for I2C SCL and PB9 for I2C1_SDL
*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/*
/*
* This sequence sets up the I2C1SDA and I2C1SCL pins
* so they work correctly with the I2C1 peripheral
*/
@ -168,9 +168,9 @@ static void i2c1_setup(struct iic_descr *i2c)
gpio_conf.GPIO_OType = GPIO_OType_OD; // this defines the output type as open drain
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;// this activates the pullup resistors on the IO pins
#endif
GPIO_Init(GPIOB, &gpio_conf); // now all the values are passed to the GPIO_Init()
GPIO_Init(GPIOB, &gpio_conf); // now all the values are passed to the GPIO_Init()
/*
/*
* The I2C1_SCL and I2C1_SDA pins are now connected to their AF
* so that the I2C1 can take over control of the pins
*/
@ -190,14 +190,14 @@ static void i2c3_setup(struct iic_descr *i2c)
{
GPIO_InitTypeDef gpio_conf;
/*
/*
* enable the peripheral clock for the pins used by
* PA8 for I2C3_SCL and PC9 for I2C3_SDL
*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/*
/*
* This sequence sets up the I2C1SDA and I2C1SCL pins
* so they work correctly with the I2C1 peripheral
*/
@ -208,12 +208,12 @@ static void i2c3_setup(struct iic_descr *i2c)
gpio_conf.GPIO_Speed = GPIO_Speed_2MHz; // this defines the IO speed and has nothing to do with the baudrate!
gpio_conf.GPIO_OType = GPIO_OType_OD; // this defines the output type as open drain
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;// this activates the pullup resistors on the IO pins
GPIO_Init(GPIOA, &gpio_conf); // now all the values are passed to the GPIO_Init()
GPIO_Init(GPIOA, &gpio_conf); // now all the values are passed to the GPIO_Init()
gpio_conf.GPIO_Pin = GPIO_Pin_9; // Pins 9(I2C3_SDA)
GPIO_Init(GPIOC, &gpio_conf); // now all the values are passed to the GPIO_Init()
GPIO_Init(GPIOC, &gpio_conf); // now all the values are passed to the GPIO_Init()
/*
/*
* The I2C3_SCL and I2C3_SDA pins are now connected to their AF
* so that the I2C1 can take over control of the pins
*/

630
lib/serial.c

@ -11,122 +11,122 @@
struct serial {
const char *name;
void (*setup)(struct serial *);
/* uart */
USART_TypeDef *uart;
uint32_t rcc_uart;
void (*setup)(struct serial *);
/* uart */
USART_TypeDef *uart;
uint32_t rcc_uart;
int8_t tx_irq;
int8_t tx_irq_priority;
int8_t tx_irq;
int8_t tx_irq_priority;
int8_t eie_irq;
int8_t eie_irq_priority;
uint32_t dma_rcc;
uint32_t tx_dma_channel;
uint32_t rx_dma_channel;
DMA_Stream_TypeDef * rx_dma_stream;
DMA_Stream_TypeDef * tx_dma_stream;
uint32_t tx_dma_tcflag;
DMA_InitTypeDef tx_dma_conf;
uint32_t dma_rcc;
uint32_t tx_dma_channel;
uint32_t rx_dma_channel;
DMA_Stream_TypeDef * rx_dma_stream;
DMA_Stream_TypeDef * tx_dma_stream;
uint32_t tx_dma_tcflag;
DMA_InitTypeDef tx_dma_conf;
volatile uint8_t *tx_buffer;
uint16_t tx_buffer_size;
volatile uint8_t *tx_buffer;
uint16_t tx_buffer_size;
volatile uint8_t *tx_dma_buffer;
volatile uint8_t *tx_dma_buffer;
volatile uint8_t *rx_buffer;
uint16_t rx_buffer_size;
volatile uint16_t tx_head;
volatile uint16_t tx_tail;
uint16_t rx_head;
volatile uint8_t *rx_buffer;
uint16_t rx_buffer_size;
volatile uint16_t tx_head;
volatile uint16_t tx_tail;
uint16_t rx_head;
};
void serial_reconfigure(struct serial *uart, int baudrate, int databits, int stopbits, int parity)
{
USART_InitTypeDef uart_conf;
USART_InitTypeDef uart_conf;
USART_Cmd(uart->uart, DISABLE);
USART_DeInit(uart->uart);
/* Set up USART/UART using default parameters */
USART_StructInit(&uart_conf);
switch (databits) {
case 9:
uart_conf.USART_WordLength = USART_WordLength_9b;
break;
default:
uart_conf.USART_WordLength = USART_WordLength_8b;
break;
}
uart_conf.USART_BaudRate = baudrate;
uart_conf.USART_StopBits = stopbits;
USART_DeInit(uart->uart);
/* Set up USART/UART using default parameters */
USART_StructInit(&uart_conf);
switch (databits) {
case 9:
uart_conf.USART_WordLength = USART_WordLength_9b;
break;
default:
uart_conf.USART_WordLength = USART_WordLength_8b;
break;
}
uart_conf.USART_BaudRate = baudrate;
uart_conf.USART_StopBits = stopbits;
uart_conf.USART_Parity = parity ;
uart_conf.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart_conf.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(uart->uart, &uart_conf);
USART_Init(uart->uart, &uart_conf);
USART_Cmd(uart->uart, ENABLE);
}
void serial_setup(struct serial *uart, int baudrate, int databits, int stopbits)
{
DMA_InitTypeDef dma_conf;
NVIC_InitTypeDef irq_conf;
DMA_InitTypeDef dma_conf;
NVIC_InitTypeDef irq_conf;
/* setup pins */
uart->setup(uart);
/* setup pins */
uart->setup(uart);
RCC_AHB1PeriphClockCmd(uart->dma_rcc, ENABLE);
RCC_AHB1PeriphClockCmd(uart->dma_rcc, ENABLE);
serial_reconfigure(uart, baudrate, databits, stopbits, SERIAL_PARITY_NO);
serial_reconfigure(uart, baudrate, databits, stopbits, SERIAL_PARITY_NO);
USART_Cmd(uart->uart, DISABLE);
/* Configure RX DMA ... */
DMA_DeInit(uart->rx_dma_stream);
DMA_StructInit(&dma_conf);
dma_conf.DMA_Channel = uart->rx_dma_channel;
dma_conf.DMA_PeripheralBaseAddr = (uint32_t)&uart->uart->DR;
dma_conf.DMA_Memory0BaseAddr = (uint32_t)uart->rx_buffer;
dma_conf.DMA_BufferSize = uart->rx_buffer_size;
dma_conf.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma_conf.DMA_Mode = DMA_Mode_Circular;
dma_conf.DMA_Priority = DMA_Priority_High;
dma_conf.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma_conf.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_Init(uart->rx_dma_stream, &dma_conf);
/* Configure TX DMA ... */
DMA_DeInit(uart->tx_dma_stream);
DMA_StructInit(&dma_conf);
dma_conf.DMA_Channel = uart->tx_dma_channel;
dma_conf.DMA_PeripheralBaseAddr = (uint32_t)&uart->uart->DR;
dma_conf.DMA_Memory0BaseAddr = (uint32_t)uart->tx_dma_buffer;
dma_conf.DMA_BufferSize = TX_DMA_BUFFER_SIZE;
dma_conf.DMA_DIR = DMA_DIR_MemoryToPeripheral;
dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma_conf.DMA_Priority = DMA_Priority_High;
DMA_Init(uart->tx_dma_stream, &dma_conf);
uart->tx_dma_conf = dma_conf;
DMA_Cmd(uart->rx_dma_stream, ENABLE);
DMA_Cmd(uart->tx_dma_stream, DISABLE);
DMA_ITConfig(uart->tx_dma_stream, DMA_IT_TC, ENABLE);
USART_DMACmd(uart->uart, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE);
memset(&irq_conf, 0, sizeof irq_conf);
irq_conf.NVIC_IRQChannel = uart->tx_irq;
if (uart->tx_irq_priority > 0) {
irq_conf.NVIC_IRQChannelPreemptionPriority = uart->tx_irq_priority;
}
irq_conf.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&irq_conf);
DMA_DeInit(uart->rx_dma_stream);
DMA_StructInit(&dma_conf);
dma_conf.DMA_Channel = uart->rx_dma_channel;
dma_conf.DMA_PeripheralBaseAddr = (uint32_t)&uart->uart->DR;
dma_conf.DMA_Memory0BaseAddr = (uint32_t)uart->rx_buffer;
dma_conf.DMA_BufferSize = uart->rx_buffer_size;
dma_conf.DMA_DIR = DMA_DIR_PeripheralToMemory;
dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma_conf.DMA_Mode = DMA_Mode_Circular;
dma_conf.DMA_Priority = DMA_Priority_High;
dma_conf.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
dma_conf.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_Init(uart->rx_dma_stream, &dma_conf);
/* Configure TX DMA ... */
DMA_DeInit(uart->tx_dma_stream);
DMA_StructInit(&dma_conf);
dma_conf.DMA_Channel = uart->tx_dma_channel;
dma_conf.DMA_PeripheralBaseAddr = (uint32_t)&uart->uart->DR;
dma_conf.DMA_Memory0BaseAddr = (uint32_t)uart->tx_dma_buffer;
dma_conf.DMA_BufferSize = TX_DMA_BUFFER_SIZE;
dma_conf.DMA_DIR = DMA_DIR_MemoryToPeripheral;
dma_conf.DMA_MemoryInc = DMA_MemoryInc_Enable;
dma_conf.DMA_Priority = DMA_Priority_High;
DMA_Init(uart->tx_dma_stream, &dma_conf);
uart->tx_dma_conf = dma_conf;
DMA_Cmd(uart->rx_dma_stream, ENABLE);
DMA_Cmd(uart->tx_dma_stream, DISABLE);
DMA_ITConfig(uart->tx_dma_stream, DMA_IT_TC, ENABLE);
USART_DMACmd(uart->uart, USART_DMAReq_Tx | USART_DMAReq_Rx, ENABLE);
memset(&irq_conf, 0, sizeof irq_conf);
irq_conf.NVIC_IRQChannel = uart->tx_irq;
if (uart->tx_irq_priority > 0) {
irq_conf.NVIC_IRQChannelPreemptionPriority = uart->tx_irq_priority;
}
irq_conf.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&irq_conf);
if (uart->eie_irq >= 0) {
memset(&irq_conf, 0, sizeof irq_conf);
@ -137,7 +137,7 @@ void serial_setup(struct serial *uart, int baudrate, int databits, int stopbits)
irq_conf.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&irq_conf);
USART_ITConfig(uart->uart, USART_IT_PE | USART_IT_ERR | USART_IT_NE | USART_IT_FE, ENABLE);
USART_ITConfig(uart->uart, USART_IT_PE | USART_IT_ERR | USART_IT_NE | USART_IT_FE, ENABLE);
}
/* Enable the UART with the new settings */
@ -146,25 +146,25 @@ void serial_setup(struct serial *uart, int baudrate, int databits, int stopbits)
void serial_close(struct serial *uart)
{
NVIC_InitTypeDef irq_conf;
NVIC_InitTypeDef irq_conf;
DMA_ITConfig(uart->tx_dma_stream, DMA_IT_TC, DISABLE);
DMA_ITConfig(uart->tx_dma_stream, DMA_IT_TC, DISABLE);
USART_DMACmd(uart->uart, USART_DMAReq_Tx | USART_DMAReq_Rx, DISABLE);
USART_DMACmd(uart->uart, USART_DMAReq_Tx | USART_DMAReq_Rx, DISABLE);
DMA_Cmd(uart->rx_dma_stream, DISABLE);
DMA_Cmd(uart->tx_dma_stream, DISABLE);
DMA_Cmd(uart->rx_dma_stream, DISABLE);
DMA_Cmd(uart->tx_dma_stream, DISABLE);
irq_conf.NVIC_IRQChannel = uart->tx_irq;
irq_conf.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&irq_conf);
irq_conf.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&irq_conf);
USART_Cmd(uart->uart, DISABLE);
USART_DeInit(uart->uart);
DMA_DeInit(uart->rx_dma_stream);
DMA_DeInit(uart->tx_dma_stream);
DMA_DeInit(uart->rx_dma_stream);
DMA_DeInit(uart->tx_dma_stream);
}
static int tx_buffer_empty(struct serial *uart)
@ -305,29 +305,29 @@ size_t serial_recv_blocking(struct serial *uart, uint8_t* data, size_t max_bytes
__eie_isr(uart);
while (i > 0) {
while (USART_GetFlagStatus(uart->uart, USART_FLAG_RXNE) == RESET)
;
*data++ = USART_ReceiveData(uart->uart);
while (i > 0) {
while (USART_GetFlagStatus(uart->uart, USART_FLAG_RXNE) == RESET)
;
*data++ = USART_ReceiveData(uart->uart);
--i;
}
return (max_bytes);
}
return (max_bytes);
}
static int dma_tx(struct serial *uart)
{
int i;
int i;
for (i = 0; i < TX_DMA_BUFFER_SIZE && !tx_buffer_empty(uart); ++i) {
uart->tx_dma_buffer[i] = tx_buffer_get(uart);
}
for (i = 0; i < TX_DMA_BUFFER_SIZE && !tx_buffer_empty(uart); ++i) {
uart->tx_dma_buffer[i] = tx_buffer_get(uart);
}
if (i > 0) {
uart->tx_dma_conf.DMA_BufferSize = i;
DMA_Init(uart->tx_dma_stream, &uart->tx_dma_conf);
DMA_Cmd(uart->tx_dma_stream, ENABLE);
}
return (i);
if (i > 0) {
uart->tx_dma_conf.DMA_BufferSize = i;
DMA_Init(uart->tx_dma_stream, &uart->tx_dma_conf);
DMA_Cmd(uart->tx_dma_stream, ENABLE);
}
return (i);
}
size_t serial_send_buffered(struct serial *uart, const uint8_t* data, size_t num_bytes)
@ -341,7 +341,7 @@ size_t serial_send_buffered(struct serial *uart, const uint8_t* data, size_t num
}
if (!tx_buffer_empty(uart) && !(uart->tx_dma_stream->CR & DMA_SxCR_EN)) {
dma_tx(uart);
dma_tx(uart);
}
return bytes_written;
}
@ -354,47 +354,47 @@ size_t serial_send_blocking(struct serial *uart, const uint8_t *data, size_t num
while (USART_GetFlagStatus(uart->uart, USART_FLAG_TC) == RESET)
;
while (i > 0) {
USART_SendData(uart->uart, *data++);
while (i > 0) {
USART_SendData(uart->uart, *data++);
while (USART_GetFlagStatus(uart->uart, USART_FLAG_TC) == RESET);
--i;
}
return (num);
}
return (num);
}
static __attribute__((unused)) void __tx_dma_isr(struct serial *uart)
{
if (DMA_GetITStatus(uart->tx_dma_stream, uart->tx_dma_tcflag)) {
DMA_ClearITPendingBit(uart->tx_dma_stream, uart->tx_dma_tcflag);
if (!tx_buffer_empty(uart)) {
dma_tx(uart);
} else {
if (DMA_GetITStatus(uart->tx_dma_stream, uart->tx_dma_tcflag)) {
DMA_ClearITPendingBit(uart->tx_dma_stream, uart->tx_dma_tcflag);
if (!tx_buffer_empty(uart)) {
dma_tx(uart);
} else {
/* Disable the selected DMA Stream by clearing EN bit */
uart->tx_dma_stream->CR &= ~(uint32_t)DMA_SxCR_EN;
}
}
}
}
}
#if (TARGET_HAS_USART1)
void __attribute__((weak)) target_usart1_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_9;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_9;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_10;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_Pin = GPIO_Pin_10;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
@ -402,8 +402,8 @@ void __attribute__((weak)) target_usart1_setup()
static void usart1_setup(struct serial *uart)
{
RCC_APB2PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart1_setup();
RCC_APB2PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart1_setup();
}
static volatile uint8_t __usart1_rx_buffer[TARGET_USART1_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart1,\"aw\",%nobits@")));
@ -412,56 +412,56 @@ static volatile uint8_t __usart1_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial0 = {
.name = "USART1",
.setup = usart1_setup,
.setup = usart1_setup,
.uart = USART1,
.rcc_uart = RCC_APB2Periph_USART1,
.tx_irq = DMA2_Stream7_IRQn,
.uart = USART1,
.rcc_uart = RCC_APB2Periph_USART1,
.tx_irq = DMA2_Stream7_IRQn,
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA2,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA2_Stream2,
.tx_dma_stream = DMA2_Stream7,
.tx_dma_tcflag = DMA_IT_TCIF7,
.dma_rcc = RCC_AHB1Periph_DMA2,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA2_Stream2,
.tx_dma_stream = DMA2_Stream7,
.tx_dma_tcflag = DMA_IT_TCIF7,
.tx_dma_buffer = __usart1_tx_dma_buffer,
.tx_buffer_size = TARGET_USART1_TX_BUFFER_SIZE,
.tx_buffer = __usart1_tx_buffer,
.tx_buffer_size = TARGET_USART1_TX_BUFFER_SIZE,
.tx_buffer = __usart1_tx_buffer,
.rx_buffer_size = TARGET_USART1_RX_BUFFER_SIZE,
.rx_buffer = __usart1_rx_buffer,
.rx_buffer_size = TARGET_USART1_RX_BUFFER_SIZE,
.rx_buffer = __usart1_rx_buffer,
};
void DMA2_Stream7_IRQHandler(void)
{
__tx_dma_isr(&serial0);
__tx_dma_isr(&serial0);
}
#endif
#if (TARGET_HAS_USART2)
void __attribute__((weak)) target_usart2_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_2;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_2;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_3;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_Pin = GPIO_Pin_3;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
@ -469,8 +469,8 @@ void __attribute__((weak)) target_usart2_setup()
static void usart2_setup(struct serial *uart)
{
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart2_setup();
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart2_setup();
}
static volatile uint8_t __usart2_rx_buffer[TARGET_USART2_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart2,\"aw\",%nobits@")));
@ -479,56 +479,56 @@ static volatile uint8_t __usart2_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial1 = {
.name = "USART2",
.setup = usart2_setup,
.setup = usart2_setup,
.uart = USART2,
.rcc_uart = RCC_APB1Periph_USART2,
.tx_irq = DMA1_Stream6_IRQn,
.uart = USART2,
.rcc_uart = RCC_APB1Periph_USART2,
.tx_irq = DMA1_Stream6_IRQn,
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream5,
.tx_dma_stream = DMA1_Stream6,
.tx_dma_tcflag = DMA_IT_TCIF6,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream5,
.tx_dma_stream = DMA1_Stream6,
.tx_dma_tcflag = DMA_IT_TCIF6,
.tx_dma_buffer = __usart2_tx_dma_buffer,
.tx_buffer_size = TARGET_USART2_TX_BUFFER_SIZE,
.tx_buffer = __usart1_tx_buffer,
.tx_buffer_size = TARGET_USART2_TX_BUFFER_SIZE,
.tx_buffer = __usart1_tx_buffer,
.rx_buffer_size = TARGET_USART2_RX_BUFFER_SIZE,
.rx_buffer = __usart2_rx_buffer,
.rx_buffer_size = TARGET_USART2_RX_BUFFER_SIZE,
.rx_buffer = __usart2_rx_buffer,
};
void DMA1_Stream6_IRQHandler(void)
{
__tx_dma_isr(&serial1);
__tx_dma_isr(&serial1);
}
#endif
#if (TARGET_HAS_USART3)
void __attribute__((weak)) target_usart3_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_10;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_10;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &gpio_conf);
GPIO_Init(GPIOB, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_11;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_Pin = GPIO_Pin_11;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &gpio_conf);
GPIO_Init(GPIOB, &gpio_conf);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);
@ -536,8 +536,8 @@ void __attribute__((weak)) target_usart3_setup()
static void usart3_setup(struct serial *uart)
{
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart3_setup();
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart3_setup();
}
static volatile uint8_t __usart3_rx_buffer[TARGET_USART3_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart3,\"aw\",%nobits@")));
@ -546,55 +546,55 @@ static volatile uint8_t __usart3_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial2 = {
.name = "USART3",
.setup = usart3_setup,
.setup = usart3_setup,
.uart = USART3,
.rcc_uart = RCC_APB1Periph_USART3,
.tx_irq = DMA1_Stream3_IRQn,
.uart = USART3,
.rcc_uart = RCC_APB1Periph_USART3,
.tx_irq = DMA1_Stream3_IRQn,
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.rx_dma_channel = DMA_Channel_4,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream1,
.tx_dma_stream = DMA1_Stream3,
.tx_dma_tcflag = DMA_IT_TCIF3,
.dma_rcc = RCC_AHB1Periph_DMA1,
.rx_dma_channel = DMA_Channel_4,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream1,
.tx_dma_stream = DMA1_Stream3,
.tx_dma_tcflag = DMA_IT_TCIF3,
.tx_dma_buffer = __usart3_tx_dma_buffer,
.tx_buffer_size = TARGET_USART3_TX_BUFFER_SIZE,
.tx_buffer = __usart3_tx_buffer,
.tx_buffer_size = TARGET_USART3_TX_BUFFER_SIZE,
.tx_buffer = __usart3_tx_buffer,
.rx_buffer_size = TARGET_USART3_RX_BUFFER_SIZE,
.rx_buffer = __usart3_rx_buffer,
.rx_buffer_size = TARGET_USART3_RX_BUFFER_SIZE,
.rx_buffer = __usart3_rx_buffer,
};
void DMA1_Stream3_IRQHandler(void)
{
__tx_dma_isr(&serial2);
__tx_dma_isr(&serial2);
}
#endif
#if (TARGET_HAS_USART4)
void __attribute__((weak)) target_uart4_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_0;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_0;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_1;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_Pin = GPIO_Pin_1;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio_conf);
GPIO_Init(GPIOA, &gpio_conf);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_UART4);
@ -602,8 +602,8 @@ void __attribute__((weak)) target_uart4_setup()
static void usart4_setup(struct serial *uart)
{
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_uart4_setup();
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_uart4_setup();
}
static volatile uint8_t __usart4_rx_buffer[TARGET_USART4_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart4,\"aw\",%nobits@")));
@ -612,33 +612,33 @@ static volatile uint8_t __usart4_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial3 = {
.name = "UART4",
.setup = usart4_setup,
.setup = usart4_setup,
.uart = UART4,
.rcc_uart = RCC_APB1Periph_UART4,
.tx_irq = DMA1_Stream4_IRQn,
.uart = UART4,
.rcc_uart = RCC_APB1Periph_UART4,
.tx_irq = DMA1_Stream4_IRQn,
/* .eie_irq = UART4_IRQn, */
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream2,
.tx_dma_stream = DMA1_Stream4,
.tx_dma_tcflag = DMA_IT_TCIF4,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream2,
.tx_dma_stream = DMA1_Stream4,
.tx_dma_tcflag = DMA_IT_TCIF4,
.tx_dma_buffer = __usart4_tx_dma_buffer,
.tx_buffer_size = TARGET_USART4_TX_BUFFER_SIZE,
.tx_buffer = __usart4_tx_buffer,
.tx_buffer_size = TARGET_USART4_TX_BUFFER_SIZE,
.tx_buffer = __usart4_tx_buffer,
.rx_buffer_size = TARGET_USART4_RX_BUFFER_SIZE,
.rx_buffer = __usart4_rx_buffer,
.rx_buffer_size = TARGET_USART4_RX_BUFFER_SIZE,
.rx_buffer = __usart4_rx_buffer,
};
void DMA1_Stream4_IRQHandler(void)
{
__tx_dma_isr(&serial3);
__tx_dma_isr(&serial3);
}
void UART4_IRQHandler(void)
@ -651,26 +651,26 @@ void UART4_IRQHandler(void)
#if (TARGET_HAS_USART5)
void __attribute__((weak)) target_uart5_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_2;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_2;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &gpio_conf);
GPIO_Init(GPIOD, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_12;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_12;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &gpio_conf);
GPIO_Init(GPIOC, &gpio_conf);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
@ -678,8 +678,8 @@ void __attribute__((weak)) target_uart5_setup()
static void usart5_setup(struct serial *uart)
{
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_uart5_setup();
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_uart5_setup();
}
static volatile uint8_t __usart5_rx_buffer[TARGET_USART5_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart5,\"aw\",%nobits@")));
@ -688,33 +688,33 @@ static volatile uint8_t __usart5_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial4 = {
.name = "USART5",
.setup = usart5_setup,
.setup = usart5_setup,
.uart = UART5,
.rcc_uart = RCC_APB1Periph_UART5,
.tx_irq = DMA1_Stream7_IRQn,
/* .eie_irq = USART6_IRQn, */
.uart = UART5,
.rcc_uart = RCC_APB1Periph_UART5,
.tx_irq = DMA1_Stream7_IRQn,
/* .eie_irq = USART6_IRQn, */
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream0,
.tx_dma_stream = DMA1_Stream7,
.tx_dma_tcflag = DMA_IT_TCIF7,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_4,
.rx_dma_channel = DMA_Channel_4,
.rx_dma_stream = DMA1_Stream0,
.tx_dma_stream = DMA1_Stream7,
.tx_dma_tcflag = DMA_IT_TCIF7,
.tx_dma_buffer = __usart5_tx_dma_buffer,
.tx_buffer_size = TARGET_USART5_TX_BUFFER_SIZE,
.tx_buffer = __usart5_tx_buffer,
.tx_buffer_size = TARGET_USART5_TX_BUFFER_SIZE,
.tx_buffer = __usart5_tx_buffer,
.rx_buffer_size = TARGET_USART5_RX_BUFFER_SIZE,
.rx_buffer = __usart5_rx_buffer,
.rx_buffer_size = TARGET_USART5_RX_BUFFER_SIZE,
.rx_buffer = __usart5_rx_buffer,
};
void DMA1_Stream7_IRQHandler(void)
{
__tx_dma_isr(&serial4);
__tx_dma_isr(&serial4);
}
#endif
@ -722,24 +722,24 @@ void DMA1_Stream7_IRQHandler(void)
#if (TARGET_HAS_USART6)
void __attribute__((weak)) target_usart6_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_14;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_14;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &gpio_conf);
GPIO_Init(GPIOG, &gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_7;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_Pin = GPIO_Pin_7;
gpio_conf.GPIO_OType = GPIO_OType_OD;
gpio_conf.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &gpio_conf);
GPIO_Init(GPIOC, &gpio_conf);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource14, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
@ -747,8 +747,8 @@ void __attribute__((weak)) target_usart6_setup()
static void usart6_setup(struct serial *uart)
{
RCC_APB2PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart6_setup();
RCC_APB2PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart6_setup();
}
static volatile uint8_t __usart6_rx_buffer[TARGET_USART6_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart6,\"aw\",%nobits@")));
@ -757,33 +757,33 @@ static volatile uint8_t __usart6_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial5 = {
.name = "USART6",
.setup = usart6_setup,
.setup = usart6_setup,
.uart = USART6,
.rcc_uart = RCC_APB2Periph_USART6,
.tx_irq = DMA2_Stream6_IRQn,
/* .eie_irq = USART6_IRQn, */
.uart = USART6,
.rcc_uart = RCC_APB2Periph_USART6,
.tx_irq = DMA2_Stream6_IRQn,
/* .eie_irq = USART6_IRQn, */
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA2,
.tx_dma_channel = DMA_Channel_5,
.rx_dma_channel = DMA_Channel_5,
.rx_dma_stream = DMA2_Stream1,
.tx_dma_stream = DMA2_Stream6,
.tx_dma_tcflag = DMA_IT_TCIF6,
.dma_rcc = RCC_AHB1Periph_DMA2,
.tx_dma_channel = DMA_Channel_5,
.rx_dma_channel = DMA_Channel_5,
.rx_dma_stream = DMA2_Stream1,
.tx_dma_stream = DMA2_Stream6,
.tx_dma_tcflag = DMA_IT_TCIF6,
.tx_dma_buffer = __usart6_tx_dma_buffer,
.tx_buffer_size = TARGET_USART6_TX_BUFFER_SIZE,
.tx_buffer = __usart6_tx_buffer,
.tx_buffer_size = TARGET_USART6_TX_BUFFER_SIZE,
.tx_buffer = __usart6_tx_buffer,
.rx_buffer_size = TARGET_USART6_RX_BUFFER_SIZE,
.rx_buffer = __usart6_rx_buffer,
.rx_buffer_size = TARGET_USART6_RX_BUFFER_SIZE,
.rx_buffer = __usart6_rx_buffer,
};
void DMA2_Stream6_IRQHandler(void)
{
__tx_dma_isr(&serial5);
__tx_dma_isr(&serial5);
}
void USART6_IRQHandler(void)
@ -796,18 +796,18 @@ void USART6_IRQHandler(void)
#if (TARGET_HAS_USART7)
void __attribute__((weak)) target_usart7_setup()
{
GPIO_InitTypeDef gpio_conf;
GPIO_InitTypeDef gpio_conf;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_StructInit(&gpio_conf);
gpio_conf.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
gpio_conf.GPIO_OType = GPIO_OType_PP;
gpio_conf.GPIO_Mode = GPIO_Mode_AF;
gpio_conf.GPIO_Speed = GPIO_Speed_50MHz;
gpio_conf.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &gpio_conf);
GPIO_Init(GPIOF, &gpio_conf);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_UART7);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_UART7);
@ -815,8 +815,8 @@ void __attribute__((weak)) target_usart7_setup()
static void usart7_setup(struct serial *uart)
{
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart7_setup();
RCC_APB1PeriphClockCmd(uart->rcc_uart, ENABLE);
target_usart7_setup();
}
static volatile uint8_t __usart7_rx_buffer[TARGET_USART7_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart7,\"aw\",%nobits@")));
@ -825,33 +825,33 @@ static volatile uint8_t __usart7_tx_dma_buffer[TX_DMA_BUFFER_SIZE] __attri
struct serial serial6 = {
.name = "USART7",
.setup = usart7_setup,
.setup = usart7_setup,
.uart = UART7,
.rcc_uart = RCC_APB1Periph_UART7,
.tx_irq = DMA1_Stream1_IRQn,
/* .eie_irq = UART7_IRQn, */
.uart = UART7,
.rcc_uart = RCC_APB1Periph_UART7,
.tx_irq = DMA1_Stream1_IRQn,
/* .eie_irq = UART7_IRQn, */
.eie_irq = -1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_5,
.rx_dma_channel = DMA_Channel_5,
.rx_dma_stream = DMA1_Stream3,
.tx_dma_stream = DMA1_Stream1,
.tx_dma_tcflag = DMA_IT_TCIF1,
.dma_rcc = RCC_AHB1Periph_DMA1,
.tx_dma_channel = DMA_Channel_5,
.rx_dma_channel = DMA_Channel_5,
.rx_dma_stream = DMA1_Stream3,
.tx_dma_stream = DMA1_Stream1,
.tx_dma_tcflag = DMA_IT_TCIF1,
.tx_dma_buffer = __usart7_tx_dma_buffer,
.tx_buffer_size = TARGET_USART7_TX_BUFFER_SIZE,
.tx_buffer = __usart7_tx_buffer,
.tx_buffer_size = TARGET_USART7_TX_BUFFER_SIZE,
.tx_buffer = __usart7_tx_buffer,
.rx_buffer_size = TARGET_USART7_RX_BUFFER_SIZE,
.rx_buffer = __usart7_rx_buffer,
.rx_buffer_size = TARGET_USART7_RX_BUFFER_SIZE,
.rx_buffer = __usart7_rx_buffer,
};
void DMA1_Stream1_IRQHandler(void)
{
__tx_dma_isr(&serial6);
__tx_dma_isr(&serial6);
}
void UART7_IRQHandler(void)

6
lib/spi.c

@ -5,7 +5,7 @@
#include "spi.h"
struct spi_device {
SPI_TypeDef *base;
SPI_TypeDef *base;
int apb;
uint32_t rcc;
@ -167,7 +167,7 @@ uint8_t spi_txrx(spi_t spi, uint8_t data)
while (SPI_I2S_GetFlagStatus(spi->base, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(spi->base);
return SPI_I2S_ReceiveData(spi->base);
}
uint8_t spi_send_byte(spi_t spi, uint8_t data)
@ -216,7 +216,7 @@ void __attribute__((weak)) target_spi1_setup()
conf.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &conf);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, 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);
}

8
lib/tick.c

@ -41,15 +41,15 @@ void ticks_init(int ms)
if (ms > 0) {
__irq_per_ms = ms;
}
__current_mills = 0;
__current_mills = 0;
/* Set systick reload value to generate 1ms interrupt */
SysTick_Config(__irq_per_ms * SystemCoreClock / 1000);
}
void ticks_close()
{
SysTick->CTRL &= ~(SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk); /* Enable SysTick IRQ and SysTick Timer */
SysTick->CTRL &= ~(SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk); /* Enable SysTick IRQ and SysTick Timer */
}

100
lib/utils.c

@ -6,49 +6,49 @@
/* CRC-8 x8+x2+x+1 */
unsigned char crc8(const unsigned char *p, int n)
{
unsigned char crc8 = 0;
static unsigned char crc_array[256] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
};
unsigned char crc8 = 0;
static unsigned char crc_array[256] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5, 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85, 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2, 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C, 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C, 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B, 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
};
while (n > 0) {
crc8 = crc_array[crc8^*p]; /* lookup table */
p++;
--n;
}
return crc8;
while (n > 0) {
crc8 = crc_array[crc8^*p]; /* lookup table */
p++;
--n;
}
return crc8;
}
#else
/* CRC-8 x8+x2+x+1 */
unsigned char crc8(const unsigned char *data, int len)
{
unsigned crc = 0;
int i, j;
for (j = len; j; j--, data++) {
crc ^= (*data << 8);
for (i = 8; i; i--) {
if (crc & 0x8000)
crc ^= (0x1070 << 3);
crc <<= 1;
}
}
unsigned crc = 0;
int i, j;
for (j = len; j; j--, data++) {
crc ^= (*data << 8);
for (i = 8; i; i--) {
if (crc & 0x8000)
crc ^= (0x1070 << 3);
crc <<= 1;
}
}
return (unsigned char)(crc >> 8);
return (unsigned char)(crc >> 8);
}
#endif
@ -58,30 +58,30 @@ void __attribute__((weak)) target_close()
int go(unsigned long vect)
{
volatile unsigned int *ptr = (volatile unsigned int *)vect;
register unsigned int stack = ptr[0];
register unsigned int addr = ptr[1];
volatile unsigned int *ptr = (volatile unsigned int *)vect;
register unsigned int stack = ptr[0];
register unsigned int addr = ptr[1];
// printf("stack: %x, entry: %x, [%x, %x]\r\n", stack, addr, (stack & 0x2ffC0000), (stack & 0x1ffe0000));
// printf("off: %x\n", (vect - NVIC_VectTab_FLASH));
if (((stack & 0x2ffc0000) == 0x20000000) /* SRAM */ || ((stack & 0x1ffe0000) == 0x10000000) /* CCMRAM */) {
/* disable global irq */
__disable_irq();
// printf("stack: %x, entry: %x, [%x, %x]\r\n", stack, addr, (stack & 0x2ffC0000), (stack & 0x1ffe0000));
// printf("off: %x\n", (vect - NVIC_VectTab_FLASH));
if (((stack & 0x2ffc0000) == 0x20000000) /* SRAM */ || ((stack & 0x1ffe0000) == 0x10000000) /* CCMRAM */) {
target_close();
/* disable global irq */
__disable_irq();
NVIC_SetVectorTable(NVIC_VectTab_FLASH, (vect - NVIC_VectTab_FLASH));
target_close();
NVIC_SetVectorTable(NVIC_VectTab_FLASH, (vect - NVIC_VectTab_FLASH));
/* set the stack pointer, then run */
__asm__ __volatile__(
"msr msp, %[tos]\n\t"
"msr psp, %[tos]\n\t"
"msr psp, %[tos]\n\t"
"isb sy\n\t"
"dsb sy\n\t"
"blx %[app]\n\t" : : [tos] "r" (stack), [app] "r" (addr));
}
return -1;
}
return -1;
}
void delay_microseconds(unsigned int us)

346
lib/wm8978.c

@ -10,255 +10,255 @@
#define WADDR (0x34)
static union {
iic_t iic;
i2c_t i2c;
void *raw;
iic_t iic;
i2c_t i2c;
void *raw;
} _i2c;
static int (*_write)(uint8_t reg, uint8_t val);
static uint16_t __regs[58]= {
0X0000,0X0000,0X0000,0X0000,0X0050,0X0000,0X0140,0X0000,
0X0000,0X0000,0X0000,0X00FF,0X00FF,0X0000,0X0100,0X00FF,
0X00FF,0X0000,0X012C,0X002C,0X002C,0X002C,0X002C,0X0000,
0X0032,0X0000,0X0000,0X0000,0X0000,0X0000,0X0000,0X0000,
0X0038,0X000B,0X0032,0X0000,0X0008,0X000C,0X0093,0X00E9,
0X0000,0X0000,0X0000,0X0000,0X0003,0X0010,0X0010,0X0100,
0X0100,0X0002,0X0001,0X0001,0X0039,0X0039,0X0039,0X0039,
0X0001,0X0001
};
0X0000,0X0000,0X0000,0X0000,0X0050,0X0000,0X0140,0X0000,
0X0000,0X0000,0X0000,0X00FF,0X00FF,0X0000,0X0100,0X00FF,
0X00FF,0X0000,0X012C,0X002C,0X002C,0X002C,0X002C,0X0000,
0X0032,0X0000,0X0000,0X0000,0X0000,0X0000,0X0000,0X0000,
0X0038,0X000B,0X0032,0X0000,0X0008,0X000C,0X0093,0X00E9,
0X0000,0X0000,0X0000,0X0000,0X0003,0X0010,0X0010,0X0100,
0X0100,0X0002,0X0001,0X0001,0X0039,0X0039,0X0039,0X0039,
0X0001,0X0001
};
uint16_t wm8978_read(uint8_t reg)
{
return __regs[reg];
return __regs[reg];
}
static int _iic_write(uint8_t regr, uint8_t regv)
{
if (iic_start(_i2c.iic)) {
printf("start\r\n");
return -1;
}
if (iic_send_slave_address(_i2c.iic, WADDR)) {
printf("slave\r\n");
return -2;
}
if (iic_send_byte(_i2c.iic, regr)) {
printf("byte 1\r\n");
return -3;
}
if (iic_send_byte(_i2c.iic, regv)) {
printf("byte 2\r\n");
return -4;
}
iic_stop(_i2c.raw);
return 0;
if (iic_start(_i2c.iic)) {
printf("start\r\n");
return -1;
}
if (iic_send_slave_address(_i2c.iic, WADDR)) {
printf("slave\r\n");
return -2;
}
if (iic_send_byte(_i2c.iic, regr)) {
printf("byte 1\r\n");
return -3;
}
if (iic_send_byte(_i2c.iic, regv)) {
printf("byte 2\r\n");
return -4;
}
iic_stop(_i2c.raw);
return 0;
}
static int _i2c_write(uint8_t regr, uint8_t regv)
{
if (i2c_start(_i2c.i2c)) {
printf("start\r\n");
return -1;
}
if (i2c_start(_i2c.i2c)) {
printf("start\r\n");
return -1;
}
i2c_send_byte(_i2c.i2c, WADDR);
if (i2c_wait_ack(_i2c.i2c)) { /* NACK */
printf("address not response\r\n");
return -2;
}
i2c_send_byte(_i2c.i2c, WADDR);
if (i2c_wait_ack(_i2c.i2c)) { /* NACK */
printf("address not response\r\n");
return -2;
}
i2c_send_byte(_i2c.i2c, regr);
i2c_wait_ack(_i2c.i2c);
i2c_send_byte(_i2c.i2c, regr);
i2c_wait_ack(_i2c.i2c);
i2c_send_byte(_i2c.i2c, regv);
i2c_wait_ack(_i2c.i2c);
i2c_send_byte(_i2c.i2c, regv);
i2c_wait_ack(_i2c.i2c);
i2c_stop(_i2c.i2c);
i2c_stop(_i2c.i2c);
return 0;
return 0;
}
int wm8978_write(uint8_t reg, uint16_t val)
{
uint8_t regr, regv;
if (_write) {
regr = ((reg << 1) & 0xfe) | ((val >> 8) & 0x01);
regv = val & 0xff;
if (!_write(regr, regv)) {
__regs[reg] = val;
return 0;
}
}
return -1;
uint8_t regr, regv;
if (_write) {
regr = ((reg << 1) & 0xfe) | ((val >> 8) & 0x01);
regv = val & 0xff;
if (!_write(regr, regv)) {
__regs[reg] = val;
return 0;
}
}
return -1;
}
static void aux_gain(uint8_t gain)
{
uint16_t regval;
uint16_t regval;
gain &=0x07;
gain &=0x07;
regval = wm8978_read(47);
regval &= ~(7<<0);
wm8978_write(47,regval|gain<<0);
regval = wm8978_read(47);
regval &= ~(7<<0);
wm8978_write(47,regval|gain<<0);
regval = wm8978_read(48);
regval &=~(7<<0);
regval = wm8978_read(48);
regval &=~(7<<0);
wm8978_write(48, regval|gain<<0);
}
wm8978_write(48, regval|gain<<0);
}
static void linein_gain(uint8_t gain)
{
uint16_t regval;
uint16_t regval;
gain &= 0x07;
regval = wm8978_read(47);
gain &= 0x07;
regval = wm8978_read(47);
regval &=~(7<<4);
wm8978_write(47,regval|gain<<4);
regval &=~(7<<4);
wm8978_write(47,regval|gain<<4);
regval = wm8978_read(48);
regval&=~(7<<4);
wm8978_write(48,regval|gain<<4);
}
regval = wm8978_read(48);
regval&=~(7<<4);
wm8978_write(48,regval|gain<<4);
}
static void input_cfg(uint8_t micen,uint8_t lineinen,uint8_t auxen)
{
uint16_t regval;
regval = wm8978_read(2);
if(micen)
regval|=3<<2;
else
regval&=~(3<<2);
wm8978_write(2,regval);
regval = wm8978_read(44);
if(micen)
regval|=3<<4|3<<0;
else
regval&=~(3<<4|3<<0);
wm8978_write(44,regval);
if(lineinen)
linein_gain(5);
else
linein_gain(0);
if (auxen)
aux_gain(7);
else
aux_gain(0);
uint16_t regval;
regval = wm8978_read(2);
if(micen)
regval|=3<<2;
else
regval&=~(3<<2);
wm8978_write(2,regval);
regval = wm8978_read(44);
if(micen)
regval|=3<<4|3<<0;
else
regval&=~(3<<4|3<<0);
wm8978_write(44,regval);
if(lineinen)
linein_gain(5);
else
linein_gain(0);
if (auxen)
aux_gain(7);
else
aux_gain(0);
}
static void enable_ad_da(uint8_t dac, uint8_t adc)
{
uint16_t regv;
regv = wm8978_read(3);
if (dac) {
regv |= 3;
} else {
regv &= ~3;
}
wm8978_write(3, regv);
regv = wm8978_read(2);
if (adc) {
regv |= 3;
} else {
regv &= ~3;
}
wm8978_write(2, regv);
uint16_t regv;
regv = wm8978_read(3);
if (dac) {
regv |= 3;
} else {
regv &= ~3;
}
wm8978_write(3, regv);
regv = wm8978_read(2);
if (adc) {
regv |= 3;
} else {
regv &= ~3;
}
wm8978_write(2, regv);
}
static void output_cfg(uint8_t dacen,uint8_t bpsen)
{
uint16_t regval=0;
uint16_t regval=0;
if(dacen)
regval|=1<<0;
if(dacen)
regval|=1<<0;
if(bpsen) {
regval|=1<<1;
regval|=5<<2;
}
if(bpsen) {
regval|=1<<1;
regval|=5<<2;
}
wm8978_write(50, regval);
wm8978_write(51, regval);
wm8978_write(50, regval);
wm8978_write(51, regval);
}
static void hp_vol_set(uint8_t voll,uint8_t volr)
{
voll&=0x3F;
volr&=0x3F;
voll&=0x3F;
volr&=0x3F;
if(voll==0)voll|=1<<6;
if(volr==0)volr|=1<<6;
if(voll==0)voll|=1<<6;
if(volr==0)volr|=1<<6;
wm8978_write(52,voll);
wm8978_write(53,volr|(1<<8));
wm8978_write(52,voll);
wm8978_write(53,volr|(1<<8));
}
static void spk_vol_set(uint8_t volx)
{
volx &= 0x3F;
if(volx==0)volx|=1<<6;
volx &= 0x3F;
if(volx==0)volx|=1<<6;
wm8978_write(54, volx);
wm8978_write(55, volx|(1<<8));
wm8978_write(54, volx);
wm8978_write(55, volx|(1<<8));
}
static int do_init()
{
if (wm8978_write(0, 0)) {
return -1;
}
wm8978_write(1, 0x1b);
wm8978_write(2, 0x1b0);
wm8978_write(3, 0x6c);
wm8978_write(6, 0);
wm8978_write(43, 1 << 4);
wm8978_write(47, 1 << 8);
wm8978_write(48, 1 << 8);
wm8978_write(49, 1 << 1);
wm8978_write(10, 1 << 3);
wm8978_write(14, 1 << 3);
/* i2s cfg */
wm8978_write(4, (0x2 << 3) | (0x00 << 5));
/* ad da */
enable_ad_da(1, 0);
input_cfg(0, 0, 0);
output_cfg(1, 0);
hp_vol_set(63, 63);
spk_vol_set(63);
return 0;
if (wm8978_write(0, 0)) {
return -1;
}
wm8978_write(1, 0x1b);
wm8978_write(2, 0x1b0);
wm8978_write(3, 0x6c);
wm8978_write(6, 0);
wm8978_write(43, 1 << 4);
wm8978_write(47, 1 << 8);
wm8978_write(48, 1 << 8);
wm8978_write(49, 1 << 1);
wm8978_write(10, 1 << 3);
wm8978_write(14, 1 << 3);
/* i2s cfg */
wm8978_write(4, (0x2 << 3) | (0x00 << 5));
/* ad da */
enable_ad_da(1, 0);
input_cfg(0, 0, 0);
output_cfg(1, 0);
hp_vol_set(63, 63);
spk_vol_set(63);
return 0;
}
int wm8978_init_with_iic(iic_t i2c)
{
_i2c.iic = i2c;
_write = _iic_write;
_i2c.iic = i2c;
_write = _iic_write;
return do_init();
return do_init();
}
int wm8978_init_with_i2c(i2c_t i2c)
{
_i2c.i2c = i2c;
_write = _i2c_write;
_i2c.i2c = i2c;
_write = _i2c_write;
return do_init();
return do_init();
}

796
lib/ymodem.c

@ -53,49 +53,49 @@ static int put_byte (struct ymodem_xfer *uart, uint8_t c)
*/
static int do_recv(struct ymodem_xfer *uart, uint8_t *data, int *length, uint32_t timeout)
{
uint16_t i, packet_size;
uint8_t c;
*length = 0;
if (get_byte(uart, &c, timeout) != 0) {
return -1;
}
switch (c) {
case SOH:
packet_size = PACKET_SIZE;
break;
case STX:
packet_size = PACKET_1K_SIZE;
break;
case EOT:
return 0;
case CA:
if ((get_byte(uart, &c, timeout) == 0) && (c == CA)) {
*length = -1;
return 0;
} else {
return -1;
}
case ABORT1:
case ABORT2:
return 1;
default:
return -1;
}
*data = c;
for (i = 1; i < (packet_size + PACKET_OVERHEAD); i ++) {
if (get_byte(uart, data + i, timeout) != 0) {
return -1;
}
}
if (data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff)) {
return -1;
}
*length = packet_size;
return 0;
uint16_t i, packet_size;
uint8_t c;
*length = 0;
if (get_byte(uart, &c, timeout) != 0) {
return -1;
}
switch (c) {
case SOH:
packet_size = PACKET_SIZE;
break;
case STX:
packet_size = PACKET_1K_SIZE;
break;
case EOT:
return 0;
case CA:
if ((get_byte(uart, &c, timeout) == 0) && (c == CA)) {
*length = -1;
return 0;
} else {
return -1;
}
case ABORT1:
case ABORT2:
return 1;
default:
return -1;
}
*data = c;
for (i = 1; i < (packet_size + PACKET_OVERHEAD); i ++) {
if (get_byte(uart, data + i, timeout) != 0) {
return -1;
}
}
if (data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff)) {
return -1;
}
*length = packet_size;
return 0;
}
/*
@ -103,106 +103,106 @@ static int do_recv(struct ymodem_xfer *uart, uint8_t *data, int *length, uint32_
*/
int ymodem_recv(struct ymodem_xfer *uart)
{
char filename[FILE_NAME_LENGTH + 1], file_size[FILE_SIZE_LENGTH + 1];
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], *file_ptr;
int i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0;
session_done = 0;
session_begin = 0;
errors = 0;
while (session_done == 0) {
packets_received = 0;
file_done = 0;
while (file_done == 0) {
switch (do_recv(uart, packet_data, &packet_length, NAK_TIMEOUT)) {
case 0:
errors = 0;
switch (packet_length) {
/* Abort by sender */
case -1:
put_byte(uart, ACK);
return 0;
/* End of transmission */
case 0:
put_byte(uart, ACK);
file_done = 1;
break;
/* Normal packet */
default:
if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) {
put_byte(uart, NAK);
} else {
if (packets_received == 0) {
/* Filename packet */
if (packet_data[PACKET_HEADER] != 0) {
/* Filename packet has valid data */
for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) {
filename[i++] = *file_ptr++;
}
filename[i++] = '\0';
for (i = 0, file_ptr++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);) {
file_size[i++] = *file_ptr++;
}
file_size[i++] = '\0';
size = strtoul(file_size, NULL, 0);
/* Test the size of the image to be sent */
/* Image size is greater than Flash size */
if (uart->on_info && uart->on_info(uart, filename, size)) { /* error */
/* End session */
put_byte(uart, CA);
put_byte(uart, CA);
return -1;
}
put_byte(uart, ACK);
put_byte(uart, CRC16);
} else {/* Filename packet is empty, end session */
put_byte(uart, ACK);
file_done = 1;
session_done = 1;
break;
}
} else { /* Data packet */
/* Write received data in Flash */
if (uart->on_data) {
if (uart->on_data(uart, packet_data + PACKET_HEADER, packet_length)) { /* An error occurred while writing to Flash memory */
/* End session */
put_byte(uart, CA);
put_byte(uart, CA);
return -2;
} else {
put_byte(uart, ACK);
}
} else {
put_byte(uart, CA);
put_byte(uart, CA);
}
}
packets_received++;
session_begin = 1;
}
}
break;
case 1:
put_byte(uart, CA);
put_byte(uart, CA);
return -3;
default:
if (session_begin > 0) {
errors ++;
}
if (errors > MAX_ERRORS) {
put_byte(uart, CA);
put_byte(uart, CA);
return 0;
}
put_byte(uart, CRC16);
break;
}
}
}
return size;
char filename[FILE_NAME_LENGTH + 1], file_size[FILE_SIZE_LENGTH + 1];
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], *file_ptr;
int i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0;
session_done = 0;
session_begin = 0;
errors = 0;
while (session_done == 0) {
packets_received = 0;
file_done = 0;
while (file_done == 0) {
switch (do_recv(uart, packet_data, &packet_length, NAK_TIMEOUT)) {
case 0:
errors = 0;
switch (packet_length) {
/* Abort by sender */
case -1:
put_byte(uart, ACK);
return 0;
/* End of transmission */
case 0:
put_byte(uart, ACK);
file_done = 1;
break;
/* Normal packet */
default:
if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) {
put_byte(uart, NAK);
} else {
if (packets_received == 0) {
/* Filename packet */
if (packet_data[PACKET_HEADER] != 0) {
/* Filename packet has valid data */
for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) {
filename[i++] = *file_ptr++;
}
filename[i++] = '\0';
for (i = 0, file_ptr++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);) {
file_size[i++] = *file_ptr++;
}
file_size[i++] = '\0';
size = strtoul(file_size, NULL, 0);
/* Test the size of the image to be sent */
/* Image size is greater than Flash size */
if (uart->on_info && uart->on_info(uart, filename, size)) { /* error */
/* End session */
put_byte(uart, CA);
put_byte(uart, CA);
return -1;
}
put_byte(uart, ACK);
put_byte(uart, CRC16);
} else {/* Filename packet is empty, end session */
put_byte(uart, ACK);
file_done = 1;
session_done = 1;
break;
}
} else { /* Data packet */
/* Write received data in Flash */
if (uart->on_data) {
if (uart->on_data(uart, packet_data + PACKET_HEADER, packet_length)) { /* An error occurred while writing to Flash memory */
/* End session */
put_byte(uart, CA);
put_byte(uart, CA);
return -2;
} else {
put_byte(uart, ACK);
}
} else {
put_byte(uart, CA);
put_byte(uart, CA);
}
}
packets_received++;
session_begin = 1;
}
}
break;
case 1:
put_byte(uart, CA);
put_byte(uart, CA);
return -3;
default:
if (session_begin > 0) {
errors ++;
}
if (errors > MAX_ERRORS) {
put_byte(uart, CA);
put_byte(uart, CA);
return 0;
}
put_byte(uart, CRC16);
break;
}
}
}
return size;
}
/**
@ -213,29 +213,29 @@ int ymodem_recv(struct ymodem_xfer *uart)
*/
static void init_packet(uint8_t *data, const uint8_t* fileName, uint32_t *length)
{
uint16_t i, j;
uint8_t file_ptr[10];
uint16_t i, j;
uint8_t file_ptr[10];
/* Make first three packet */
data[0] = SOH;
data[1] = 0x00;
data[2] = 0xff;
/* Make first three packet */
data[0] = SOH;
data[1] = 0x00;
data[2] = 0xff;
/* Filename packet has valid data */
for (i = 0; (fileName[i] != '\0') && (i < FILE_NAME_LENGTH);i++) {
data[i + PACKET_HEADER] = fileName[i];
}
/* Filename packet has valid data */
for (i = 0; (fileName[i] != '\0') && (i < FILE_NAME_LENGTH);i++) {
data[i + PACKET_HEADER] = fileName[i];
}
data[i + PACKET_HEADER] = 0x00;
data[i + PACKET_HEADER] = 0x00;
snprintf((char *)file_ptr, sizeof file_ptr, "%lu", *length);
for (j =0, i = i + PACKET_HEADER + 1; file_ptr[j] != '\0' ; ) {
data[i++] = file_ptr[j++];
}
snprintf((char *)file_ptr, sizeof file_ptr, "%lu", *length);
for (j =0, i = i + PACKET_HEADER + 1; file_ptr[j] != '\0' ; ) {
data[i++] = file_ptr[j++];
}
for (j = i; j < PACKET_SIZE + PACKET_HEADER; j++) {
data[j] = 0;
}
for (j = i; j < PACKET_SIZE + PACKET_HEADER; j++) {
data[j] = 0;
}
}
/**
@ -246,55 +246,55 @@ static void init_packet(uint8_t *data, const uint8_t* fileName, uint32_t *length
*/
static void prepare_packet(uint8_t *sourceBuf, uint8_t *data, uint8_t pktNo, uint32_t sizeBlk)
{
uint16_t i, size, packetSize;
uint8_t* filePtr;
/* Make first three packet */
packetSize = sizeBlk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
size = sizeBlk < packetSize ? sizeBlk :packetSize;
if (packetSize == PACKET_1K_SIZE)
{
data[0] = STX;
}
else
{
data[0] = SOH;
}
data[1] = pktNo;
data[2] = (~pktNo);
filePtr = sourceBuf;
/* Filename packet has valid data */
for (i = PACKET_HEADER; i < size + PACKET_HEADER;i++) {
data[i] = *filePtr++;
}
if ( size <= packetSize) {
for (i = size + PACKET_HEADER; i < packetSize + PACKET_HEADER; i++) {
data[i] = 0x1A; /* EOF (0x1A) or 0x00 */
}
}
uint16_t i, size, packetSize;
uint8_t* filePtr;
/* Make first three packet */
packetSize = sizeBlk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE;
size = sizeBlk < packetSize ? sizeBlk :packetSize;
if (packetSize == PACKET_1K_SIZE)
{
data[0] = STX;
}
else
{
data[0] = SOH;
}
data[1] = pktNo;
data[2] = (~pktNo);
filePtr = sourceBuf;
/* Filename packet has valid data */
for (i = PACKET_HEADER; i < size + PACKET_HEADER;i++) {
data[i] = *filePtr++;
}
if ( size <= packetSize) {
for (i = size + PACKET_HEADER; i < packetSize + PACKET_HEADER; i++) {
data[i] = 0x1A; /* EOF (0x1A) or 0x00 */
}
}
}
/**
* @brief Update CRC16 for input byte
* @param CRC input value
* @param CRC input value
* @param input byte
* @retval None
*/
static uint16_t crc16(uint16_t crcIn, uint8_t byte)
{
uint32_t crc = crcIn;
uint32_t in = byte | 0x100;
do {
crc <<= 1;
in <<= 1;
if(in & 0x100)
++crc;
if(crc & 0x10000)
crc ^= 0x1021;
} while (!(in & 0x10000));
return crc & 0xffffu;
uint32_t crc = crcIn;
uint32_t in = byte | 0x100;
do {
crc <<= 1;
in <<= 1;
if(in & 0x100)
++crc;
if(crc & 0x10000)
crc ^= 0x1021;
} while (!(in & 0x10000));
return crc & 0xffffu;
}
@ -306,16 +306,16 @@ static uint16_t crc16(uint16_t crcIn, uint8_t byte)
*/
static uint16_t do_crc16(const uint8_t* data, uint32_t size)
{
uint32_t crc = 0;
const uint8_t* dataEnd = data+size;
uint32_t crc = 0;
const uint8_t* dataEnd = data+size;
while(data < dataEnd)
crc = crc16(crc, *data++);
while(data < dataEnd)
crc = crc16(crc, *data++);
crc = crc16(crc, 0);
crc = crc16(crc, 0);
crc = crc16(crc, 0);
crc = crc16(crc, 0);
return crc&0xffffu;
return crc&0xffffu;
}
/**
@ -326,13 +326,13 @@ static uint16_t do_crc16(const uint8_t* data, uint32_t size)
*/
static uint8_t CalChecksum(const uint8_t* data, uint32_t size)
{
uint32_t sum = 0;
const uint8_t* dataEnd = data+size;
uint32_t sum = 0;
const uint8_t* dataEnd = data+size;
while(data < dataEnd )
sum += *data++;
while(data < dataEnd )
sum += *data++;
return (sum & 0xffu);
return (sum & 0xffu);
}
/**
@ -343,12 +343,12 @@ static uint8_t CalChecksum(const uint8_t* data, uint32_t size)
*/
static void send_packet(struct ymodem_xfer *uart, uint8_t *data, uint16_t length)
{
uint16_t i;
i = 0;
while (i < length) {
put_byte(uart, data[i]);
i++;
}
uint16_t i;
i = 0;
while (i < length) {
put_byte(uart, data[i]);
i++;
}
}
/**
@ -358,186 +358,182 @@ static void send_packet(struct ymodem_xfer *uart, uint8_t *data, uint16_t length
*/
int ymodem_send(struct ymodem_xfer *uart, uint8_t *buf, const uint8_t* sendFileName, uint32_t sizeFile)
{
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD];
uint8_t filename[FILE_NAME_LENGTH];
uint8_t *buf_ptr, tempCheckSum;
uint16_t tempCRC;
uint16_t blkNumber;
uint8_t receivedC[2], CRC16_F = 0, i;
uint32_t errors, ackReceived, size = 0, pktSize;
errors = 0;
ackReceived = 0;
for (i = 0; i < (FILE_NAME_LENGTH - 1); i++) {
filename[i] = sendFileName[i];
}
CRC16_F = 1;
/* Prepare first block */
init_packet(&packet_data[0], filename, &sizeFile);
do {
/* Send Packet */
send_packet(uart, packet_data, PACKET_SIZE + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
if (CRC16_F) {
tempCRC = do_crc16(&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
} else {
tempCheckSum = CalChecksum (&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCheckSum);
}
/* Wait for Ack and 'C' */
if (get_byte(uart, &receivedC[0], 10000) == 0) {
if (receivedC[0] == ACK) {
/* Packet transferred correctly */
ackReceived = 1;
}
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
buf_ptr = buf;
size = sizeFile;
blkNumber = 0x01;
/* Here 1024 bytes package is used to send the packets */
/* Resend packet if NAK for a count of 10 else end of communication */
while (size) {
/* Prepare next packet */
prepare_packet(buf_ptr, &packet_data[0], blkNumber, size);
ackReceived = 0;
receivedC[0]= 0;
errors = 0;
do {
/* Send next packet */
if (size >= PACKET_1K_SIZE) {
pktSize = PACKET_1K_SIZE;
} else {
pktSize = PACKET_SIZE;
}
send_packet(uart, packet_data, pktSize + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
/* Send CRC or Check Sum based on CRC16_F */
if (CRC16_F) {
tempCRC = do_crc16(&packet_data[3], pktSize);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
} else {
tempCheckSum = CalChecksum (&packet_data[3], pktSize);
put_byte(uart, tempCheckSum);
}
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 100000) == 0) && (receivedC[0] == ACK)) {
ackReceived = 1;
if (size > pktSize) {
buf_ptr += pktSize;
size -= pktSize;
if (blkNumber > ((sizeFile / 1024) + 1)) { /* XXX */
return 0xFF; /* error */
} else {
blkNumber++;
}
} else {
buf_ptr += pktSize;
size = 0;
}
} else {
errors++;
}
} while(!ackReceived && (errors < 0x0A));
/* Resend packet if NAK for a count of 10 else end of communication */
if (errors >= 0x0A) {
return errors;
}
}
ackReceived = 0;
receivedC[0] = 0x00;
errors = 0;
do {
put_byte(uart, EOT);
/* Send (EOT); */
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 10000) == 0) && receivedC[0] == ACK) {
ackReceived = 1;
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
/* Last packet preparation */
ackReceived = 0;
receivedC[0] = 0x00;
errors = 0;
packet_data[0] = SOH;
packet_data[1] = 0;
packet_data [2] = 0xFF;
for (i = PACKET_HEADER; i < (PACKET_SIZE + PACKET_HEADER); i++) {
packet_data [i] = 0x00;
}
do {
/* Send Packet */
send_packet(uart, packet_data, PACKET_SIZE + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
tempCRC = do_crc16(&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
/* Wait for Ack and 'C' */
if (get_byte(uart, &receivedC[0], 10000) == 0)
{
if (receivedC[0] == ACK)
{
/* Packet transferred correctly */
ackReceived = 1;
}
}
else
{
errors++;
}
} while (!ackReceived && (errors < 0x0A));
/* Resend packet if NAK for a count of 10 else end of communication */
if (errors >= 0x0A) {
return errors;
}
do {
put_byte(uart, EOT);
/* Send (EOT); */
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 10000) == 0) && receivedC[0] == ACK) {
ackReceived = 1;
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
return 0; /* file transmitted successfully */
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD];
uint8_t filename[FILE_NAME_LENGTH];
uint8_t *buf_ptr, tempCheckSum;
uint16_t tempCRC;
uint16_t blkNumber;
uint8_t receivedC[2], CRC16_F = 0, i;
uint32_t errors, ackReceived, size = 0, pktSize;
errors = 0;
ackReceived = 0;
for (i = 0; i < (FILE_NAME_LENGTH - 1); i++) {
filename[i] = sendFileName[i];
}
CRC16_F = 1;
/* Prepare first block */
init_packet(&packet_data[0], filename, &sizeFile);
do {
/* Send Packet */
send_packet(uart, packet_data, PACKET_SIZE + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
if (CRC16_F) {
tempCRC = do_crc16(&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
} else {
tempCheckSum = CalChecksum (&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCheckSum);
}
/* Wait for Ack and 'C' */
if (get_byte(uart, &receivedC[0], 10000) == 0) {
if (receivedC[0] == ACK) {
/* Packet transferred correctly */
ackReceived = 1;
}
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
buf_ptr = buf;
size = sizeFile;
blkNumber = 0x01;
/* Here 1024 bytes package is used to send the packets */
/* Resend packet if NAK for a count of 10 else end of communication */
while (size) {
/* Prepare next packet */
prepare_packet(buf_ptr, &packet_data[0], blkNumber, size);
ackReceived = 0;
receivedC[0]= 0;
errors = 0;
do {
/* Send next packet */
if (size >= PACKET_1K_SIZE) {
pktSize = PACKET_1K_SIZE;
} else {
pktSize = PACKET_SIZE;
}
send_packet(uart, packet_data, pktSize + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
/* Send CRC or Check Sum based on CRC16_F */
if (CRC16_F) {
tempCRC = do_crc16(&packet_data[3], pktSize);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
} else {
tempCheckSum = CalChecksum (&packet_data[3], pktSize);
put_byte(uart, tempCheckSum);
}
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 100000) == 0) && (receivedC[0] == ACK)) {
ackReceived = 1;
if (size > pktSize) {
buf_ptr += pktSize;
size -= pktSize;
if (blkNumber > ((sizeFile / 1024) + 1)) { /* XXX */
return 0xFF; /* error */
} else {
blkNumber++;
}
} else {
buf_ptr += pktSize;
size = 0;
}
} else {
errors++;
}
} while(!ackReceived && (errors < 0x0A));
/* Resend packet if NAK for a count of 10 else end of communication */
if (errors >= 0x0A) {
return errors;
}
}
ackReceived = 0;
receivedC[0] = 0x00;
errors = 0;
do {
put_byte(uart, EOT);
/* Send (EOT); */
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 10000) == 0) && receivedC[0] == ACK) {
ackReceived = 1;
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
/* Last packet preparation */
ackReceived = 0;
receivedC[0] = 0x00;
errors = 0;
packet_data[0] = SOH;
packet_data[1] = 0;
packet_data [2] = 0xFF;
for (i = PACKET_HEADER; i < (PACKET_SIZE + PACKET_HEADER); i++) {
packet_data [i] = 0x00;
}
do {
/* Send Packet */
send_packet(uart, packet_data, PACKET_SIZE + PACKET_HEADER);
/* Send CRC or Check Sum based on CRC16_F */
tempCRC = do_crc16(&packet_data[3], PACKET_SIZE);
put_byte(uart, tempCRC >> 8);
put_byte(uart, tempCRC & 0xFF);
/* Wait for Ack and 'C' */
if (get_byte(uart, &receivedC[0], 10000) == 0) {
if (receivedC[0] == ACK) {
/* Packet transferred correctly */
ackReceived = 1;
}
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
/* Resend packet if NAK for a count of 10 else end of communication */
if (errors >= 0x0A) {
return errors;
}
do {
put_byte(uart, EOT);
/* Send (EOT); */
/* Wait for Ack */
if ((get_byte(uart, &receivedC[0], 10000) == 0) && receivedC[0] == ACK) {
ackReceived = 1;
} else {
errors++;
}
} while (!ackReceived && (errors < 0x0A));
if (errors >= 0x0A) {
return errors;
}
return 0; /* file transmitted successfully */
}

Loading…
Cancel
Save