Skip to content

Commit

Permalink
Main change: different baudrates are working now.
Browse files Browse the repository at this point in the history
Reading data is stable.
Different small changes and improvements.
  • Loading branch information
Polarisru committed Oct 20, 2018
1 parent 3a1838d commit 078ed4a
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 177 deletions.
74 changes: 49 additions & 25 deletions com.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>

#ifdef __MINGW32__
static HANDLE hSerial;
Expand All @@ -20,22 +21,22 @@ static int fd;
#endif

static uint32_t COM_Baudrate = 115200;
//static uint16_t COM_Bytes;

/** \brief Open COM port with settings
*
* \param [in] port
* \param [in] baudrate
* \param [in] have_parity
* \param [in] two_stopbits
* \param [in] port Port name as string
* \param [in] baudrate Port baudrate
* \param [in] have_parity true if parity should be switched on
* \param [in] two_stopbits true if 2 stop bits used
* \return true if succeed
*
*/
bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits)
{
printf("Opening %s at %u baud\n", port, baudrate);
#ifdef __MINGW32__
char str[64];
char str[64];
uint8_t multiplier;

sprintf(str, "\\\\.\\%s", port);
hSerial = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0,
Expand All @@ -58,14 +59,15 @@ bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
SetCommState(hSerial, &dcbSerialParams);
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout = 0xFFFFFFFF;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 50;
multiplier = (uint8_t)ceil((float)100000 / baudrate);
timeouts.ReadIntervalTimeout = 10 * multiplier;//0xFFFFFFFF;
timeouts.ReadTotalTimeoutMultiplier = 1 * multiplier;
timeouts.ReadTotalTimeoutConstant = 100 * multiplier;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
SetCommTimeouts(hSerial, &timeouts);
COM_Baudrate = baudrate;
COM_Bytes = 0;
//COM_Bytes = 0;
#endif

#ifdef __linux
Expand All @@ -75,8 +77,30 @@ bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
/* Setting the Baud rate */
cfsetispeed(&SerialPortSettings, B115200); /* Set Read Speed as 9600 */
cfsetospeed(&SerialPortSettings, B115200); /* Set Write Speed as 9600 */
switch (baudrate)
{
case 300:
cfsetispeed(&SerialPortSettings, B300);
cfsetospeed(&SerialPortSettings, B300);
break;
case 9600:
cfsetispeed(&SerialPortSettings, B9600);
cfsetospeed(&SerialPortSettings, B9600);
break;
case 38400:
cfsetispeed(&SerialPortSettings, B38400);
cfsetospeed(&SerialPortSettings, B38400);
break;
case 57600:
cfsetispeed(&SerialPortSettings, B57600);
cfsetospeed(&SerialPortSettings, B57600);
break;
default:
case 115200:
cfsetispeed(&SerialPortSettings, B115200);
cfsetospeed(&SerialPortSettings, B115200);
break;
}
if (have_parity == true)
SerialPortSettings.c_cflag |= PARENB; /* Enables the Parity Enable bit(PARENB) */
else
Expand All @@ -88,9 +112,9 @@ bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */
//SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_cflag |= (CREAD | CLOCAL); /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */
SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* Non Cannonical mode */
SerialPortSettings.c_oflag = 0; // no remapping, no delays
SerialPortSettings.c_cc[VMIN] = 0; // read doesn't block
SerialPortSettings.c_cc[VTIME] = 5; // 0.1 seconds read timeout
Expand All @@ -103,18 +127,18 @@ bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits

/** \brief Write data to COM port
*
* \param [in] data
* \param [in] len
* \param [in] data Data buffer for writing
* \param [in] len Length of data buffer
* \return 0 if everything Ok
*
*/
int COM_Write(uint8_t *data, uint16_t len)
{
#ifdef __MINGW32__
DWORD dwBytesWritten = 0;
DWORD signal;
//DWORD signal;
//OVERLAPPED ov = { 0 };
int res;
//int res;
//ov.hEvent = CreateEvent(NULL, true, true, NULL);

if (!WriteFile(hSerial, data, len, &dwBytesWritten, NULL))
Expand All @@ -140,18 +164,18 @@ int COM_Write(uint8_t *data, uint16_t len)

/** \brief Read data from COM port
*
* \param [out] data
* \param [in] len
* \param [out] data Data buffer to read data in
* \param [in] len Length of data to read
* \return number of received bytes as int
*
*/
int COM_Read(uint8_t *data, uint16_t len)
{
#ifdef __MINGW32__
//OVERLAPPED ov = { 0 };
COMSTAT status;
DWORD errors;
DWORD mask, btr, temp, signal;
//COMSTAT status;
//DWORD errors;
//DWORD mask, btr, temp, signal;
DWORD dwBytesRead = 0;
// ClearCommError(hSerial, &errors, &status);
// if (!ReadFile(hSerial, data, len, &dwBytesRead, &ov))
Expand Down Expand Up @@ -184,7 +208,7 @@ int COM_Read(uint8_t *data, uint16_t len)

/** \brief Calculate time for transmission with current baudrate
*
* \param
* \param [in] len Length of transmitted data
* \return time in milliseconds as uint16_t
*
*/
Expand All @@ -206,7 +230,7 @@ void COM_WaitForTransmit(void)

/** \brief Close current COM port
*
* \return
* \return Nothing
*
*/
void COM_Close(void)
Expand Down
28 changes: 14 additions & 14 deletions devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
tDevice DEVICES_List[] =
{
{
"tiny1617",
"tiny161x",
0x8000,
16 * 1024,
64,
Expand All @@ -16,7 +16,7 @@ tDevice DEVICES_List[] =
9
},
{
"tiny817",
"tiny81x",
0x8000,
8 * 1024,
64,
Expand All @@ -28,7 +28,7 @@ tDevice DEVICES_List[] =
9
},
{
"tiny417",
"tiny41x",
0x8000,
4 * 1024,
64,
Expand All @@ -40,7 +40,7 @@ tDevice DEVICES_List[] =
9
},
{
"tiny214",
"tiny21x",
0x8000,
2 * 1024,
64,
Expand All @@ -57,8 +57,8 @@ static int8_t DEVICE_Id = DEVICE_UNKNOWN_ID;

/** \brief Get device ID from name string
*
* \param [in] name
* \return
* \param [in] name Name to find as string
* \return index of the found device or -1 as error
*
*/
int8_t DEVICES_GetId(char *name)
Expand All @@ -80,7 +80,7 @@ int8_t DEVICES_GetId(char *name)

/** \brief Get flash memory length for selected device
*
* \return
* \return Size of the flash memory as uint16_t
*
*/
uint16_t DEVICES_GetFlashLength(void)
Expand All @@ -93,7 +93,7 @@ uint16_t DEVICES_GetFlashLength(void)

/** \brief Get flash start address for selected device
*
* \return
* \return Address of the flash area as uint16_t
*
*/
uint16_t DEVICES_GetFlashStart(void)
Expand All @@ -106,7 +106,7 @@ uint16_t DEVICES_GetFlashStart(void)

/** \brief Get flash page size for selected device
*
* \return
* \return Flash page size as uint16_t
*
*/
uint16_t DEVICES_GetPageSize(void)
Expand All @@ -119,7 +119,7 @@ uint16_t DEVICES_GetPageSize(void)

/** \brief Get NVM control registers address for selected device
*
* \return
* \return NVM registers adress as uint16_t
*
*/
uint16_t DEVICES_GetNvmctrlAddress(void)
Expand All @@ -132,7 +132,7 @@ uint16_t DEVICES_GetNvmctrlAddress(void)

/** \brief Get fuses address for selected device
*
* \return
* \return Fuses address as uint16_t
*
*/
uint16_t DEVICES_GetFusesAddress(void)
Expand All @@ -145,7 +145,7 @@ uint16_t DEVICES_GetFusesAddress(void)

/** \brief Get number of the fuses for selected device
*
* \return
* \return Number of the fuses as uint8_t
*
*/
uint8_t DEVICES_GetFusesNumber(void)
Expand All @@ -158,7 +158,7 @@ uint8_t DEVICES_GetFusesNumber(void)

/** \brief Get number of devices in the list
*
* \return
* \return Number of the devices as uint8_t
*
*/
uint8_t DEVICES_GetNumber(void)
Expand All @@ -168,7 +168,7 @@ uint8_t DEVICES_GetNumber(void)

/** \brief Get string name of the device by ID
*
* \return
* \return Device name as string
*
*/
char *DEVICES_GetNameByNumber(uint8_t number)
Expand Down
34 changes: 17 additions & 17 deletions ihex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ static uint8_t crc;

/** \brief Convert byte to string with HEX representation
*
* \param [in] byte
* \param [in] byte One byte of data
* \return HEX representation as string
*
*/
Expand All @@ -26,8 +26,8 @@ static char* IHEX_AddByte(uint8_t byte)

/** \brief Write Intel HEX ending to a file
*
* \param [in] fp
* \return
* \param [in] fp File handle
* \return true if succeed
*
*/
bool IHEX_WriteEnd(FILE *fp)
Expand All @@ -37,12 +37,12 @@ bool IHEX_WriteEnd(FILE *fp)
return true;
}

/** \brief
/** \brief Write data buffer to HEX file
*
* \param [in] fp
* \param [in] data
* \param [in] len
* \return
* \param [in] fp File handle
* \param [in] data Data buffer to write
* \param [in] len Length of data buffer
* \return error code as uint8_t
*
*/
uint8_t IHEX_WriteFile(FILE *fp, uint8_t *data, uint16_t len)
Expand Down Expand Up @@ -84,8 +84,8 @@ uint8_t IHEX_WriteFile(FILE *fp, uint8_t *data, uint16_t len)

/** \brief Get one nibble from char
*
* \param [in] c
* \return
* \param [in] c Char value
* \return data nibble as uint8_t
*
*/
uint8_t IHEX_GetNibble(char c)
Expand All @@ -102,8 +102,8 @@ uint8_t IHEX_GetNibble(char c)

/** \brief Get full byte from two chars
*
* \param [in] data
* \return
* \param [in] data Two chars as HEX representation
* \return byte value as uint8_t
*
*/
uint8_t IHEX_GetByte(char *data)
Expand All @@ -115,11 +115,11 @@ uint8_t IHEX_GetByte(char *data)

/** \brief Read Intel HEX file to a binary memory buffer
*
* \param [in] fp
* \param [out] data
* \param [in] maxlen
* \param [out] max_addr
* \return
* \param [in] fp File handler
* \param [out] data Data buffer to read data into
* \param [in] maxlen Maximal data length
* \param [out] max_addr Maximal address with non-empty data
* \return error code as uint8_t
*
*/
uint8_t IHEX_ReadFile(FILE *fp, uint8_t *data, uint16_t maxlen, uint16_t *max_addr)
Expand Down
6 changes: 3 additions & 3 deletions link.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ bool LINK_st16(uint16_t address, uint16_t value)

/** \brief
*
* \param
* \param
* \return
* \param [out] data Data buffer to write received data in
* \param [in] size Size of received data
* \return true if succeed
*
*/
bool LINK_ld_ptr_inc(uint8_t *data, uint8_t size)
Expand Down
Loading

0 comments on commit 078ed4a

Please sign in to comment.