<commit_before>
#include <string.h>

#include "LPC8xx.h"			/* LPC8xx Peripheral Registers */

#include "config.h"
#include "myuart.h"

#include "params.h"
extern params_union_type params_union;

#ifdef FEATURE_GPS_ON_USART1
//volatile uint32_t nmea_line = 0;
volatile uint32_t gps_last_position_t;
volatile uint32_t nmea_buf_index = 0;
//volatile uint32_t nmea_flags = 0;
volatile uint8_t nmea_words[24];
volatile int32_t nmea_word_count=0;
volatile uint8_t gps_latitude[12];
volatile uint8_t gps_longitude[12];
volatile uint8_t gps_time_of_day[12];
volatile uint8_t gps_hdop[8];
volatile uint8_t gps_fix[2];
volatile uint8_t gps_heading[8];
volatile uint8_t gps_speed[8];
volatile uint8_t nmea_buf[GPS_NMEA_SIZE];
#endif



#ifdef FEATURE_GPS_ON_USART1

/**
 * Copy a data field from NMEA sentence to address dst. Like strcpy()
 * except terminating on comma ','.
 */
static void copy_nmea_word (uint8_t *dst, uint8_t *src) {
	while (*src != ',') {
		*dst = *src;
		dst++;
		src++;
	}
	// Terminate with 0
	*dst = 0;
}

/**
 * Get length of a NMEA sentence word
 */
static int nmea_word_len (int word_index) {
	return nmea_words[word_index+1] - nmea_words[word_index] - 1;
}

/**
 * Second UART (USART1) interrupt handler to handle incoming NMEA sentences
 * from GPS receiver. (Optional feature).
 */
void UART1_IRQHandler(void)
{
	uint32_t uart_status = LPC_USART1->STAT;

	// UM10601 §15.6.3, Table 162, p181. USART Status Register.
	// Bit 0 RXRDY: 1 = data is available to be read from RXDATA
	// Bit 2 TXRDY: 1 = data may be written to TXDATA
	if (uart_status & UART_STAT_RXRDY ) {

		uint8_t c = LPC_USART1->RXDATA;

		// If CR flag EOL
		if (c=='\r') {
			//nmea_flags |= UART_BUF_FLAG_EOL;
			nmea_buf[nmea_buf_index]=0; // zero-terminate buffer
			//nmea_line++;
			nmea_buf_index = 0;

			nmea_word_count = 0;

			// $GPGGA NMEA sentence
			if (nmea_buf[3]=='G' && nmea_buf[4] == 'G' && nmea_buf[5] == 'A') {
				extern uint32_t systick_counter;
				gps_last_position_t = systick_counter;

				// Make copy from NMEA buffer to avoid data being clobbered in the background by incoming chars
				copy_nmea_word (gps_time_of_day, nmea_buf + nmea_words[0]);

				// Prefix with negative sign if south of equator
				if (nmea_buf[nmea_words[2]]=='S') {
					gps_latitude[0] = '-';
					copy_nmea_word (gps_latitude+1, nmea_buf + nmea_words[1]);
				} else {
					copy_nmea_word (gps_latitude, nmea_buf + nmea_words[1]);
				}

				// Prefix with negative sign of west of Greenwich meridian
				if (nmea_buf[nmea_words[4]]=='W') {
					gps_longitude[0] = '-';
					copy_nmea_word (gps_longitude+1, nmea_buf + nmea_words[3]);
				} else {
					copy_nmea_word (gps_longitude, nmea_buf + nmea_words[3]);
				}

				copy_nmea_word (gps_fix, nmea_buf + nmea_words[5]);
				copy_nmea_word (gps_hdop, nmea_buf + nmea_words[7]);
			}

			// $GPRMC NMEA sentence
			if (nmea_buf[3]=='R' && nmea_buf[4] == 'M' && nmea_buf[5] == 'C') {

				// If no heading substitute with "999"
				if ( nmea_word_len(6) == 0) {
					strcpy(gps_heading,"999");
				} else {
					copy_nmea_word (gps_heading, nmea_buf + nmea_words[6]);
				}

				copy_nmea_word (gps_speed, nmea_buf + nmea_words[7]);
			}

		} else if (c>31){

			if (nmea_buf_index == GPS_NMEA_SIZE) {
				nmea_buf[GPS_NMEA_SIZE-1]=0;
				nmea_buf_index = 0;
			}

			if (c == ',') {
				nmea_words[nmea_word_count++] = nmea_buf_index+1;
			}

			nmea_buf[nmea_buf_index++] = c;
		}

		// If bit 0 if gps_echo param set then echo to UART0
		if (params_union.params.gps_echo&0x1) {
			LPC_USART0->TXDATA = c;
		}

	} else if (uart_status & UART_STAT_TXRDY ){
		LPC_USART1->INTENCLR = 0x04;
	}
}
#endif<commit_msg>Use start of NMEA  sentence to estimate top-of-second systick time +/- 5ms<commit_after>
#include <string.h>

#include "LPC8xx.h"			/* LPC8xx Peripheral Registers */

#include "config.h"
#include "myuart.h"

#include "params.h"
extern params_union_type params_union;

extern uint32_t systick_counter;


