Home > Mobile >  Beggining on Raspberry PI Pico RP2040 - blink.c Low Level Programming in C
Beggining on Raspberry PI Pico RP2040 - blink.c Low Level Programming in C

Time:09-23

I want to be short. I got one Pico yesterday and I spent the evening yesterday making a blink.c (from Raspberry site) work on mi Pico through Linux and I made it work.

Now I want to make myblink.c which is a blink.c in Low Level programming (registers and all of that). But the last time I did a low level programming was 5 years ago on a MSP430, and I can't remember the basics anymore. Could someone please help me? Sorry. How can I make this low level programming in C compile a uf2 file which makes my raspberry pi pico blink his LED which is on the 25 pin? It compile, but the Pico doesnt do nothing. Obviously this code is not correct, what I can change to make it work? Thank you.

Below are myblink.c and CMakeLists.txt:

#include <stdio.h>
#include <stdlib.h>

#define SIO_BASE        0xd0000000
#define GPIO_IN         0xd0000004
#define GPIO_HI_IN      0xd0000008
#define GPIO_OUT        0xd0000010
#define GPIO_OUT_SET    0xd0000014
#define GPIO_OUT_CLR    0xd0000018
#define GPIO_OUT_XOR    0xd000001c
#define GPIO_OE         0xd0000020
#define GPIO_OE_SET     0xd0000024
#define GPIO_OE_CLR     0xd0000028
#define GPIO_OE_XOR     0xd000002c
#define GPIO_HI_OUT     0xd0000030

typedef unsigned int uint;

void write32(uint dst, uint val){
    uint dst_u = (uint)dst;
    dst_u = val;  
    return;
}

uint read32(uint src){
    uint src_u = (uint)src;
    return src;
}

int main(){
    uint gpoes = read32(GPIO_OE);
    gpoes |= (1<<25);
    write32(GPIO_OE, gpoes);

    //int i=0;

    while(1){
        //turn on pin 0
        write32(GPIO_OUT, 1<<25);
        //delay
        //while (i < 0x80000){
        //    i  ;    
        //}
        //turn off pin 0
        //write32(GPIO_OUT_CLR, 1<<25);
        //delay
        //while (i < 0x80000){
        //    i  ;    
        //}
    }        
}
cmake_minimum_required(VERSION 3.12)

# PUll in PICO SDK (must be before project)
include(pico_sdk_import.cmake)

project(myblink C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

add_executable(myblink
    myblink.c
    )

# Pull in our pico_stdlib which pulls in common
target_link_libraries(myblink pico_stdlib)

# create map/bin/hex file etc.
pico_add_extra_outputs(myblink)

CodePudding user response:

I haven't used this specific target, but generally in case you have no pre-defined register map with all those registers already defined, then you can define them yourself by following the advise here: How to access a hardware register from firmware?

We can then fix your code like this:

// #include <stdio.h> avoid stdio.h on microcontrollers
#include <stdint.h>

#define SIO_BASE (*(volatile uint32_t*)0xd0000000u)
// ... and so on

// typedef unsigned int uint; get rid of home-made garage standard types

// replace "magic numbers" with meaningful names, "PORTX_PIN25" or whatever it might be called in hw
#define MEANINGFUL_NAME_HERE (1u << 25) 

int main(){
    GPIO_OE |= MEANINGFUL_NAME_HERE;

    while(1){
        GPIO_OUT |= MEANINGFUL_NAME_HERE;

        // write an actually working busy-delay loop by including volatile:
        for(volatile int i=0; i<0x80000; i  )
        {}

        GPIO_OUT_CLR |= MEANINGFUL_NAME_HERE;
        // I'm assuming GPIO_OUT &= ~MEANINGFUL_NAME_HERE; would work too

        for(volatile int i=0; i<0x80000; i  )
        {}
    }        
}

Probably you could also just do a bit toggle: GPIO_OUT_CLR ^= ... (bitwise XOR). Then you just need 1 delay loop.

CodePudding user response:

Try correcting your read and write functions.

Here is what the read function should look like:

uint read32(addr){
    uint* base = (uint*)addr;
    uint value = *base;
    return value; }

Here is what the write function should look like:

void write32(void *dst, uint val){
    uint* dst_u = (uint*)dst;
    *dst_u = val;
    return;
}

After all, I don't know much about the Rpi pico but this seems to be one of the problems in your code.

  • Related