Skip to content

Commit

Permalink
looked through code, cleaned it up or added extra explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
OscarKro committed Jan 5, 2021
1 parent 3a8e719 commit 07fd14c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
7 changes: 3 additions & 4 deletions library/targets/hwlib-mimxrt1062.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ namespace mimxrt1062
};
/**
* @brief Struct containing the core pin structs that map a chip pad to the configuration register adresses and IO ports
* @details all this info is gotten from the consumer reference manual in combination with the Arduino IDE library. This may be a bit vague
* but every pin was tested for it's functionality and confirmed working
*
*/
constexpr core_pin core_pin_struct_array[40] =
Expand Down Expand Up @@ -139,9 +141,6 @@ namespace mimxrt1062
*/
inline void writeIOMUXPADCTL(int n, uint32_t mask)
{
// IOMUXC->SW_PAD_CTL_PAD[n] &= ~(0b111111 << 10);
// IOMUXC->SW_PAD_CTL_PAD[n] &= ~(0b11111 << 3);
// IOMUXC->SW_PAD_CTL_PAD[n] &= ~0b1;
uint32_t clearMask = ~((0b111111 << 10) | (0b11111 << 3) | 0b1);
IOMUXC->SW_PAD_CTL_PAD[n] &= clearMask;
IOMUXC->SW_PAD_CTL_PAD[n] |= mask;
Expand All @@ -150,7 +149,7 @@ namespace mimxrt1062
/// the number of ticks per us
int_fast64_t HWLIB_WEAK ticks_per_us()
{
return 600; // this number should be the same as the cpu freq in Mhz
return 600; // this number should be the same as the cpu freq in Mhz, do not touch if you do not know what you are doing
}

uint_fast64_t HWLIB_WEAK now_ticks()
Expand Down
34 changes: 13 additions & 21 deletions library/targets/hwlib-teensy_40.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ namespace teensy_40

void flush() override
{
// what to do?
}
/**
* @brief Function to Toggle the GPIO on and off
Expand Down Expand Up @@ -211,7 +210,7 @@ namespace teensy_40
public:
pin_adc(pins pin_number) : hwlib::adc(12), myCorePin(mimxrt1062::core_pin_struct_array[(int)pin_number])
{
if (myCorePin.ad_channel == 255) // what to do if pin_number is not an adc pin
if (myCorePin.ad_channel == 0XFFFFFFFF) // what to do if pin_number is not an adc pin
{
return;
}
Expand Down Expand Up @@ -259,8 +258,7 @@ namespace teensy_40

void refresh() override
{
// didn't know what a refresh should do here, just did a new calibration for now.
// calibrate using the on chip calibration function
// calibrate using the on chip calibration function
ADC1->GC |= (0b1 << 7);
while((ADC1->GC & (0b1 << 7)) != 0){} // check if the CAL bit is still high, if not, calibration is done
return;
Expand Down Expand Up @@ -316,7 +314,7 @@ namespace teensy_40

void flush() override
{
// Function not implemented, calling write immidiately writes a pin
// Function not implemented, calling write immidiately writes to a pin
}

};
Expand All @@ -337,24 +335,18 @@ namespace teensy_40
{
return (reinterpret_cast<uint32_t>(reinterpret_cast<GPIO_Type *>(myCorePin.GPIO_port_base_adress)->PSR) & (1 << myCorePin.GPIO_port_bit_number)) != 0;
}
/**
* @brief This function writes a bit over the oc pin
* @details Notice that for both a zero and a 1, this function also waits 1 us_busy to make sure the output pin is set to either input or ouput as is necessary for an oc pin in this style.
*
* @param x the bit that needs to be written
*/

void write(bool x) override
{
if (x)
{
reinterpret_cast<GPIO_Type *>(myCorePin.GPIO_port_base_adress)->GDIR &= ~(1 << myCorePin.GPIO_port_bit_number); // set the pin to read mode
wait_32_nops();
wait_32_nops(); // this is needed (tested thoroughly!) do not touch. This is the shortest time that is needed to wait to let the pins change the input mode
}
else
{
reinterpret_cast<GPIO_Type *>(myCorePin.GPIO_port_base_adress)->GDIR |= (1 << myCorePin.GPIO_port_bit_number); // set the pin to write mode
// this wait is necessary for letting the pin go to another mode. (32 asm("nops") also do the trick)
wait_32_nops();
wait_32_nops(); // this is needed (tested thoroughly!) do not touch. This is the shortest time that is needed to wait to let the pins change the input mode
}
}
void refresh() override
Expand All @@ -365,7 +357,6 @@ namespace teensy_40

void flush() override
{
// what to do?
}

};
Expand All @@ -380,7 +371,8 @@ namespace teensy_40

#ifdef _HWLIB_ONCE
// NOTICE! To use UART on the Teensy 4.0, you need to use pin 0 (rx) and pin 1 (tx) in combination with an TTL to USB hardware piece.
// Teensy does not have this on board (as fas as I know), and so it is not implemented to use the standard USB
// Teensy does not have this on board (as fas as I know), and so it is not implemented to use the standard USB. Other TX or RX pins will not work.
// I used a USB to TTL converter to read out the UART pins. Works good.
void uart_init()
{
static bool init_done = false;
Expand Down Expand Up @@ -460,8 +452,8 @@ namespace teensy_40
// uart_init() is not needed because uart_char_available does that
while( ! uart_char_available() )
{
dont_optimize();
// TODO: hwlib::background::do_background_work();
//dont_optimize();
hwlib::background::do_background_work();
}
const mimxrt1062::core_pin & rx = mimxrt1062::core_pin_struct_array[0]; // teensy 4.0 rx1
return reinterpret_cast<LPUART_Type *>(rx.serial_base_adress) -> DATA;
Expand All @@ -473,8 +465,8 @@ namespace teensy_40
const mimxrt1062::core_pin & tx = mimxrt1062::core_pin_struct_array[1]; // teensy 4.0 tx1
while((reinterpret_cast<LPUART_Type*>(tx.serial_base_adress) -> STAT & (0b1 << 22)) == 0)
{
dont_optimize();
// TODO: hwlib::background::do_background_work();
//dont_optimize();
hwlib::background::do_background_work();
}
reinterpret_cast<LPUART_Type *>(tx.serial_base_adress) -> DATA |= c;
}
Expand Down Expand Up @@ -558,7 +550,7 @@ return teensy_40::uart_getc();
auto end = now_us() + n;
while (now_us() < end)
{
// background::do_background_work();
background::do_background_work();
}
}

Expand Down

0 comments on commit 07fd14c

Please sign in to comment.