diff --git a/tiny_array.c b/tiny_array.c new file mode 100644 index 0000000..efc695e --- /dev/null +++ b/tiny_array.c @@ -0,0 +1,234 @@ +#include +#include +#include + +#define LED PB1 +#define ITERATIONS 1000 + +uint32_t ticks = 0; +uint32_t sticks = 0; +uint32_t iticks = 0; +uint32_t lticks = 0; +uint32_t llticks = 0; +uint32_t fticks = 0; +uint32_t dticks = 0; +uint32_t ddticks = 0; + +void delay(uint32_t milliseconds) +{ + for(uint32_t i = 0; i < (milliseconds / 10); i++) + { + _delay_ms(10); + } +} + +void led_on() +{ + PORTB = PORTB | (1 << LED); +} + +void led_off() +{ + PORTB = PORTB & ~(1 << LED); +} + +/*void short_array() +{ + short array[50]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 18; + array[j] = array[j] * 18; + } + } +}*/ + +void int_array() +{ + int array[50]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 18; + array[j] = array[j] * 18; + } + } +} + +/*void long_array() +{ + long array[50]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 18; + array[j] = array[j] * 18; + } + } +} + +void longlong_array() +{ + long long array[50]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 18; + array[j] = array[j] * 18; + } + } +} + +void float_array() +{ + float array[100]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7.0; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 17.97; + array[j] = array[j] * 17.97; + } + } +} + +void double_array() +{ + double array[100]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7.0; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 17.97; + array[j] = array[j] * 17.97; + } + } +} + +void longdouble_array() +{ + long double array[50]; + for(int i = 0; i < 50; i++) + { + array[i] = 1000UL / 7.0; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < 50; j++) + { + array[j] = array[j] / 17.97; + array[j] = array[j] * 17.97; + } + } +}*/ + +void flash_num(uint32_t input) +{ + led_on(); + delay(2000); + led_off(); + for(int i = 0; i < 32; i++) + { + if((input >> i) % 2 == 0) + { + led_on(); + delay(250); + led_off(); + delay(1000); + } + else if((input >> i) % 2 == 1) + { + led_on(); + delay(750); + led_off(); + delay(1000); + } + } +} + +ISR(TIMER0_COMPA_vect) +{ + ticks = ticks + 1; +} + +int main(void) +{ + cli(); + DDRB = DDRB | (1 << LED); + TCCR0A = 0 | (1 << WGM01); + TCCR0B = 0 | (1 << CS02); + OCR0A = 255; + TIMSK = 0 | (1 << OCIE0A); + TCNT0 = 0; + sei(); + led_off(); + + + /*short_array(); + sticks = (ticks / 122);*/ + + int_array(); + iticks = (ticks / 122) - sticks; + + /* long_array(); + lticks = (ticks / 122) - iticks; + + longlong_array(); + llticks = (ticks / 122) - lticks; + + float_array(); + fticks = (ticks / 122) - llticks; + + double_array(); + dticks = (ticks / 122) - fticks; + + longdouble_array(); + ddticks = (ticks / 122) - dticks; */ + + cli(); + ticks = ticks / 122; + + while(1) + { + led_on(); + delay(10000); + led_off(); + + /*flash_num(sticks);*/ + flash_num(iticks); + /* flash_num(lticks); + flash_num(llticks); + flash_num(fticks); + flash_num(dticks); + flash_num(ddticks);*/ + } +} \ No newline at end of file diff --git a/tiny_blink.c b/tiny_blink.c new file mode 100644 index 0000000..8cb09d8 --- /dev/null +++ b/tiny_blink.c @@ -0,0 +1,127 @@ +#include +#include +#include + +#define LED PB1 +#define DATA PB5 + +uint32_t ticks = 0; + +void delay(uint32_t milliseconds) +{ + for(uint32_t i = 0; i < (milliseconds / 10); i++) + { + _delay_ms(16); + } +} + +void led_on() +{ + PORTB = PORTB | (1 << LED); +} + +void led_off() +{ + PORTB = PORTB & ~(1 << LED); +} + +void bit_toggle(int* reg, int index) +{ + if((*reg >> (16 - index)) % 2 == 0) + { + *reg = *reg | (1 << index); + PORTB = PORTB | (1 << DATA); + _delay_us(10); + PORTB = PORTB & ~(1 << DATA); + _delay_us(10); + } + else if((*reg >> (16 - index)) % 2 == 1) + { + *reg = *reg & ~(1 << index); + PORTB = PORTB & ~(1 << DATA); + _delay_us(10); + PORTB = PORTB & ~(1 << DATA); + _delay_us(10); + } +} + +ISR(TIMER0_COMPA_vect) +{ + ticks = ticks + 1; + // led_on(); + // _delay_ms(100); + // led_off(); + // _delay_ms(100); +} + +int main() +{ + + cli(); + DDRB = DDRB | (1 << LED) | (1 << DATA); + TCCR0A = 0 | (1 << WGM01); + TCCR0B = 0 | (1 << CS02); + OCR0A = 255; + TIMSK = 0 | (1 << OCIE0A); + TCNT0 = 0; + sei(); + led_off(); + + /*for(int i = 0; i < 1; i++) + { + + uint32_t seed = 17; + uint32_t increment = 12345; + uint32_t modulus = 2147483648; + uint32_t multiplier = 1103515245; + uint32_t iterator = ((multiplier * seed) + increment) % modulus; + uint32_t counter = 1; + + while(iterator != seed) + { + iterator = ((multiplier * iterator) + increment) % modulus; + counter = counter + 1; + } + }*/ + + int array[225]; + for(uint32_t i = 0; i < 1000; i++) + { + for(int j = 0; j < 225; j++) + { + for(int k = 0; k < 16; k++) + { + bit_toggle(&array[j], k); + } + } + } + + TIMSK = 0 & ~(1 << OCIE0A); + cli(); + ticks = ticks / 122; + + while(1) + { + led_on(); + delay(2000); + led_off(); + for(int i = 0; i < 32; i++) + { + if((ticks >> i) % 2 == 0) + { + led_on(); + delay(250); + led_off(); + delay(1000); + } + else if((ticks >> i) % 2 == 1) + { + led_on(); + delay(750); + led_off(); + delay(1000); + } + } + } +} + diff --git a/tiny_math.c b/tiny_math.c new file mode 100644 index 0000000..bc4903b --- /dev/null +++ b/tiny_math.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#define ALPHA 1.0e-6 + +uint8_t w[14 * 14]; +uint8_t x[14 * 14]; + +double sigmoid(double input) +{ + return 1.0 / (1.0 + exp(-input)); +} + +double loss_calc(bool true_value, double computed_value) +{ + return (true_value) ? -log(computed_value) : -log(1 - computed_value); +} + +double matrix_multiply(uint8_t* w, uint8_t* x) +{ + +} diff --git a/tiny_memmove.c b/tiny_memmove.c new file mode 100644 index 0000000..51a356e --- /dev/null +++ b/tiny_memmove.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include + +#define LED PB1 +#define SIZE 230 +#define ITERATIONS 1000000 + +volatile uint32_t ticks = 0; +bool flag = false; +uint32_t loop_const; +uint16_t delay_const; +uint8_t source[SIZE]; +uint8_t destination[SIZE]; + +void delay(uint16_t milliseconds) +{ + for(delay_const = 0; delay_const < (milliseconds / 10); delay_const++) + { + _delay_ms(10); + } +} + +void led_on() +{ + PORTB = PORTB | (1 << LED); +} + +void led_off() +{ + PORTB = PORTB & ~(1 << LED); +} + +void flash_num(uint32_t input) +{ + led_on(); + delay(2000); + led_off(); + for(loop_const = 0; loop_const < 32; loop_const++) + { + if((input >> loop_const) % 2 == 0) + { + led_on(); + delay(250); + led_off(); + delay(1000); + } + else if((input >> loop_const) % 2 == 1) + { + led_on(); + delay(750); + led_off(); + delay(1000); + } + } +} + +ISR(TIMER1_COMPA_vect) +{ + ticks = ticks + 1; + if((ticks % (122L * 5L)) == 0) + { + led_on(); + delay(10); + led_off(); + } +} + +int main(void) +{ + cli(); + GTCCR = GTCCR | (1 << TSM) | (1 << PSR0) | (1 << PSR1); + DDRB = DDRB | (1 << LED); + //TCCR0A = 0 | (1 << WGM01); + //TCCR0B = 0 | (1 << CS02); + //OCR0A = 255; + TIMSK = 0 | (1 << OCIE1A); + TCCR1 = 0 | (1 << CTC1) | (1 << CS13) | (1 << CS10); + OCR1C = 255; + OCR1A = 255; + //TCNT0 = 0; + TCNT1 = 0; + GTCCR = GTCCR & ~(1 << TSM); + sei(); + led_off(); + + for(loop_const = 0; loop_const < SIZE; loop_const++) + { + source[loop_const] = loop_const; + } + ticks = 0; + + for(loop_const = 0; loop_const < ITERATIONS / 2; loop_const++) + { + memcpy(destination, source, sizeof(uint8_t) * SIZE); + memcpy(source, destination, sizeof(uint8_t) * SIZE); + } + + cli(); + ticks = ticks / 122; + + for(loop_const = 0; loop_const < SIZE; loop_const++) + { + if(source[loop_const] != loop_const) + { + flag = true; + } + } + + if(flag == false) + { + while(1) + { + flash_num(ticks); + } + } + else + { + led_on(); + while(1) + { + + } + } +} \ No newline at end of file diff --git a/tiny_pwm.c b/tiny_pwm.c new file mode 100644 index 0000000..cb03aa4 --- /dev/null +++ b/tiny_pwm.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + +#define LED PB1 +#define INT PB2 +#define PCINT PB0 +#define PWM PB4 +#define SIZE 100 +#define ITERATIONS 1000000 + +volatile uint32_t ticks = 0; +volatile uint32_t debounce = 0; +volatile bool flag = false; +volatile double array[SIZE]; + +void delay(uint32_t milliseconds) +{ + for(uint32_t i = 0; i < (milliseconds / 10); i++) + { + _delay_ms(10); + } +} + +void led_on() +{ + PORTB = PORTB | (1 << LED); +} + +void led_off() +{ + PORTB = PORTB & ~(1 << LED); +} + +void flash_num(uint32_t input) +{ + led_on(); + delay(2000); + led_off(); + for(int i = 0; i < 32; i++) + { + if((input >> i) % 2 == 0) + { + led_on(); + delay(250); + led_off(); + delay(1000); + } + else if((input >> i) % 2 == 1) + { + led_on(); + delay(750); + led_off(); + delay(1000); + } + } +} + +ISR(TIMER0_COMPA_vect) +{ + if(flag == false) + { + PORTB = PORTB | (1 << PWM); + flag = true; + } + else if(flag == true) + { + PORTB = PORTB & ~(1 << PWM); + flag = false; + } +} + +ISR(TIMER1_COMPA_vect) +{ + ticks = ticks + 1; + if(debounce > 0) + { + debounce = debounce - 1; + } + if((ticks % 122) == 0) + { + led_on(); + delay(10); + led_off(); + } +} + +ISR(INT0_vect) +{ + if(((debounce > 0) || ((PINB >> INT) % 2 == 0))) + { + return; + } + + led_off(); + + array[7] = array[7] + 21.73456; + debounce = 30; +} + +ISR(PCINT0_vect) +{ + if(((debounce > 0) || ((PINB >> PCINT) % 2 == 0))) + { + return; + } + + led_on(); + + array[17] = array[17] + 10091.89; + debounce = 30; +} + +int main(void) +{ + cli(); + GTCCR = GTCCR | (1 << TSM) | (1 << PSR0) | (1 << PSR1); + MCUCR = MCUCR | (1 << ISC01) | (1 << ISC00); + GIMSK = GIMSK | (1 << INT0) | (1 << PCIE); + PCMSK = PCMSK | (1 << PCINT3); + DDRB = DDRB | (1 << LED) | (1 << PWM) | (0 << INT) | (0 << PCINT); + PORTB = PORTB | (1 << INT) | (1 << PCINT); + TCCR0A = 0 | (1 << WGM01); + TCCR0B = 0 | (1 << CS02); + OCR0A = 63; + TIMSK = 0 | (1 << OCIE0A) | (1 << OCIE1A); + TCCR1 = 0 | (1 << CTC1) | (1 << CS13) | (1 << CS10); + OCR1C = 255; + OCR1A = 255; + TCNT0 = 0; + GTCCR = GTCCR & ~(1 << TSM); + sei(); + led_off(); + + for(int i = 0; i < SIZE; i++) + { + array[i] = 1000000UL / 7.0; + } + for(uint32_t i = 0; i < ITERATIONS; i++) + { + for(int j = 0; j < SIZE; j++) + { + array[j] = array[j] + 23.002; + array[j] = array[j] - 23.002; + + array[j] = array[j] / 17.97; + array[j] = array[j] * 17.97; + + array[j] = pow(3.14159, array[j]); + array[j] = log(array[j]) / log(3.14159); + + array[j] = sin(array[j]); + array[j] = asin(array[j]); + + array[j] = cos(array[j]); + array[j] = acos(array[j]); + + array[j] = tan(array[j]); + array[j] = atan(array[j]); + } + } + + cli(); + ticks = ticks / 122; + + while(1) + { + flash_num(ticks); + } +} diff --git a/tiny_tone_client.c b/tiny_tone_client.c new file mode 100644 index 0000000..1c499b5 --- /dev/null +++ b/tiny_tone_client.c @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +#include +#include + +#define TIMEOUT 5000 +#define DEVICE_VENDOR_ID 0x16C0 +#define DEVICE_VENDOR "I can feel everything unravel." +#define DEVICE_PRODUCT_ID 0x05DC +#define DEVICE_PRODUCT "DigiSpark" +#define USB_TONE_WRITE 17 +#define SIGINT 2 + +int usb_get_descriptor_string(struct usb_dev_handle *dev, int index, int lang_id, char *buffer, int buffer_length) +{ + char lbuffer[256]; + int return_value; + int cindex; + + // send control message to device, requesting descriptor. + return_value = usb_control_msg(dev, USB_TYPE_STANDARD | USB_ENDPOINT_IN | USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, + ((USB_DT_STRING) << 8) + index, lang_id, lbuffer, sizeof(lbuffer), TIMEOUT); + + // error out as underlying function failed. + if(return_value < 0) + { + fprintf(stderr, "ERROR: usb_control_msg errored out with return value %i.\n", return_value); + return return_value; + } + + // actual size of response is actually the first byte of buffer + return_value = (uint8_t)(lbuffer[0]); + // ensure data type is the right one or error out + if(lbuffer[1] != USB_DT_STRING) + { + fprintf(stderr, "ERROR: Received invalid response type.\n"); + return 0; + } + + // begin conversion of UTF-16 little-endian response to "ISO-Latin1" whose lower half is standard ASCII. + return_value = return_value / 2; + + for(cindex = 1; (cindex < return_value) && (cindex < buffer_length); cindex = cindex + 1) + { + if(lbuffer[(2 * cindex) + 1] == 0) + { + buffer[cindex - 1] = lbuffer[2 * cindex]; + } + else + { + buffer[cindex - 1] = '?'; /* outside of ISO Latin1 range */ + } + } + buffer[cindex - 1] = '\0'; + return cindex - 1; +} + +static usb_dev_handle* usb_open_device(int vendor, char* match_device_vendor, int product, char* match_device_product) +{ + struct usb_bus* bus; + struct usb_device* dev; + char device_vendor[256]; + char device_product[256]; + struct usb_dev_handle* handle = NULL; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + // iterate over all devices attached to all busses + for(bus = usb_get_busses(); bus; bus=bus->next) + { + for(dev=bus->devices; dev; dev=dev->next) + { + if(dev->descriptor.idVendor != vendor || dev->descriptor.idProduct != product) + { + continue; + } + + // open the device to pass on to usb_get_descriptor_string, error on failure + if(!(handle = usb_open(dev))) + { + fprintf(stderr, "WARNING: cannot open USB device: %s\n", usb_strerror()); + continue; + } + + // get vendor name, 0x0409 is English Language + if(usb_get_descriptor_string(handle, dev->descriptor.iManufacturer, 0x0409, device_vendor, + sizeof(device_vendor)) < 0) + { + fprintf(stderr, "WARNING: cannot query manufacturer for device: %s\n", usb_strerror()); + usb_close(handle); + continue; + } + + // get produce name + if(usb_get_descriptor_string(handle, dev->descriptor.iProduct, 0x0409, device_product, + sizeof(device_product)) < 0) + { + fprintf(stderr, "WARNING: cannot query product for device: %s\n", usb_strerror()); + usb_close(handle); + continue; + } + + if((strcmp(device_vendor, match_device_vendor) == 0) && (strcmp(device_product, match_device_product) == 0)) + { + return handle; + } + else + { + usb_close(handle); + } + } + } + + // no match + return NULL; +} + +void sigint_handler() +{ + printf("\nGoodbye!\n"); + exit(0); +} + +int main(int argc, char** argv) +{ + signal(SIGINT, sigint_handler); + int return_value; + char buffer[256]; + struct usb_dev_handle* handle = usb_open_device(DEVICE_VENDOR_ID, DEVICE_VENDOR, DEVICE_PRODUCT_ID, DEVICE_PRODUCT); + if(handle == NULL) + { + fprintf(stderr, "ERROR: Could not find requested device.\n"); + return 1; + } + + uint16_t frequency; + uint16_t time_period; + while(true) + { + printf("Enter frequency command: "); + scanf("%hi", &frequency); + printf("Enter time period command: "); + scanf("%hi", &time_period); + return_value = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, frequency, time_period, (char *)buffer, sizeof(buffer), TIMEOUT); + + if(return_value < 0) + { + fprintf(stderr, "ERROR: Error while sending messages.\nExiting...\n"); + return 1; + } + else + { + printf("Command succeeded.\n"); + } + sleep(1); + } + + /*usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 500, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "1\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 1000, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "2\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 1500, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "3\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 2000, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "4\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 2500, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "5\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 3000, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "6\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 3500, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "7\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 4000, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "8\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 4500, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "9\n"); + sleep(2); + usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + USB_TONE_WRITE, 5000, 1500, (char *)buffer, sizeof(buffer), TIMEOUT); + fprintf(stderr, "10\n"); + sleep(2); */ +} \ No newline at end of file