<commit_before>#include "robot.hpp"
#include "console.hpp"
#include "commands.hpp"
#include "logger.hpp"
#include "radio.hpp"

Ticker lifeLight;
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);

/*
 * some forward declarations to make the code easier to read
 */
void imAlive(void);
void setISRPriorities(void);
void initRadioThread(void);
void initConsoleRoutine(void);

/**
 * system entry point
 */
int main(void) 
{
	setISRPriorities();
	lifeLight.attach(&imAlive, 0.25);

	isLogging = true;
	rjLogLevel = INF2;
	
	//initRadioThread();
	initConsoleRoutine();
}

/**
 * Initializes the peripheral nested vector interrupt controller (PNVIC) with 
 * appropriate values. Low values have the higest priority (with system 
 * interrupts having negative priority). All maskable interrupts are
 * diabled; do not intialize any interrupt frameworks before this funtion is
 * called. PNVIC interrupt priorities are independent of thread and NVIC 
 * priorities.
 * 
 * The PNVIC is independent of the NVIC responsible for system NMI's. The NVIC
 * is not accissable through the library, so in this context the NVIC functions
 * refer to the PNVIC. The configuration system for PNVIC priorities is strange
 * and different from X86. If you haven't already, look over 
 * doc/ARM-Cortex-M_Interrupt-Priorities.pdf from RJ root and the online 
 * documentation regarding Interrupt Priority Registers (IPRs) first. 
 */
void setISRPriorities(void)<commit_msg>Reading serial number from ID chip works.<commit_after>#include "robot.hpp"
#include "console.hpp"
#include "commands.hpp"
#include "logger.hpp"
#include "radio.hpp"

Ticker lifeLight;
DigitalOut ledOne(LED1);
DigitalOut ledTwo(LED2);

DigitalOut led(p20, 0);

/*
 * some forward declarations to make the code easier to read
 */
void imAlive(void);
void setISRPriorities(void);
void initRadioThread(void);
void initConsoleRoutine(void);

void writeByte(DigitalInOut*, char);
char readByte(DigitalInOut* pin);
unsigned int crc8_add(unsigned int acc, char b);

const int tREC = 5;
const int tSLOT = 65;
const int tRSTLmin = 480;
const int tRSTLmax = 640;
const int tRSTLavg = 540;
const int tPDHmin = 15;
const int tPDHmax = 60;
const int tPDHavg = 35;
const int tPDLmin = 60;
const int tPDLmax = 240;
const int tPDLavg = 150;
const int tFPD = 8;
const int tMSPmin = 60;
const int tMSPmax = 75;
const int tMSPavg = 68;
const int tRSTH = 480;
const int tW0Lmin = 60;
const int tW0Lmax = 120;
const int tW0Lavg = 90;
const int tW1Lmin = 5;
const int tW1Lmax = 15;
const int tW1Lavg = 10;
const int tRLmin = 5;
const int tRLmax = 15;
const int tRLavg = 6;
const int tMSR = 15;

/**
 * system entry point
 */
int main(void) 
{
	setISRPriorities();
	lifeLight.attach(&imAlive, 0.25);

	DigitalInOut idPin(p21, PIN_OUTPUT, PullUp, 1);

	printf("\033[2J");
	printf("Communicating with ID Chip...\r\n");

	// Reset signal, low for 480us
	idPin = 0;
	wait_us(tRSTLavg); // Trstl
	idPin = 1;

	// wait for presence response
	wait_us(tMSPavg); // Tmsp
	idPin.input();
	if(idPin == 0) {
		wait_us(tRSTH - tMSPavg); // wait for rest of tRSTH

		writeByte(&idPin, 0x33);

		char family = readByte(&idPin);

		char serial[6];
		for(int i = 0; i < 6; i++)
			serial[i] = readByte(&idPin);

		char crc = readByte(&idPin);

		printf("Family byte  : 0x%x\r\n", family);
		
		printf("Serial byte  : 0x");
		for(int i = 5; i >= 0; i--)
			printf("%x", serial[i]);

		printf("\r\nCRC          : 0x%x \r\n", crc);

		unsigned int calcCRC = crc8_add(0x0, family);
		for(int i = 0; i < 6; i++)
			calcCRC = crc8_add(calcCRC, serial[i]);

		printf("CRCs match?  : %d\r\n", calcCRC == crc);

		for(int i = 0; i < 17; i++)
			printf("\n");

		while(1) {
			led = !led;
			wait(1.0);
		}
	}
	else {
		printf("Failure :(\r\n");
		while(1) {
			led = !led;
			wait(0.5);
		}
	}

	isLogging = true;
	rjLogLevel = INF2;
	
	//initRadioThread();
	initConsoleRoutine();
}

void writeOne(DigitalInOut* pin) {
	*pin = 0;
	wait_us(tW1Lavg);
	*pin = 1;
	wait_us(tSLOT);
}

void writeZero(DigitalInOut* pin) {
	*pin = 0;
	wait_us(tW0Lavg);
	*pin = 1;
	wait_us(tREC);
}

void writeByte(DigitalInOut* pin, char b) {
	pin->output();

	for(uint i = 1; i < 256; i <<= 1) {
		if((b & i) == i)
			writeOne(pin);
		else
			writeZero(pin);
	}

	printf("Sent         : 0x%x\r\n", b);
}

char readByte(DigitalInOut* pin) {
	char value = 0;
	for(int i = 0; i < 8; i++) {
		pin->output();
		*pin = 0;
		wait_us(tRLavg);
		*pin = 1;

		wait_us(tMSR - tRLavg);
		pin->input();

		int bit = *pin;
		value |= bit << i;

		wait_us(tSLOT - tMSR);
	}

	return value;
}

unsigned int crc8_add(unsigned int acc, char byte) {
	acc ^= byte;

	for(int i = 0; i < 8; i++) {
		if(acc & 1) {
			acc = (acc >> 1) ^ 0x8c;
		}
		else {
			acc >>= 1;
		}
	}

	return acc;
}

/**
 * Initializes the peripheral nested vector interrupt controller (PNVIC) with 
 * appropriate values. Low values have the higest priority (with system 
 * interrupts having negative priority). All maskable interrupts are
 * diabled; do not intialize any interrupt frameworks before this funtion is
 * called. PNVIC interrupt priorities are independent of thread and NVIC 
 * priorities.
 * 
 * The PNVIC is independent of the NVIC responsible for system NMI's. The NVIC
 * is not accissable through the library, so in this context the NVIC functions
 * refer to the PNVIC. The configuration system for PNVIC priorities is strange
 * and different from X86. If you haven't already, look over 
 * doc/ARM-Cortex-M_Interrupt-Priorities.pdf from RJ root and the online 
 * documentation regarding Interrupt Priority Registers (IPRs) first. 
 */
void setISRPriorities(void)
{'fraction_non_alphanumeric': 0.12484438911613017, 'fraction_numerical': 0.025609105459719013, 'mean_word_length': 3.758037225042301, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '5605826', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2022, 'n_words': 655}