Skip to content

Instantly share code, notes, and snippets.

View nscooling's full-sized avatar

Niall Cooling nscooling

View GitHub Profile
@nscooling
nscooling / CMakeLists.txt
Created March 21, 2024 09:42
Freestanding doctest on STM32 target using arm-eabi-none-g++
include(ExternalProject)
find_package(Git REQUIRED)
ExternalProject_Add(
doctest
PREFIX ${CMAKE_BINARY_DIR}/doctest
GIT_REPOSITORY https://github.com/doctest/doctest.git
TIMEOUT 10
UPDATE_COMMAND ${GIT_EXECUTABLE} pull
CONFIGURE_COMMAND ""
@nscooling
nscooling / main.c
Created May 19, 2021 10:09
RBC-201.1 Numerical Issues - Integer
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
void test_unsigned_add_8(uint8_t a, uint8_t b){
uint8_t c;
c = a + b;
assert(c == 0); // (A1)
}
int main(void)
{
*rcc_ahb1enr |= (1 << 3); // enable PortD's clock
// assume powerup conditions, moder is all 0's
// set bit-8 as output
uint32_t moder = portd->MODER;
moder |= (1 << 16);
moder &= ~(1 << 17);
portd->MODER = moder;
volatile GPIO_t* const portd = (GPIO_t*)0x40020C00;
#include <stdint.h>
typedef struct
{
uint32_t MODER; // mode register, offset: 0x00
uint32_t OTYPER; // output type register, offset: 0x04
uint32_t OSPEEDR; // output speed register, offset: 0x08
uint32_t PUPDR; // pull-up/pull-down register, offset: 0x0C
uint32_t IDR; // input data register, offset: 0x10
uint32_t ODR; // output data register, offset: 0x14
@nscooling
nscooling / main.c
Created January 18, 2019 14:38
embedded-hello-world-1
#include <stdint.h>
#include "timer.h"
volatile uint32_t* const portd_moder = (uint32_t*) 0x40020C00;
volatile uint32_t* const portd_odr = (uint32_t*) 0x40020C14;
volatile uint32_t* const rcc_ahb1enr = (uint32_t*) 0x40023830;
extern void sleep(uint32_t ms); // use systick to busy-wait
// $ ./bufferOverflow -2147482047
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#define MAX_BUF_SZ 1024
int copySize;
int s[MAX_BUF_SZ*2]; // source buffer
#include <stdio.h>
int main(void)
{
unsigned int ui = 16;
printf("%u * 16 = %u\n", ui, ui*16);
printf("%u << 4 = %u\n", ui, ui << 4);
return 0;
}
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
int main(void)
{
assert(sizeof(int)==4);
int intMin = INT_MIN;
#include <stdio.h>
#include <limits.h>
#include <assert.h>
int main(void)
{
assert(sizeof(unsigned char)==1);
unsigned char uc1 = 0xff;
unsigned char uc2 = 0;