#ifdef FEATURE_GPS_ON_USART1
//volatile uint32_t nmea_line = 0;
volatile uint32_t gps_last_position_t;
volatile uint32_t nmea_buf_index = 0;
//volatile uint32_t nmea_flags = 0;
volatile uint8_t nmea_words[24];
volatile int32_t nmea_word_count=0;
volatile uint8_t gps_latitude[12];
volatile uint8_t gps_longitude[12];
volatile uint8_t gps_time_of_day[12];
volatile uint8_t gps_hdop[8];
volatile uint8_t gps_fix[2];
volatile uint8_t gps_heading[8];          // heading in degrees (0=North)
volatile uint8_t gps_speed[8];            // speed from NMEA sentence (in knots)
volatile uint8_t nmea_buf[GPS_NMEA_SIZE]; // buffer for incoming NMEA sentences
volatile uint32_t gps_top_of_second_t;    // systick time of top of last UTC second
static volatile uint32_t gps_dollar_t;    // systick time at which NMEA '$' received
#endif



#ifdef FEATURE_GPS_ON_USART1

/**
 * Copy a data field from NMEA sentence to address dst. Like strcpy()
 * except terminating on comma ','.
 */
static void copy_nmea_word (uint8_t *dst, uint8_t *src) {
	while (*src != ',') {
		*dst = *src;
		dst++;
		src++;
	}
	// Terminate with 0
	*dst = 0;
}

/**
 * Get length of a NMEA sentence word
 */
static int nmea_word_len (int word_index) {
	return nmea_words[word_index+1] - nmea_words[word_index] - 1;
}

/**
 * Second UART (USART1) interrupt handler to handle incoming NMEA sentences
 * from GPS receiver. (Optional feature).
 */
void UART1_IRQHandler(void)
{
	uint32_t uart_status = LPC_USART1->STAT;

	// UM10601 §15.6.3, Table 162, p181. USART Status Register.
	// Bit 0 RXRDY: 1 = data is available to be read from RXDATA
	// Bit 2 TXRDY: 1 = data may be written to TXDATA
	if (uart_status & UART_STAT_RXRDY ) {

		uint8_t c = LPC_USART1->RXDATA;

		// If CR flag EOL
		if (c=='\r') {
			//nmea_flags |= UART_BUF_FLAG_EOL;
			nmea_buf[nmea_buf_index]=0; // zero-terminate buffer
			//nmea_line++;
			nmea_buf_index = 0;

			nmea_word_count = 0;

			// $GPGGA NMEA sentence
			if (nmea_buf[3]=='G' && nmea_buf[4] == 'G' && nmea_buf[5] == 'A') {
				extern uint32_t systick_counter;
				gps_last_position_t = systick_counter;

				// Make copy from NMEA buffer to avoid data being clobbered in the background by incoming chars
				copy_nmea_word (gps_time_of_day, nmea_buf + nmea_words[0]);

				// Prefix with negative sign if south of equator
				if (nmea_buf[nmea_words[2]]=='S') {
					gps_latitude[0] = '-';
					copy_nmea_word (gps_latitude+1, nmea_buf + nmea_words[1]);
				} else {
					copy_nmea_word (gps_latitude, nmea_buf + nmea_words[1]);
				}

				// Prefix with negative sign of west of Greenwich meridian
				if (nmea_buf[nmea_words[4]]=='W') {
					gps_longitude[0] = '-';
					copy_nmea_word (gps_longitude+1, nmea_buf + nmea_words[3]);
				} else {
					copy_nmea_word (gps_longitude, nmea_buf + nmea_words[3]);
				}

				copy_nmea_word (gps_fix, nmea_buf + nmea_words[5]);
				copy_nmea_word (gps_hdop, nmea_buf + nmea_words[7]);
			}

			// $GPRMC NMEA sentence
			if (nmea_buf[3]=='R' && nmea_buf[4] == 'M' && nmea_buf[5] == 'C') {

				// The "$" of $GPRMC sentence (the first in sequence) is measured by
				// oscilloscope to be about 250ms +/- 5ms after PPS signal. This allows
				// the systick timer to be tied to GPS UTC within +/- 5ms.
				gps_top_of_second_t = gps_dollar_t - 25;

				// If no heading substitute with "999"
				if ( nmea_word_len(6) == 0) {
					strcpy(gps_heading,"999");
				} else {
					copy_nmea_word (gps_heading, nmea_buf + nmea_words[6]);
				}

				copy_nmea_word (gps_speed, nmea_buf + nmea_words[7]);
			}

		} else if (c>31){

			if (c == '$') {
				gps_dollar_t = systick_counter;
			}

			if (nmea_buf_index == GPS_NMEA_SIZE) {
				nmea_buf[GPS_NMEA_SIZE-1]=0;
				nmea_buf_index = 0;
			}

			if (c == ',') {
				nmea_words[nmea_word_count++] = nmea_buf_index+1;
			}



			nmea_buf[nmea_buf_index++] = c;
		}

		// If bit 0 if gps_echo param set then echo to UART0
		if (params_union.params.gps_echo&0x1) {
			LPC_USART0->TXDATA = c;
		}

	} else if (uart_status & UART_STAT_TXRDY ){
		LPC_USART1->INTENCLR = 0x04;
	}
}
#endif
{'fraction_non_alphanumeric': 0.1247354662019171, 'fraction_numerical': 0.03174405576994896, 'mean_word_length': 3.617241379310345, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5901905', 'n_tokens_mistral': 3898, 'n_tokens_neox': 3375, 'n_words': 762}